access vba - Linking 2 combo boxes -
i know best way create combo box linked selection of combo box. example, in combo box number 1 selects 'fruits', options in combo box number 2 mango, orange , kiwi.. when user selects in combo box number 1 'vegetables', in combo box number 2 options carrot, artichoke, , tomato. both combo boxes should linked same table called produce.
i don't have trouble building query support combo box number one, don't understand how can link selected query supporting combo box number 2.
there several ways how this, easiest 1 , 1 use setting different row source
combobox2 when change
event (or afterupdate
, depending on needs) of combobox1 fired.
example:
i have 2 tables
animals 1 dog 2 cat 3 mouse 4 rabbit cars 1 audi 2 bmw 3 ferrari 4 porsche 5 mclaren
on form have 2 comboboxes, second 1 based on selection of first one, contains 2 options: animals, cars.
sample code:
private sub combo1_change() dim cmb1 combobox: set cmb1 = me.combo1 dim cmb2 combobox: set cmb2 = me.combo2 select case cmb1.value case "animals" cmb2.rowsource = "animals" ' table name animals case "cars" cmb2.rowsource = "select top 3 * cars" ' sql command table cars case else cmb2.rowsource = "animals" end select end sub
now each time value in combo1 changes, rowsource of combo2.
note: need set default rowsource of combo2 based on value in combo1 on form load, combo2 not empty on start.
Comments
Post a Comment