0
votes

I have 2 combo boxes on a Form. ComboID and ComboName

ComboID contains items such as 1001, 1002, 1003, 1004 etc... ComboName contains items such as John, Matt, David, Luke etc...

I have a table with this data: 1001, John 1002, Matt 1003, David 1004, Luke

I want ComboID to display 1001 when I select John from ComboName. And I also want ComboName to display '1002' when I select Matt.

I need to be able to select a new Name or ID at any point and have its corresponding value update in the other combo-box.

I'm stuck with trying to get this to work and it can't be as hard as I'm making it out to be. I am still learning VBA code. Any help please?

2

2 Answers

4
votes

First Glance Solution (not recommended)

What it Was

My initial reaction to this problem was to try and use a change event. Setting up subroutines for each combobox to change the value of the other combobox would have then been the solution.

Why you shouldn't use it

However, in the documentation for the Access vba change events (link above) it clearly states:

"Avoid creating two or more controls having Change events that affect each other — for example, two text boxes that update each other" (msdn access vba change event)

Unfortunately, this is exactly what you would be doing using the change event solution (using comboboxes instead of text boxes). In your example, a change in the ComboID combobox would trigger the appropriate change in the ComboName combobox, but that change in the ComboName combobox would trigger an attempted change in the ComboID, and this loop could continue on. Therefore, this is not a good answer to your problem.

A Better Solution

An AfterUpdate Event appears to be the more appropriate approach to use in this case.

Why the problematic loop won't be triggered

As mentioned in the AfterUpdate documentation:

Changing data in a control by using Visual Basic or a macro containing the SetValue action doesn't trigger these events for the control.

Therefore, an AfterUpdate Event (possibly using a SetValue) should be a much better approach.

Much thanks to @Remou for pointing out the error of my change event ways and bringing the AfterUpdate event to my attention.

2
votes

A change event is very rarely a suitable choice for code. For example, a combobox allows the user to type as well as to select items, so the change event will fire for every single character typed. As will any events that are chained to that event, leading to the possibility of horrible complications.

Set up the combo boxes like so, thought it is a bit of a mystery as to why you wish to do this. You may need to keep them in synch in the current event. You do not mention if the combos are bound to a Control Source, so I have not included one.

ComboID

RowSource: SELECT ID, [Name] FROM Table ORDER BY ID
BoundColumn: 1 
ColumnCount: 2
ColumnWidths: 2cm;0cm ''The second column is hidden,
                      ''the first column is any width

Private Sub ComboID_AfterUpdate()
    Me.ComboName = Me.ComboID
End Sub

ComboName

RowSource: SELECT ID, [Name] FROM Table ORDER BY [Name]
BoundColumn: 1
ColumnCount: 2
ColumnWidths: 0cm;2cm ''The first column is hidden, 
                      ''the second column is any width

Private Sub ComboName_AfterUpdate()
    Me.ComboID = Me.ComboName
End Sub