Consulting

Results 1 to 5 of 5

Thread: How to search between two or multiple fields in Combo box

  1. #1
    VBAX Regular
    Joined
    Jun 2020
    Posts
    15
    Location

    How to search between two or multiple fields in Combo box

    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!
    ComboBox search-.jpg

    Greg
    Attached Files Attached Files

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    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/art...-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.

  3. #3
    VBAX Regular
    Joined
    Jun 2020
    Posts
    15
    Location
    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

  4. #4
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    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.

  5. #5
    VBAX Regular
    Joined
    Jun 2020
    Posts
    15
    Location
    Thanks for the help. it worked! I even put the code in both fields so that it works both way

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •