MS 访问组合框
Posted
技术标签:
【中文标题】MS 访问组合框【英文标题】:MS ACCESS COMBOBOX 【发布时间】:2021-02-18 00:01:35 【问题描述】:美好的一天!
我正在使用 ms 访问,我想知道如果我的表单中有 5 个组合框,然后有 10 个名称选择连接在一个表中,我会怎么做,如果我选择列表中的一个名称,如何做到这一点那么所选列表将不会显示在第二个组合框列表中。我的列表中有 5 个组合框。看起来像这样
【问题讨论】:
第二个组合框 RowSource 必须是仅检索数据表中尚未存在的值的查询。更新组合框 1 后重新查询组合框 2。类似:SELECT field FROM table1 WHERE ID NOT IN (SELECT ID_FK FROM table2)
或使用查询向导并构建 FindUnmatched 查询。这一直是许多讨论的话题。 ***.com/questions/55615054/…。做研究,当你有特定的代码有问题时,发布问题。但是,要包含当前记录中的值,请使用 UNION 查询。
如果你有多个字段存储同一种数据,听起来像是一个非规范化的数据结构。
为什么要编辑字段和表名?
您应该阅读如何规范化数据库。您可能需要选定员工的子表。
【参考方案1】:
考虑切换到值列表并使用 VBA 中的查询将项目添加到框中。然后在进行更改时从其他框中删除和添加项目。
Option Explicit
' Have to use global variables because combobox.oldValue is not reliable
Dim strOld1 As String
Dim strOld2 As String
Dim strOld3 As String
Dim strOld4 As String
Dim strOld5 As String
Private Sub frmMain_Load()
Dim rsNames as Recordset
' Get names
Set rsNames = CurrentDB.OpenRecordset( _
"SELECT [Names] " & _
"FROM tblPerson")
' Setup recordset
If rsNames.RecordCount > 0 Then
rsNames.MoveLast
rsNames.MoveFirst
' Add names to all boxes
Do While Not rsNames.EOF
cboNames1.addItem rsNames.Field("Name")
cboNames2.addItem rsNames.Field("Name")
cboNames3.addItem rsNames.Field("Name")
cboNames4.addItem rsNames.Field("Name")
cboNames5.addItem rsNames.Field("Name")
rsNames.MoveNext
End If
' Dispose recordset asap
Set rsNames = Nothing
End Sub
Private Sub addRemoveItem(ByRef thisCombo As Variant, ByRef oldValue As String)
Dim arrCombos(1 To 5) As ComboBox
Dim otherCombo As Variant
Dim intIndex As Integer
' Get a list of all combo boxes
Set arrCombos(1) = Me.cboNames1
Set arrCombos(2) = Me.cboNames2
Set arrCombos(3) = Me.cboNames3
Set arrCombos(4) = Me.cboNames4
Set arrCombos(5) = Me.cboNames5
' Check for comboboxes that are not the one changed
For Each otherCombo in arrCombos
If otherCombo.ControlName <> thisCombo.ControlName Then
' Search for exisitng item
IntIndex = 0
Do While otherCombo.itemData(intIndex) <> thisCombo.Value _
And intIndex < otherCombo.ListCount
intIndex = intIndex + 1
Loop
' Remove the found item
otherCombo.removeItem intIndex
' Add unselected value back
If oldValue <> "" Then
otherCombo.addItem oldValue
End if
Next
' Change the old value to the new one
oldValue = thisCombo.Value
End Sub
Private Sub cboName1_Change()
RemoveAddItem Me.cboName1, strOld1
End Sub
Private Sub cboName2_Change()
RemoveAddItem Me.cboName2, strOld2
End Sub
Private Sub cboName3_Change()
RemoveAddItem Me.cboName3, strOld3
End Sub
Private Sub cboName4_Change()
RemoveAddItem Me.cboName4, strOld4
End Sub
Private Sub cboName5_Change()
RemoveAddItem Me.cboName5, strOld5
End Sub
抱歉,我是通过手机完成的...
【讨论】:
请标记为答案。以上是关于MS 访问组合框的主要内容,如果未能解决你的问题,请参考以下文章