通过 vba 毫秒访问将多值列的数据绑定到组合框中
Binding multivalued column's data into combo box via vba ms access
我有一个table如下
其中 BranchIds 列是引用分支 ID 的多值列 table。
我需要如何将 id 及其相关值绑定到驻留在另一个访问表单中的组合框中,如下所示
权限table包含允许哪个用户访问哪个分支的数据。
我无法将 perimssion table 中的分支绑定到我另一种形式的组合框。
The code i am trying. Got from MSDN after a length search..
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
多值字段与值列表关系不大。
只需使用 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 将自动重新查询组合框。
我有一个table如下
其中 BranchIds 列是引用分支 ID 的多值列 table。
我需要如何将 id 及其相关值绑定到驻留在另一个访问表单中的组合框中,如下所示
权限table包含允许哪个用户访问哪个分支的数据。 我无法将 perimssion table 中的分支绑定到我另一种形式的组合框。
The code i am trying. Got from MSDN after a length search..
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
多值字段与值列表关系不大。
只需使用 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 将自动重新查询组合框。