【问题标题】:Binding multivalued column's data into combo box via vba ms access通过 vba ms 访问将多值列的数据绑定到组合框中
【发布时间】:2019-12-11 09:54:28
【问题描述】:

我有一张如下表

BranchIds 列是一个多值列,引用了分支表的 id。 我需要如何将 id 及其相关值绑定在位于另一个访问表单中的组合框中,如下所示

权限表包含允许哪个用户访问哪个分支的数据。 我无法将 perimssion 表中的分支绑定到另一种形式的组合框。

我正在尝试的代码。经过长度搜索后从 MSDN 获得..

Sub BindBranches()
   Me.comboBranchIds.RowSourceType = "Value List"

   Dim db As Database
   Dim rs As Recordset
   Dim childRS As Recordset

   Set db = CurrentDb()

   ' Open a Recordset for the Tasks table.
   Set rs = db.OpenRecordset("SELECT BranchIds FROM Permissions WHERE UserId = " & Forms!NavigationForm!txtSession.Value)
   rs.MoveFirst

   Do Until rs.EOF
      ' Print the name of the task to the Immediate window.
      'Debug.Print rs!TaskName.Value

      ' Open a Recordset for the multivalued field.
      Set childRS = rs!BranchIds.Value

         ' Exit the loop if the multivalued field contains no records.
         Do Until childRS.EOF
             childRS.MoveFirst

             ' Loop through the records in the child recordset.
             Do Until childRS.EOF
                 ' Print the owner(s) of the task to the Immediate
                 ' window.
                 'Debug.Print Chr(0), childRS!Value.Value
                 Me.comboBranchIds.AddItem Item:=childRS!Value.Value
                 'Me.comboBranchIds.RowSource = "SELECT BranchName FROM Branches WHERE ID = " + childRS!Value.Value
                 childRS.MoveNext
             Loop
         Loop
      rs.MoveNext
   Loop
End Sub

【问题讨论】:

    标签: vba ms-access combobox


    【解决方案1】:

    MultiValue 字段与值列表关系不大。

    只需使用 MultiValue 字段的 RowSource 属性,例如:

    SELECT [TableName].[FieldName] FROM [TableName] ORDER BY [Id];
    

    作为组合框的 RowSource 属性。

    有一个过滤器:

    SELECT [TableName].[FieldName] 
    FROM [TableName] 
    WHERE UserId = [Forms]![NavigationForm]![txtSession]
    ORDER BY [Id];
    

    或者通过代码修改SQL,比如:

    SELECT [TableName].[FieldName] 
    FROM [TableName] 
    WHERE UserId = 466
    ORDER BY [Id];
    

    应用修改后的 SQL 将自动重新查询组合框。

    【讨论】:

    • 首先感谢您的紧急响应。我在哪里包括标准/ where 子句。我的意思是我需要权限表中特定用户 ID 的分支 ID。
    • 查询就是它默认的显示方式。对于您的新组合框,您可以按常规方式添加/包含任何过滤器或其他排序方式。
    • 嗨@Gustav。我用 Query 想出了一个解决方案。你能告诉如何将参数传递给组合框行源中的查询
    • 组合没有被填充
    • 对不起。我的错误是使用 txtSession.Text。现在删除了那个多余的词:“文本”。谢谢。它的工作完美
    猜你喜欢
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多