我如何使用Dlookup for Multiple Criteria创建动态组合框 [英] How do i use Dlookup for Multiple Criteria to create a dynamic Combobox
问题描述
我有两个组合框,来自:txtKategorie和txtTyp.第一个组合框(txtKategorie)的值是固定的!我希望根据用户在第一个组合框中选择的内容来更改第二个组合框(txtTyp)的值.如果用户选择数据记录器",则选择否".第二个组合框应只包含"Base Layer Classic"(基本层经典)和"Base Layer Plus";如图所示.对于加速度计",同样的想法是正确的.
I have two Comboboxes on a from: txtKategorie and txtTyp. The values for the frist Combobox (txtKategorie) are fix! I want the values of the second Combobox (txtTyp) to change according to what the user chooses in the first one. If the user chooses "Datalogger" the second combobox should only contain "Base Layer Classic" and "Base Layer Plus" as can be seen in the image. The same idea is true for "Accelerometer".
我已将代码放入第一个组合框的AfterUpdate事件中:
I've put my code in the AfterUpdate Event of the first Combobox:
If txtKategorie.Value = "Datalogger" And txtTyp.ListCount = 0 Then
i = 1
Do While txtTyp.ListCount < DCount("ID", "tblNomenklatur", "[Kat] = 'K'")
txtTyp.AddItem DLookup("[Typ]", "tblNomenklatur", "[ID] =" & i And "[Kat] = 'K'")
'And "[Kat] = 'K'"
i = i + 1
Loop
当表单仅打开第一个组合框"txtKategorie"时,有价值观.当用户选择数据记录器时,代码检查表中有多少记录的[Kat] ="K".定义"Do While语句"将运行多长时间.然后,"txtTyp.AddItem"-声明应在"txtTyp.AddItem"声明中添加"Base Layer Classic".和"Base Layer Plus";到"txtTyp"组合框.但不幸的是,《守则》确实起作用.包含两个条件的Dlookup-Statement存在问题.如果我删除两个条件之一,则代码可以工作,但显然会给第二个组合框提供错误的结果.如果我这样离开,第二个组合框将保持空白.有人知道我在做什么错吗?
When the form opens only the first Combobox "txtKategorie" has Values. When the user chooses Datalogger the code checks how many records in the table have the [Kat] = "K" to define how long the Do While-Statement will run. Then the "txtTyp.AddItem"-Statement should add "Base Layer Classic" and "Base Layer Plus" to the "txtTyp" Combobox. But unfortunately the Code doenst work. There is a problem with the Dlookup-Statement containing tow criterias. If i remove either one of the two criterias the Code works but delivers wrong results for the second Combobox obviously. If i leave it like this the second Combobox stays empty. Does someone know what im doing wrong?
推荐答案
您可以通过以下代码轻松完成此操作.用表名更改表名.
You can do it easily by below code. Change table name with your table name.
Private Sub txtKategorie_AfterUpdate()
Me.txtTyp.RowSource = "SELECT DISTINCT Table1.Typ FROM Table1 WHERE Table1.Kategorie='" & Me.txtKategorie & "'"
Me.txtTyp.Requery
End Sub
或者您可以从行源查询构建器以图形方式执行此操作.以下是步骤.
Or you can do it graphically from row source query builder. Below are steps.
- 组合框
txtKategorie
已修复.很好! - 对于第二个组合框
txtTyp
,请执行以下步骤. - 选择组合框
txtTyp
.在属性窗口中,选择Row Source
,然后单击查询生成器...
三个小点.查看屏幕截图. - 在查询生成器窗口中,从数据表(如屏幕快照)构建查询,并为
Kategorie
列设置条件为[Forms]![Form1]![txtKategorie]
保存并关闭查询窗口.
- Combobox
txtKategorie
is fix. Fine! - For second combobox
txtTyp
follow the below steps. - Select combobox
txtTyp
. From property windows selectRow Source
then click on query builder...
small three dot. See screenshot. - In query builder window build a query from your data table like screenshot and set criteria for
Kategorie
column is[Forms]![Form1]![txtKategorie]
Save and close the query bulder window.
现在在 Update
事件中的Combobox txtKategorie
中写入以下行以重新查询 txtTyp
.你完成了!
Now for Combobox txtKategorie
in After Update
event write below line to requery txtTyp
. You are done!
Private Sub txtKategorie_AfterUpdate()
Me.txtTyp.Requery
End Sub
这篇关于我如何使用Dlookup for Multiple Criteria创建动态组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!