首页 文章

使用组合框作为Access窗体上的数据条目

提问于
浏览
-1

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

例如,我有一个基于查询的表单,我想使用组合框根据组合框中的值编辑查询端空白的字段,类似于下拉框中的下拉框 . 查询或表格 .

我为要输入的数据值创建了一个列,并且值将根据创建组合框的记录页面进入给定值 .

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

2 回答

  • 0

    我在这里有点困惑,但听起来你想使用一个参数传递给查询 . 是对的吗?这样的事,对吧 .

    enter image description here

    如果这是你想要的方向,请阅读下面的链接,并下载URL底部的示例文件 .

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

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

  • 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) & ";"
    

相关问题