Filtering combo boxes from a list of names in a table (1 Viewer)

seeker9969

New member
Local time
Today, 01:02
Joined
Jan 25, 2020
Messages
23
I have a table with a list of names.
on the form I have 4 combo boxes all pulling from the same table. Is there a way to remove a name from the next combo box once its been picked in one of the other combo boxes?
 

Micron

AWF VIP
Local time
Today, 04:02
Joined
Oct 20, 2018
Messages
3,478
What you want is commonly referred to as cascading combo boxes. Lots of posts everywhere on the subject so no real need on how to reproduce the behaviour here, I think. At least you now know what to search on?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:02
Joined
May 21, 2018
Messages
8,527
This one is a little more complex IMO since you can select from the combos in any order. I had to try to demo
Code:
Public Function StrNames()
  Dim ctrl As Access.Control
 
  For Each ctrl In Me.Controls
    If ctrl.Tag = "?" Then
      If Not IsNull(ctrl) Then
        If StrNames = "" Then
          StrNames = "'" & ctrl & "'"
        Else
          StrNames = StrNames & ", '" & ctrl & "'"
        End If
      End If
    End If
  Next ctrl
 
  If StrNames <> "" Then
    StrNames = "(" & StrNames & ")"
  Else
    StrNames = "('Something not in your list')"
  End If
 
End Function

Private Sub cmboName_Enter()
  Dim strSql As String
  strSql = "Select Full_Name from tblData where Full_name not in " & StrNames & " ORDER BY Full_name"
  Debug.Print strSql
  cmboName.RowSource = strSql
 
End Sub

Private Sub cmboName2_Enter()
  Dim strSql As String
  strSql = "Select Full_Name from tblData where Full_name not in " & StrNames & " ORDER BY Full_name"
  'Debug.Print strSql
  cmboName2.RowSource = strSql
End Sub

Private Sub cmboName3_Enter()
  Dim strSql As String
  strSql = "Select Full_Name from tblData where Full_name not in " & StrNames & " ORDER BY Full_name"
  'Debug.Print strSql
  cmboName3.RowSource = strSql
End Sub

Private Sub cmboName4_Enter()
  Dim strSql As String
  strSql = "Select Full_Name from tblData where Full_name not in " & StrNames & " ORDER BY Full_name"
  'Debug.Print strSql
  cmboName4.RowSource = strSql
End Sub
 

Attachments

  • 4Comob.zip
    27.1 KB · Views: 94

Micron

AWF VIP
Local time
Today, 04:02
Joined
Oct 20, 2018
Messages
3,478
@MajP, that's impressive. Would/could you not also just select all the combos in design view and set their Enter property to call one function that in turn calls strName? Or is 4 combos not enough (number wise) to go that route, in your opinion?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:02
Joined
May 21, 2018
Messages
8,527
hat's impressive. Would/could you not also just select all the combos in design view and set their Enter property to call one function that in turn calls strName? Or is 4 combos not enough (number wise) to go that route, in your opinion?
I was going to do that, but thought the OP might have a harder time following if they had not seen that technique before.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:02
Joined
May 21, 2018
Messages
8,527
Code:
Public Function StrNames()
  Dim ctrl As Access.Control
 
  For Each ctrl In Me.Controls
    If ctrl.Tag = "?" Then
      If Not IsNull(ctrl) Then
        If StrNames = "" Then
          StrNames = "'" & ctrl & "'"
        Else
          StrNames = StrNames & ", '" & ctrl & "'"
        End If
      End If
    End If
  Next ctrl
 
  If StrNames <> "" Then
    StrNames = "(" & StrNames & ")"
  Else
    StrNames = "('Something not in your list')"
  End If
 
End Function

Private Function FilterCOmbos()
  Dim ctrl As ComboBox
  Set ctrl = Me.ActiveControl
  ctrl.RowSource = "Select Full_Name from tblData where Full_name not in " & StrNames & " ORDER BY Full_name"
End Function
in the onEnter of the combox
=FilterCombos()
 

seeker9969

New member
Local time
Today, 01:02
Joined
Jan 25, 2020
Messages
23
Thats awesome MajP I wondered in the table you were pulling the data from it has 6 fields are those necessary? As the table I want to pull from only has the Id field and the name field. Thank you very much that will work great.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:02
Joined
May 21, 2018
Messages
8,527
MajP I wondered in the table you were pulling the data from it has 6 fields are those necessary?
They are not necessary, but they are not used either so makes no difference. The combo boxes are based on a query that returns only one field. Your queries will have to be modified to match your names. Are you wanting to filter on the name or the id. If you are doing the ID which is numeric than you have to lose the single quotes
StrNames = "'" & ctrl & "'"
becomes
StrNames = " & ctrl
 

Users who are viewing this thread

Top Bottom