【发布时间】:2018-07-27 08:11:54
【问题描述】:
我想根据表单上的两个参数(组合框和文本框)过滤子表单。
我有一个带有组合框 cboTimePeriod 的表单,它显示表 TimePeriod 中的数据(例如 TimePeriod=”10.01.2018-10.02.2018”;ID=12)。
组合框数据:
数据行来源= SELECT [tblTimePeriod].[TimePeriod], [tblTimePeriod].[ID] FROM tblTimePeriod ORDER BY [TimePeriod];
数据绑定列=2(绑定到 ID)
Format Column Count=1(显示 TimePeriod 的文本值) 格式列表宽度=2,54cm
我还有一些带有城市的 cbobuttons,所以当我按下一个名为“波士顿”的按钮时,TextBox txtCity 显示即“波士顿”。
我想要的是,当我选择时间段 (cboTimePeriod) 时,必须根据这两个参数过滤子表单(在文本框中选择 TimePeriod AND City)。
正如您可能猜到的那样,它不起作用。
我尝试了几个代码,这是我用过的一个:
'How do I filter an Access subform with multiple combo boxes in the form?
这是我的实现,它不起作用:
Dim strWhere As String
If Nz(Me.cboTimePeriod, "") <> "" Then
strWhere = strWhere & "[TimePeriodID] = '" & Trim(Me. cboTimePeriod) & " ' AND "
End If
If Nz(Me.txtSelectedCity, "") <> "" Then
strWhere = strWhere & "[CityName] = '" & Trim(Me. txtSelectedCity) & " ' AND "
End If
If strWhere <> "" Then
strWhere = Left(strWhere, Len(strWhere) - 5)
Me.qry_SomeData_subform.Form.Filter = strWhere 'after this line, function exits the code
Me.qry_ SomeData_subform.Form.FilterOn = True
Else
Me.qry_ SomeData_subform.Form.Filter = ""
Me.qry_ SomeData_subform.Form.FilterOn = False
End If
strWhere 给出了这个:
strWhere = "[TimePreriodID] = '12 ' AND [CityName] = 'Boston '"
应用过滤器后,子表单上的“未过滤”更改为“已过滤”,但数据没有变化。
感谢任何帮助。
【问题讨论】:
标签: vba ms-access-2010 subform