'Assumes Combo1 has the data to filter and Table1 is source and Query1 exists
Private Sub Combo1_AfterUpdate()
Dim Obj_QueryDef As Object
Dim Temp_QueryName As Variant
'Save Query Name for refreshing Form Data (Query will be overwritten!)
Temp_QueryName = Me.Form.RecordSource
'dereference the query definition object that already exists
Set Obj_QueryDef = Me.Form.Application.DBEngine.Workspaces(0).Databases(0).QueryDefs(Temp_QueryName)
'For Number Key
Obj_QueryDef.SQL = "SELECT * FROM Table1 WHERE [Field1] = " & Combo1 & ";"
'Reset the Record Query then repaint the form
Me.Form.RecordSource = Temp_QueryName
Me.Form.Repaint
End Sub
'To add Add More Combo Boxes, After update Regenerate SQL adding & " AND Field2 = " Combo2 & ";"
'Or better yet Create a Function that handles the SQL statement
'You can use the same idea to limit the items that appear in the dropdown selection if you update
' the record source for the combobox
'reference for different data types:
'String Data 'String Value' use chr(39) = '
'Obj_QueryDef.SQL = "SELECT * FROM Table1 WHERE Field1 = " & Chr(39) & Combo1 & Chr(39) & ";"
'For Date Use #Date Time# Use chr(35) = #
'Obj_QueryDef.SQL = "SELECT * FROM Table1 WHERE Field1 = " & Chr(35) & Combo1 & Chr(35) & ";"
2 回答
我在这里有点困惑,但听起来你想使用一个参数传递给查询 . 是对的吗?这样的事,对吧 .
如果这是你想要的方向,请阅读下面的链接,并下载URL底部的示例文件 .
http://www.fontstuff.com/access/acctut17a.htm
http://www.fontstuff.com/access/acctut18.htm
您需要创建一个包含所有数据的基本查询,然后对于每个组合框,您将需要重新创建并运行查询 . 我通常在更新后事件上编程 . 我还创建并存储查询,以便当用户返回到表单时,它返回到与之前相同的数据 . 请注意,在查询执行和更新子表单时,这可能会导致错误 .
表格上的示例