Why can't I specify criteria for a conditional

2019-07-25 22:50发布

问题:

I have two drop downs, Combo_A and Combo_B. I'm attempting to achieve a "cascading combo box" or "synchronous combo box" where a selection from Combo A (say, a US State) would populate Combo B (say a particular list of cities which belong to said state).

I understand using the VBA code to do this, and the reason you'd prefer to use VBA, but my question is WHY must I use VBA.

Simply making a conditional query with criteria of:

[Forms]![Main_Form]![State_Dropdown]

Doesn't seem to work. There IS a requery (of the City_Dropdown) performed AfterUpdate (of the State_Dropdown). Then I'm asked to provide the values for

Form!Main_Form!State_Dropdown

...after manipulating the State_Dropdown.

Why doesn't this work intuitively? Security? Function?

回答1:

Assuming the controls are on same form, don't need to reference the Forms collection and form name. In ComboB RowSource:
SELECT [City] FROM [Cities] WHERE [State]=[ComboA];

I recommend the requery code be in ComboB GotFocus event.

Be aware cascading comboboxes don't work nice with continuous or datasheet form if ComboB RowSource includes lookup alias.