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?
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:
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:
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.