基于多个参数的子表单过滤(组合框和文本框)

Subform filtering based off multiple parameters (Combobox AND Textbox)

我想根据表单上的两个参数(组合框和文本框)过滤子表单。

我有一个带有组合框 cboTimePeriod 的表单,它显示来自 table 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

此外,我还有一些带有城市的 cbobutton,所以当我按下名为 "Boston" 的按钮时,TextBox txtCity 显示即“波士顿”。

我想要的是当我 select 时间段 (cboTimePeriod) 时,必须根据这两个参数(selected 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 '"

应用过滤器后,子表单上的 "Unfiltered" 更改为 "Filtered",但数据没有变化。

感谢任何帮助。

我猜 TimePeriod 是 numeric,因此(并更正空格):

strWhere = strWhere & "[TimePeriodID] = " & Me!cboTimePeriod.Value & " AND "

和:

strWhere = strWhere & "[CityName] = '" & Trim(Me!txtSelectedCity.Value) & "' AND "

编辑: 要查找另一列,这里是第二列:

strWhere = strWhere & "[CityName] = '" & Trim(Me!txtSelectedCity.Column(1)) & "' AND "