vba 如何重新查询表单内的子表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13427201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Requery a subform inside a form?
提问by Paolo Bernasconi
I'm having a problem in which I can't requery a subform inside of a form in Access.
我遇到了一个问题,我无法在 Access 中的表单内重新查询子表单。
The form's name is frmSearch The subform's name is SearchResults
表单名称为 frmSearch 子表单名称为 SearchResults
I've tried
我试过了
Private Sub Command38_Click()
Me!SearchResults.Form.Requery (or)
Me.SearchResults.Form.Requery
End Sub
My form & subform look like this:
我的表单和子表单如下所示:
To be clear, I'm using the "Search" button to create a string which contains the textbox and combobox values. This string creates a SQL query called qryTrialQuery. Then my subform makes a query of the qryTrialQuery and produces the results in the table bellow.
需要明确的是,我正在使用“搜索”按钮创建一个包含文本框和组合框值的字符串。该字符串创建一个名为 qryTrialQuery 的 SQL 查询。然后我的子表单查询 qryTrialQuery 并在下表中生成结果。
I would like to be able to press the search button and then the results appear below it immediately after. The problem is, is that the results don't appear unless I close and reopen the form.
我希望能够按下搜索按钮,然后结果立即出现在它的下方。问题是,除非我关闭并重新打开表单,否则结果不会出现。
Thanks for all your help in advance.
提前感谢您的所有帮助。
Update
更新
The following is the code I used to create a query from the textbox and combobox values.
以下是我用来从文本框和组合框值创建查询的代码。
LineOne = "SELECT tblPoolPersonnel.LName, tblPoolPersonnel.FName, tblPoolPersonnel.[Tel Natel], tblPoolPersonnel.[Tel Home], tblPoolPersonnel.Email" & vbCrLf
LineTwo = "FROM (tblPoolPersonnel INNER JOIN tblDayAvailable ON tblPoolPersonnel.Code_Personal = tblDayAvailable.Code_Personal) INNER JOIN tblServiceYES ON tblPoolPersonnel.Code_Personal = tblServiceYES.Code_Personal" & vbCrLf
LineThree = "WHERE (((tblServiceYES.Service)=" & comboService & ") AND ((tblDayAvailable.Availability)=True) AND ((tblDayAvailable.Date)=" & txtDate & ") AND ((tblDayAvailable.CodeHoraire1)=" & comboCodeHoraire & "));"
Set qdf = CurrentDb.QueryDefs("myQuery")
Application.RefreshDatabaseWindow
strSQL = LineOne & LineTwo & LineThree
qdf.SQL = strSQL
qdf.Close
Set qdf = Nothing
Set dbs = Nothing
回答by Renaud Bompuis
Assuming you are reconstructing the SQL of your query based on the criteria the user selected, you should just be able to do something like this:
假设您正在根据用户选择的条件重建查询的 SQL,您应该能够执行以下操作:
Private Sub Command38_Click()
Dim qryTrialQuery as String
...
' code to construct the SQL SELECT statement for the query, '
' based on the criteria the user entered '
...
SubForm.Form.RecordSource = qryTrialQuery
End Sub
Setting the Subform's RecordSource
will refresh the data.
设置子窗体RecordSource
将刷新数据。
回答by Jay
You can try this:
你可以试试这个:
Dim frm as Form
Set frm = frmSearch
frmSearch!SearchResults.Form.Requery