PDA

View Full Version : How to search between two or multiple fields in Combo box



Grega
06-20-2020, 08:03 AM
Hello,
I need to know some VBA code that will allow me to search or switch between 2 fields inside one Combo box.
Below I have attached example file to help explain my question. In Combo box of a Form I am wanting to search and put a city code direct in field cityCode OR by city Name, so not just by one field citycode. I’m sure this is possible, but I have no idea how to do it?
I'm a VBA newbie any written code in a given example would be appreciated.

Thank you!
26848

Greg

OBP
06-20-2020, 12:56 PM
Unfortunately as far as I know VBA code does not control the search that is built in to Combo boxes when you type in the data.
You have to replace this with VBA code actually switches it off and replaces it with a VBA search.
There is this which you may be able to modify.
https://www.experts-exchange.com/articles/6490/Search-While-You-Type-in-a-ComboBox.html

However I would tend to keep it simple by either having 2 combos, one for Zips & one for Cities or have an option group to select what you want to search by and only make that combo visible.
The problem being that combos work better when they are in alphabetic or numeric order and your combo can't be both, unless you change it's recordset based on the option group or the first character that you type in.

Grega
06-20-2020, 01:48 PM
If I had two combos, is it possible that in case the user does not know the Zip code by heart, he leaves this field empty and then selects the name of the City in the second combo box for (Cities)? However, in this case, the code in the first combo would be written automatically from the City field, ist this possible? I don’t know if you understood, what I mean. Anyway I’ll try to do according to your idea and let you know later.
a

OBP
06-21-2020, 01:05 AM
Yes you need to include the key field (set to 0cm width) for the Zip code/City table and then when you select the City use
Me.CityCode = me.whatever the name of the city combo is.

Grega
06-25-2020, 04:31 AM
Thanks for the help. it worked! I even put the code in both fields so that it works both way