MS 访问组合框
MS ACCESS COMBOBOX
美好的一天!
我正在使用 ms access,我想知道如果我的表单中有 5 个组合框然后有 10 个名称选择连接成一个 table,我该怎么办 table,如果我 select 列表中的名称之一,然后 selected 列表将不会显示在第二个组合框列表中。我的列表中有 5 个组合框。看起来像这样
考虑切换到值列表并使用 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
抱歉,我是在 phone...
上做的
美好的一天!
我正在使用 ms access,我想知道如果我的表单中有 5 个组合框然后有 10 个名称选择连接成一个 table,我该怎么办 table,如果我 select 列表中的名称之一,然后 selected 列表将不会显示在第二个组合框列表中。我的列表中有 5 个组合框。看起来像这样
考虑切换到值列表并使用 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
抱歉,我是在 phone...
上做的