使用组合框作为访问表单上的数据条目

时间:2018-07-05 20:08:10

标签: forms ms-access drop-down-menu combobox

我想知道您是否可以在访问中的表单上使用组合框作为查询的数据条目。

例如,我有一个基于查询的表单,并且我想使用组合框根据组合框中的值来编辑查询末尾为空白的字段,类似于下拉菜单查询或表格中的框。

我为要输入的数据值创建了一个列,这些值将根据组合框选择所在的记录页面进入给定值。

如果需要,我可以添加其他信息。

2 个答案:

答案 0 :(得分:0)

我在这里有点困惑,但这听起来像您想在查询中使用传递参数。那正确吗?像这样的东西。

enter image description here

如果这是您想要的方向,请仔细阅读下面的链接,然后下载URL底部的示例文件。

http://www.fontstuff.com/access/acctut17a.htm

http://www.fontstuff.com/access/acctut18.htm

答案 1 :(得分:0)

您需要创建一个包含所有数据的基本查询,然后对于每个组合框,您将需要重新创建并运行查询。我通常在更新后事件上对此进行编程。我还创建并存储查询,以便当用户返回到表单时它返回与以前相同的数据。请注意,在执行查询和更新子窗体时,这可能会导致错误。

表格上的示例

'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) & ";"
相关问题