I have some code to populate a single subform on a main form with a recordset. The problem is that the records are not shown on the subform. If I open the subform as a form on it's own then with almost the same code it works perfectly:
Forms!frmCorrespondanceHolidays.RecordSource = strsql ' WORKS FINE
Me.frmCorrespondanceHolidays.Form.RecordSource = strsql 'SHOWS NO RECORDS
I have tested that if I just paste the value of strSQL into a query the correct records are returned.
I am beginning to think it is a bug in Access. I have tried recreating the database by creating a new one and importing all the objects. I have also compact & repaired. Not sure what else to try!
The name of a subform is different than the Source name of the sub report. Here is where you need to look for the name to reference in VBA. You select the subform once when the Parent form is in Design view and the look at the properties.
After further investigation I have found it is related to the sql rather than the subform. If the sql includes a query that doesn't reference the parent form records are shown. If the sql includes a query that does reference the form then no record are shown. Guess I will have to work around this limitation. Thanks for the pointers.
Related
I have a form, that is bound to a query. There are sveral condition on filtering the record and one of them is list box where I can select conrete values. I have a macro, that insert IN() statement into the SQL with selected values and the query results working properly and is showing the correct records.. Never the less when I use form.requery it still shows all of the items and it is not showing the results from query). When I change other filters (for example in combobox) then the form requery correctly although both of the changes launch the same macro...
I finally found a solution :) I Just put Me.RecordSource = Me.RecordSource :)
If your recordset is bound to the form just use
Me.Requery
If it is on your form within a form (subform) then use
Me!SubFormName.Form.Requery
If your form is not bound to a recordset/query you need to set the SQL string again.
I have a simple database I am developing in MS Access 2010 where I have a form with a "view specific" command button and a "view all" command button. Both buttons will run a query: the specific one should apply a criteria to one of the fields, and the all one should run the query with no criteria, or with a * criteria in that field. There is another field that requires criteria for both queries, so essentially the specific button should run a query with two criteria and the all button should run a query with one criteria (or one and an * criteria). The query is a stored query I created via Design View. The "all" SQL statement is something to the effect of:
SELECT tableA.field1, tableA.field2, tableB.field1, tableB.field2
FROM tableA INNER JOIN tableB ON tableA.field3=tableB.field2
WHERE tableB.field1=True;
I want the specific query to run based off of an option chosen in a combo box on the form, so it's SQL statement should look the same, except the WHERE clause would change to:
WHERE tableB.field1=True AND tableB.field2=Forms!ViewQryResults!comboSelectSpecific;
Problem
My problem is that the specific query, I'll call it Query2, works as intended, but the all query, Query1, doesn't change the results viewed on the form. Neither query throws an error message, and it seems that the query is running again as the record total at the bottom of the screen goes away, and then comes back after the query finishes and gets a total number of records again.
Attempted Solutions
I have tried multiple different solutions to attempt to get it to work, none of which have.
I first tried to establish a parameter, but then Access prompts the user for the parameter value as soon as the form opens, which I do not want. If I simply declare the parameter using the parameter button in the ribbon in design view for the query, but do not assign it to a field as a criterion in the design grid, Access no longer prompts me for the parameter value, and I can assign its value via VBA, but then I do not know how to assign the parameter to a field criteria using VBA. My attempts throw errors that say Access doesn't recognize the name of my parameter.
I then tried altering the SQL string for the query and then requerying. I used text manipulation functions to find the WHERE clause, and replace it with a WHERE clause that has both criteria, then requeryed. To determine the proper context for the SQL string, I changed the query in Design View to do what I wanted, and then switched to SQL View and copied the SQL string that Access created to represent the query.
I then inserted a Debug.Print line to print the SQL string at each step, and compared them character by character and they match exactly, meaning the text manipulation is doing what it should be doing and creating the SQL strings correctly, or at least as Access would if I wasn't messing with them at all. I tried this from both directions, i.e. I made the stored query in the way it should be for one button, and then used code to change it for the other button, and then swapped, but neither method was successful in getting both queries to work. The basic code I tried for manipulating the SQL string is as follows:
Private Sub Query2_OnClick()
Dim d As Database
Dim q As Query
Dim strSQL As String
Dim strSQLSelectFrom As String
Dim strSQLWhereNew As String
Set d = CurrentDb
Set q = d.QueryDefs("Query2")
strSQL = q.SQL 'Saves original SQL statement
'Text Manipulations to create strSQLSelectFrom as just the SELECT and FROM portions
strSQLWhereNew = "tableB.field1=True AND tableB.field2=Forms!ViewQryResults!comboSelectSpecific;"
q.SQL = strSQLSelectFrom & strSQLWhereNew
DoCmd.Requery
Me.Refresh
Debug.Print q.SQL 'Verify text manipulations created SQL String I expect
q.SQL = strSQL 'Returns SQL to original statement
Debug.Print q.SQL 'Verify SQL statement properly returned to original statement
End Sub
This arrangement works to allow me to query specifics based on my combo box. When I click the other button, whose code just does a requery because the first button should reset SQL to the way it was, the results do not change. The query appears to run as indicated by the total records counter going away and coming back, but it never changes the results. If I go back and choose another option from my combo box, and query specific, the results update to the new specific selection.
Again, if I click the All Query button, the results remain based on the combo box selection. If I change the combo box selection, and then click All Query (which shouldn't care at all about the combo box because it isn't listed anywhere in the code for that button), the results change to the new selection, like both buttons are running specific queries. That part is the most puzzling, I can figure out why it is doing that.
I know a simple solution would be just to create a second query and stop trying to change one back and forth, but I really feel I am missing a fundamental piece that I am going to need to understand at some point, so I am really looking for that fundamental piece that will make this work.
IIUC - Simply change the form's RecordSource to point to needed saved query with .Requery:
Private Sub ViewAll_OnClick()
Me.Form.RecordSource = "Query1"
Me.Requery
End Sub
Private Sub ViewSpecific_OnClick()
Me.Form.RecordSource = "Query2"
Me.Requery
End Sub
Alternatively, you can keep the same query recordsource and filter form using DoCmd.ApplyFilter, assuming criteria controls are on same form requiring filtering. Any valid SQL WHERE clause can be used in second argument and even dynamically created.
DoCmd.ApplyFilter , "field1 = True AND field2 = '" & Forms!ViewQryResults!comboSelectSpecific & "'"
And to remove filter:
DoCmd.RunCommand acCmdRemoveAllFilters
I have a combobox on a subform (ProgramSubform) in Access that's supposed to list report years for a project from a table (Program). Most projects have more than one report year, but the combobox always brings only 2014.
For example, the dropdown for project 5278 should be:
2012
2013
2014
Instead, it's only
2014
This is the select query that I'm using. It was working properly before, but I don't know when it stopped working; no changes were made to the tables or subform.
SELECT Program.ReportYear
FROM Program
WHERE (((Program.ProjNo)='ProgramSubform.ProjNo'));
Any idea why it might have stopped working, or how to fix it?
This WHERE clause asks Access to limit the rows returned by the query to those whose ProjNo values match the text string 'ProgramSubform.ProjNo'
WHERE (((Program.ProjNo)='ProgramSubform.ProjNo'))
But ProgramSubform.ProjNo is actually a data control on a subform. So don't include quotes around its name.
You can use a reference to the control via the parent form in the Forms collection:
WHERE Program.ProjNo= Forms![Parent Form]!ProgramSubform!ProjNo
If you're building the SELECT statement with VBA code in the form, you can include the control's value instead of its name:
"WHERE Program.ProjNo=" & Me!ProgramSubform!ProjNo.Value
"WHERE Program.ProjNo='" & Me!ProgramSubform!ProjNo.Value & "'"
Use the first version for a numeric field or the second for text.
Notes:
I assumed ProgramSubform is the name of the subform control. It could also be the name of the form contained in that subform control. But those names can be different. So make sure you used the subform control name.
This query is used as the Row Source for a combo box on the subform. You want to update the values displayed in the combo whenever ProjNo.Value changes. You can accomplish that by calling the combo's Requery method from the After Update event of ProjNo.
Pay attention to the brackets, you have three before Program.ProjNo on WHERE clause, but you only need two brackets (in this case) you should use:
SELECT Program.ReportYear
FROM Program
WHERE ((Program.ProjNo='ProgramSubform.ProjNo'));
anyway you will prefer:
SELECT Program.ReportYear
FROM Program
WHERE Program.ProjNo='ProgramSubform.ProjNo';
When I click the Combobox, the dropdown list is populated, yet when I try to click on an option it says "Recordset is not updateable".. I have tried to read up on this error yet can't identify the issue behind this. The joins aren't complicated and it has worked before. The query source for the AwardsSubFrm is attached:
Anyone know how to fix this?
The image of the query design indicates your record source is a GROUP BY query. The recordset from any GROUP BY query is never updateable. You can verify that point by opening the query in Datasheet View of the query designer and confirm Access won't let you change any of the values.
I don't know what the fix is for your situation, but you need something other than just a GROUP BY query. Perhaps a subform whose record source contains the table rows you want to update, and link it to the main form so it displays those records which are related to the current main form row.
I have a problem that Im completely stumped by. Or rather, I have a work-around that is messy and I don't understand why what I want to do doesn't work:
I have a mainform / subform nest. The subform is linked by master/child fields (neither form is editable- so Im using the recordsource field directly rather than a separate control). The subform is a continuous form; so the subform displays the records related to the master in the mainform when the current record changes. The subform is based on a query- there are no parameters or filters being applied- the query presents the recordset exactly as needed. This all works perfectly (in that when the record of the mainform changes, so to do the related subform records).
Problem arises when I want the subform to be based on a different a different query (ie based on different tables)- but with the same unique-key (the 'ID') and fields as the original query. The queries all work fine, and displaying them in the form works fine.
The issue arises when I change the recordsource of the subform (so from QueryA to QueryB). The records the subform are correct (ie the query results are displayed as expected)- but the LinkChildFields is no longer functioning: the subform no longer shows the related records from the query- it displays them all. In other words, the form displays the entire recordset as if I had no LinkChild / LinkMaster set.
So, the code in the AfterUpdate event of the toggle control in the MainForm is ;
Private Sub optActRep_AfterUpdate()
Select Case Me.optActRep
Case 1
MainFormSubFormControl.Form.RecordSource = "QueryA"
Case 2
MainFormSubFormControl.Form.RecordSource = "QueryB"
End Select
Ive debugged and the property 'linkchldfields' is still set correctly ('ID'). Ive tried setting these to vbNullString before the recordsource update and resetting after the recordsource update (same issue). The odd thing is if I update the linkchildfield property in the mainform (in the Current Event)- then it works and seems to fire. So,
Debug.print Me!Subform.LinkChildFields
correctly returns 'ID'. The only way I can make it fire is by coding;
Me!Subform.LinkChildFields = "ID"
into the Current event of the Mainform (ie so that every time the MainForm record changes, the correct QueryB record is displayed)... even though Im only setting the property to what my debug is telling me it is already set to. Its as if it no longer fires.
Very confused.
Access continues to do the daftest things.
Nothing to do with the form setup at all. The underlying query (QueryB) was based on a Crosstab query. Whilst the query worked fine independently of the form (including when directly using the query as the forms record source), it obviously didnt like using the product of one of the fields as the Unique key (even though it was the same format type and Grouped / Row rather than being a cross-tab field).
I changed the crosstab part of the query to a Grouped query (using IIF & Sum to manually create the Crosstab-equivalent field values) and the form now works fine.
My lesson learnt- don't use Crosstab queries! Apologies for the red-herring.
Surferosa
Apologies for the late reply, I only found this thread when I encountered problems similar to yours. I'm not sure the Crosstab query is the cause of your problem.
I can set LinkMasterFields to be either a column name or a control name.
I can only set LinkChildFields to be a column name, setting it to a control name does NOT work. Ouch.
You can change LinkChildFields in the sub-form control either in Design View or via VBA, but in many cases Access continues to use the previous value even though it displays the new value. This is true for sub-forms whose record source is either a table or a simple two table query. I do not have the time or the patience to characterise this problem any further. Sorry !
My solution is to set LinkChildFields in the sub-form control to a value e.g 'ID' and then create an alias with that name in each and every record source used by my different sub-forms. Then you only need to change the Source Object when you switch sub-forms.
Hope this helps !
HB