Subform filtering based off multiple parameters (Combobox AND Textbox)

可紊 提交于 2019-12-13 03:56:09

问题


I want to filter a subform based on two parameters on a form (combobox and textbox).

I have a Form with combobox cboTimePeriod which show data from table TimePeriod (example TimePeriod=”10.01.2018-10.02.2018”; ID=12).

Combobox data:

Data Row source= SELECT [tblTimePeriod].[TimePeriod], [tblTimePeriod].[ID] FROM tblTimePeriod ORDER BY [TimePeriod];

Data Bound Column=2 (bound to ID)

Format Column Count=1 (shows textual value of TimePeriod) Format List Width=2,54cm

Also I have some cbobuttons with Cities, so when I press a button named "Boston", TextBox txtCity shows i.e. “Boston”.

What I want is when ever I select time period (cboTimePeriod), result in a subform has to be filtered based on these two parameters (selected TimePeriod AND City in textbox).

And as you might guessed, it does not work.

I tried several codes, here is the one I've used:

'How do I filter an Access subform with multiple combo boxes in the form?

And this is my implementation, which does not work:

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 gives this: strWhere = "[TimePreriodID] = '12 ' AND [CityName] = 'Boston '"

After aplying a filter, on a subform "Unfiltered" changes to "Filtered", but with no change in data.

Any help is appreciated.


回答1:


I guess TimePeriod is numeric, thus (and correction the spaces):

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

and:

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

Edit: To look up another column, here the second column:

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


来源:https://stackoverflow.com/questions/48826413/subform-filtering-based-off-multiple-parameters-combobox-and-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!