Question

I have a main form, frm_Main, and it has a subform in it, subform_Child. When the main form is displayed the subform is shown as well, it's just blank because there's no data in it (which is perfectly fine). When the user clicks a button on the main form the code builds a SQL string that contains a WHERE clause based upon what the user has entered in a text box. So far so good, I've done this before. Now the part that I cannot figure out (because I haven't done it before) is populating that subform using the SQL just created! Building the SQL string is no problem, I just don't know Access VBA well enough to know what code to use to update the subform from the main form and have it display the recordset after the query runs.

Edit: It's my understanding that using a QueryDef isn't a recommended method, correct?

Was it helpful?

Solution

Here's what I did as it seems to be the only way to do it.

1) Built a query named "zqry_PlaceHolder". In my case this is the data set that is being used (only the WHERE will change). I did add a WHERE clause in this (WHERE dbo_project.projectid = "_") but only so it would return nothing so as not to delay the displaying of the main form.

2) In the subform set the Source Object to the query I just built, "Query.zqry_PlaceHolder". This is required.

3) When the user clicks the button on the main form the VBA code is run. It creates the WHERE clause in the string variable sWhereFilter and then adds it to the SQL string:

'Build SQL string
sSQL = Empty  'Ensure string is empty
sSQL = "SELECT Proj_Locs.ProjectID, dbo_PROJECTS.PROJECTID, Proj_Locs.Location, Master_Scans.FileName, Proj_Locs.Title, dbo_PROJECTS.TITLE" & vbCrLf
sSQL = sSQL & "FROM (tbl_Project_Locations AS Proj_Locs" & vbCrLf
sSQL = sSQL & "    LEFT JOIN dbo_PROJECT AS dbo_PROJECTS ON Proj_Locs.ProjectID = dbo_PROJECTS.PROJECTID)" & vbCrLf
sSQL = sSQL & "    LEFT JOIN tblMasterScans AS Master_Scans ON Proj_Locs.ProjectID = Master_Scans.Project" & vbCrLf
sSQL = sSQL & sWhereFilter
sSQL = sSQL & "ORDER BY Proj_Locs.ProjectID ASC, dbo_PROJECTS.PROJECTID ASC;"

Then I throw up a "Please wait" form to let the user know things are processing and set the RecordSource of the subform to the built SQL string:

DoCmd.OpenForm "frm_PleaseWait", acNormal
DoCmd.Hourglass True
DoCmd.RepaintObject acForm, "frm_PleaseWait"  'Needed to show everything in the form immediately, otherwise there's a delay
Me.subform_Results.Form.RecordSource = sSQL  'This updates the subform AND executes it, refreshing the results
Beep  'Let user know processing completed
Me.txt_RecordsReturned = Me.subform_Results.Form.Recordset.RecordCount  'Let user know how many records were returned with their query (if any)

This works and it does not change the placeholder query "zqry_PlaceHolder".

I hope this helps others as it wasn't as intuitive to me. If there is another/better method I'm all for learning something new!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top