基于多个参数的子窗体过滤(组合框和文本框) [英] Subform filtering based off multiple parameters (Combobox AND Textbox)

查看:117
本文介绍了基于多个参数的子窗体过滤(组合框和文本框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于表单(组合框和文本框)上的两个参数来过滤子表单.

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

我有一个带有组合框cboTimePeriod的表单,该表单显示表TimePeriod中的数据(示例TimePeriod =" 10.01.2018-10.02.2018"; ID = 12).

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

组合框数据:

数据行来源=选择[tblTimePeriod].[TimePeriod],[tblTimePeriod].[ID]从tblTimePeriod订购[TimePeriod];

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

数据绑定列= 2(绑定到ID)

Data Bound Column=2 (bound to ID)

格式列计数= 1(显示TimePeriod的文本值) 格式清单宽度= 2.54厘米

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

我还有一些带有城市的cbobutton,所以当我按下名为波士顿"的按钮时,TextBox txtCity会显示波士顿".

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

我想要的是,无论何时选择时间段(cboTimePeriod),都必须根据这两个参数(在文本框中选择TimePeriod AND City)过滤子窗体.

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:

'如何过滤表单中具有多个组合框的Access子表单?

这是我的实现,不起作用:

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给出以下内容: strWhere = "[TimePreriodID] = '12 ' AND [CityName] = 'Boston '"

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.

感谢您的帮助.

推荐答案

我猜想TimePeriod是数字,因此(并纠正空格):

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

strWhere = strWhere & "[TimePeriodID] = " & Me!cboTimePeriod.Value & " 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 "

这篇关于基于多个参数的子窗体过滤(组合框和文本框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆