选中后,ComboBox选项消失

时间:2016-09-30 19:33:54

标签: sql ms-access combobox listbox

我有一个状态的ComboBox,可以在选中时填充城市列表框。

我在ComboBox中使用Union查询添加了一个(全部)选项,该查询成功填充了ListBox中的所有城市,但是在选择后将ComboBox留空。我希望“(全部)”留在ComboBox中。

States ComboBox的行源是:

SELECT tblStatesProvince.ID, tblStatesProvince.LngState
FROM tblStatesProvince
UNION Select Null as AllChoice, "(all)" as Bogus From tblStatesProvince
ORDER BY tblStatesProvince.LngState;

城市列表框的行来源是:

SELECT tblCities.ID, tblCities.City, tblCities.State
FROM tblCities
WHERE (((tblCities.State)=[Forms]![frmCities].[cboSelectState])) OR ((([Forms]![frmCities].[cboSelectState]) Is Null))
ORDER BY tblCities.City;

1 个答案:

答案 0 :(得分:3)

我怀疑在原始NULL语句中使用SELECT是问题的根本原因。 NULL实际上只是值的占位符,不能与(典型的)数据类型(如字符串或数字)相比。 NULL没有数据类型。

我会按如下方式重写两个语句(假设tblStatesProvince.ID是数字),从比较中删除NULL

SELECT tblStatesProvince.ID, tblStatesProvince.LngState
FROM tblStatesProvince
UNION Select -1 as AllChoice, "(all)" as Bogus From tblStatesProvince
ORDER BY tblStatesProvince.LngState;

SELECT tblCities.ID, tblCities.City, tblCities.State
FROM tblCities
WHERE (((tblCities.State)=[Forms]![frmCities].[cboSelectState])) OR ((([Forms]![frmCities].[cboSelectState]) = -1))
ORDER BY tblCities.City;