簡體   English   中英

在ms-access中設置Recordsource之前設置過濾器?

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

我正在處理VBA舊版應用程序中的性能問題-出於任何我不知道的原因,它會通過以下方式設置連續Form的記錄源

myForm.RecordSource = newRecordsource

表單已經打開后。 單擊按鈕后應用過濾器:

DoCmd.ApplyFilter , "my filter sql"

我想過在設置RecordSource之前先設置默認過濾器,這樣可以更快地顯示表單。 但是我收到了錯誤消息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.

所以我必須設置過濾器!之后! 記錄源已設置。 但是,當我設置RecordSource時,我的應用正在發送查詢。 因此,在我的情況下,該行(“ myForm.RecordSource = newRecordsource”)將需要大約13秒的時間來執行。 之后再設置過濾器將導致更多的等待時間。

有沒有一種方法可以阻止表單在應用過濾器之前加載所有數據集? 當整個應用程序(和其他幾個應用程序)按照說明運行時,我不能只在RecordSource中更改查詢或將其設置為設計模式。

無法做到這一點。

更改記錄源后,Access會以任何形式查詢。 即使您在分配記錄源之前設置過濾器,更改記錄源后該過濾器也會消失。

相反,如果過濾器是靜態的,請調整記錄源以合並過濾器條件。

示例(在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

實際上,有幾種方法可以做到這一點。

首先,您可以(並且應該)使用“ where”子句簡單地啟動表單。 這將過濾表單。 因此,實際上,不要使用表單“過濾器”,而是在打開表單時使用“ where”子句。

如果在加載表單后有一些文本框和按鈕可以“過濾”,則將表單記錄源保留為空白,然后說出城市過濾器:

Dim strSQL     as string

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

me.RecordSource = strSQL

因此,以上就是您所需要的一切–只需刪除表單記錄源,然后獲取條件,然后按照上述方法設置表單記錄源。

如前所述,您還可以在打開表單之前提示/獲取該條件,並且帶有“ where”子句的打開表單將針對與表單綁定的數據源起作用,並且過濾器只會出現一次,並且該表單只會提取與您在open form命令中提供的where子句匹配的數據。 這種方法消除了即時設置表單記錄源的需要。

兩種方法都將是數據的一次性過濾器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM