这就是我所做的,因为它似乎是唯一的方法。
1) 构建了一个名为“zqry_PlaceHolder”的查询。在我的情况下,这是正在使用的数据集(只有 WHERE 会改变)。我确实在此 (WHERE dbo_project.projectid = "_") 中添加了一个 WHERE 子句,但它不会返回任何内容,以免延迟主窗体的显示。
2) 在子表单中,将源对象设置为我刚刚构建的查询“Query.zqry_PlaceHolder”。这是必需的。
3) 当用户单击主窗体上的按钮时,VBA 代码将运行。它在字符串变量sWhereFilter中创建 WHERE 子句,然后将其添加到 SQL 字符串中:
'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;"
然后我抛出一个“请稍候”表单,让用户知道事情正在处理,并将子表单的 RecordSource 设置为构建的 SQL 字符串:
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)
这有效,并且不会更改占位符查询“zqry_PlaceHolder”。
我希望这对其他人有帮助,因为它对我来说并不那么直观。如果有另一种/更好的方法,我完全愿意学习新东西!