简体   繁体   English

在ms-access中设置Recordsource之前设置过滤器?

[英]Setting the filter before setting the Recordsource in ms-access?

I'm working on a performance issue in a vba legacy application which - for any reason I dont know - sets the recordsource of a continuous Form via 我正在处理VBA旧版应用程序中的性能问题-出于任何我不知道的原因,它会通过以下方式设置连续Form的记录源

myForm.RecordSource = newRecordsource

after the form is already open. 表单已经打开后。 The filter is applied after clicking a button: 单击按钮后应用过滤器:

DoCmd.ApplyFilter , "my filter sql"

I thought on setting a default-filter before the RecordSource is set, so the form is displayed faster. 我想过在设置RecordSource之前先设置默认过滤器,这样可以更快地显示表单。 But I got Error-message 2491: 但是我收到了错误消息2491:

The action or method is invalid because the form or report isn't bound to a table or query.@You tried to use the ApplyFilter or SearchForRecord action or method. However, the form or report you applied the filter to is not based on a table or query, so the form or report doesn't have any records to apply a filter to.@Use the SelectObject action or method to select the desired form or report before you run the ApplyFilter action. To base a form or report on a table or query, open the form or report in Design view, and enter the table or query name in the RecordSource property.

So I have to set the filter !after! 所以我必须设置过滤器!之后! the RecordSource is set. 记录源已设置。 But at the moment I set the RecordSource, my app is sending the query. 但是,当我设置RecordSource时,我的应用正在发送查询。 So In my case the line ("myForm.RecordSource = newRecordsource") will need about 13 seconds to execute. 因此,在我的情况下,该行(“ myForm.RecordSource = newRecordsource”)将需要大约13秒的时间来执行。 And setting the filter afterwards results in even more time to wait. 之后再设置过滤器将导致更多的等待时间。

Is there a way to prevent the form from loading all datasets until I applied the filter? 有没有一种方法可以阻止表单在应用过滤器之前加载所有数据集? As the whole app (and several others) is working as described, I can't just change the query in the RecordSource or set it in design mode. 当整个应用程序(和其他几个应用程序)按照说明运行时,我不能只在RecordSource中更改查询或将其设置为设计模式。

This can't be done. 无法做到这一点。

Access requeries any form as soon as you change the record source. 更改记录源后,Access会以任何形式查询。 Even if you were to set a filter before assigning a record source, it would be gone as soon as you changed the record source. 即使您在分配记录源之前设置过滤器,更改记录源后该过滤器也会消失。

Instead, adjust your record source to incorporate your filter condition if the filter is static. 相反,如果过滤器是静态的,请调整记录源以合并过滤器条件。

Example (on Northwind.accdb) 示例(在Northwind.accdb上)

DoCmd.OpenForm "Inventory List"
Forms![Inventory List].Filter = "[Product ID] = 5"
Forms![Inventory List].FilterOn = True
Debug.Print Forms![Inventory List].FilterOn 'True
Forms![Inventory List].RecordSource = "Inventory"
Debug.Print Forms![Inventory List].FilterOn 'False, displays all records

Actually, there are several ways to do this. 实际上,有几种方法可以做到这一点。

First up, you can (and should) simply launch the form using a “where” clause. 首先,您可以(并且应该)使用“ where”子句简单地启动表单。 This will filter the form. 这将过滤表单。 So in effect, don't use the forms “filter”, but use the “where” clause when you open the form. 因此,实际上,不要使用表单“过滤器”,而是在打开表单时使用“ where”子句。

If you have some text boxes and buttons to “filter” after the form is loaded, then leave the forms record source blank, and then go say for a city filter: 如果在加载表单后有一些文本框和按钮可以“过滤”,则将表单记录源保留为空白,然后说出城市过滤器:

Dim strSQL     as string

strSQL = "select * from MyTable where city = '" & me.txtCity & "’"

me.RecordSource = strSQL

So above is all you need – simply remove the forms record source, and then get the criteria, and then set the forms record source as per above. 因此,以上就是您所需要的一切–只需删除表单记录源,然后获取条件,然后按照上述方法设置表单记录源。

As noted, you can also prompt/get that criteria BEFORE you open the form, and an open form with “where” clause will work against the forms bound data source, and a filter will only occur one time, and the form will only pull data that matches the where clause you provide in the open form command. 如前所述,您还可以在打开表单之前提示/获取该条件,并且带有“ where”子句的打开表单将针对与表单绑定的数据源起作用,并且过滤器只会出现一次,并且该表单只会提取与您在open form命令中提供的where子句匹配的数据。 This approach eliminates the need to set the forms record source on the fly. 这种方法消除了即时设置表单记录源的需要。

Either approach will be a one-time filter of the data. 两种方法都将是数据的一次性过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM