A Better Solution to Cascading Comboxes? (1 Viewer)

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:57
Joined
May 21, 2018
Messages
8,525
A few questions have come up about Command Bars recently.

I use this idea pretty frequently because I think it is cleaner and at times easier than creating cascading comboboxes. I always thought the idea would catch on, but this is the first time I have ever seen people asking about it.
In this example if I click on "Select Location" I get a command bar where I can pick the country, then the city, and a location in the city.
commandbar.jpg
The code is really not that much to do this. This example may seem a little complicated because it loops three recordsets, but the code for actually building the commandbar is very short and simple once you see it.

Code to build the the command bars
Code:
Public Sub LoadCommandBar()
  Const comBarName = "comBarLocations"
  Dim i As Integer

  Dim rsCountries As DAO.Recordset
  Dim rsCities As DAO.Recordset
  Dim rsLocations As DAO.Recordset
   
  Dim comBarLocations As Office.CommandBar

  Dim comBarControl_Lvl_1 As Office.CommandBarControl
  Dim comBarControl_Lvl_2 As Office.CommandBarControl
  Dim comBarControl_Lvl_3 As Office.CommandBarControl

  Set rsCountries = CurrentDb.OpenRecordset("Select Distinct CountryID, Country from qryLocations Order By Country")


  If isCommandBar(comBarName) Then
    Application.CommandBars(comBarName).Delete
  End If

  'Build tha bar
  Set comBarLocations = CommandBars.Add(comBarName, msoBarPopup, False, False)
  'build the levels
 
  Do While Not rsCountries.EOF
    'level 1
    Set comBarControl_Lvl_1 = comBarLocations.Controls.Add(msoControlPopup)
    comBarControl_Lvl_1.Caption = rsCountries!Country
    Set rsCities = CurrentDb.OpenRecordset("Select Distinct CityID, City from qryLocations where CountryID = " & rsCountries!CountryID & " Order by City")
    Do While Not rsCities.EOF
        'Level_2
        Set comBarControl_Lvl_2 = comBarControl_Lvl_1.Controls.Add(msoControlPopup)
        comBarControl_Lvl_2.Caption = rsCities!City
        'Level_3 Not popup
        Set rsLocations = CurrentDb.OpenRecordset("Select Distinct LocationID, Location from qryLocations where CityID = " & rsCities!CityID & " Order by Location")
        Do While Not rsLocations.EOF
          Set comBarControl_Lvl_3 = comBarControl_Lvl_2.Controls.Add()
          comBarControl_Lvl_3.Caption = rsLocations!Location
          comBarControl_Lvl_3.Tag = rsLocations!LocationID
          comBarControl_Lvl_3.OnAction = "SelectLocation"
          rsLocations.MoveNext
        Loop
        rsCities.MoveNext
    Loop
    rsCountries.MoveNext
  Loop

End Sub
Public Function isCommandBar(strBarName As String) As Boolean
  Dim cb As CommandBar
  For Each cb In Application.CommandBars
    If cb.Name = strBarName Then
      isCommandBar = True
    End If
  Next cb
End Function

Public Sub SelectLocation()
  Dim comBarCtl As CommandBarControl
  Set comBarCtl = CommandBars.ActionControl
  With Forms("frmSelectLocation")
    .LocationID_FK = CLng(comBarCtl.Tag)
    .Refresh
    .Requery
  End With
End Sub

Following the Code
1. You create a new command bar if it does not exist. In my case I delete comBarLocations each time, because I want to make sure it is current as I add new data
Set comBarLocations = CommandBars.Add(comBarName, msoBarPopup, False, False)
2. Each item on a command bar is a command bar control. At the first level (Country) you add a command bar control to the command bar. At the other levels you add the command bar control to its parent command bar control
3. Command bar control can have an OnAction which is the name of a procedure to run if it is selected.
comBarControl_Lvl_3.OnAction = "SelectLocation"

4. The procedure SelectLocation is a little tricky how it works
The on Action calls the SelectLocation method. The method then figures out which command bar control called it by this line
Set comBarCtl = CommandBars.ActionControl

To associate a control with the commandbar there is a property on the "Other" Tab called "Short Cut Menu Bar". Need to put the name of the command bar in there. In step 1 above, I called my command bar "comBarLocations"

To get the command bar to popup on a event like a click event you need to add code to show popup
Code:
Private Sub cmdSelect_Click()
  If isCommandBar("comBarLocations") Then
    CommandBars("comBarLocations").ShowPopup
  Else
    MsgBox "Command Bar 'comBarLocations' does not exist.  See administrator"
  End If
End Sub
 

Attachments

  • LocationsComBar.accdb
    3.5 MB · Views: 351

KitaYama

Well-known member
Local time
Tomorrow, 07:57
Joined
Jan 6, 2022
Messages
1,540
I saw this solution in one of your posts a while back. and actually used it in one of my databases.
But after a while I switched back to cascading combo boxes.

Mostly because of visual appearance. Combo boxes stay on the input/search form and show what have been selected, but using command bars....well....once you select them, they're gone (hidden).
One more reason to switch back was the length of recordset. Your above images contain only few items, but when the items count grow, I had to move up and down, to find what I need. Just imagine three long command bars one after another that fill the height of the screen.
The third reason was combo boxes are lookup lists. I mean I could start typing instead of selecting.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:57
Joined
May 21, 2018
Messages
8,525
I saw this slution in one of your posts a while back. and actually used it in one of my databases.
But after a while I switched back to cascading combo boxes.
Mostly because of visual appearence. Combo boxes stay on the input/search form and show what have been selected, but using command bars....well....once you select them, they're gone (hidden).
One more reason to switch back was the length of recordset. Your above images contain only few items, but when the items count grow, I had to move up and down, to find what I need.
The third reason was combo boxes are lookup lists. I mean I could start typing instead of selecting.
Yes, those are really good reasons for picking one solution over another for a given situation. Especially record counts. Yes like anything, this would be another arrow in the quiver not an overall replacement. This would not be good for large record counts per level, but would be more useful if you have lots of levels. Even more useful if you have varying amount of levels. For example you can pick a component. Some are sub systems, some are sub sub systems.... That gives me an idea to demo, because this would be good for self referencing hierarchical data. I would normally build a treeview, but there may be some application here.
However, as for showing what has been selected per level, my example purposely displays the country, city, and location.. You could do that on a search form. However, what you cannot do is pick a country and filter the list. Then the city and filter. You can only do actions on the last selection.
 

SHANEMAC51

Active member
Local time
Tomorrow, 01:57
Joined
Jan 28, 2022
Messages
310
One more reason to switch back was the length of recordset. Your above images contain only few items, but when the items count grow, I had to move up and down, to find what I need. Just imagine three long command bars one after another that fill the height of the screen.
therefore, in such cases, I do not use a standard formulation, but a subordinate form with an independent search for any part of the name

otherwise, how to find a capacitor of size 15 out of 1500 candidates in different categories or a loop from 100 to 120 out of 970 possible

the dimensions were not placed in separate fields, so it was necessary to look in a long, poorly filled name - a very real situation

often the user does not know where to look for the required information even with structured stuffing

therefore, only dynamic search is applicable - search by known fragments group + subgroup+ names
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:57
Joined
Feb 19, 2002
Messages
43,233
@MajP Nice example.
Interesting sample set. Salmiya is where all the expats lived. Not us, we lived for a short time in Sulibakart but moved to Safat and lived there for most of our stay. You could see the Emir's palace from our back windows and from my daughter's window, you could see the Kuwait towers. The round ones with the round blue tiles. I don't know what they're called today.
T01_KuwaitTowers.jpg
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:57
Joined
May 7, 2009
Messages
19,229
Kuwait, city of prostis.
been in mabullah doing proj in Kuwait National Power Corp, Mina Al-hamadi.
visited, Maliya and sabah-al-salem, al faneel, etc.
 

Users who are viewing this thread

Top Bottom