0
votes

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

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. But I got Error-message 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. So In my case the line ("myForm.RecordSource = newRecordsource") will need about 13 seconds to execute. 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.

2
The normal method is to initially load the form recordset with a "blank" record, something like SELECT * from YourTable WHERE PkID = 0. Then apply your filtering to the recordset you want to load, after the user sets some criteria on the form in question - Minty
oh, i forgot to mention that Im working with a Continuous Form - Gener4tor

2 Answers

1
votes

This can't be done.

Access requeries any form as soon as you change the record source. 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)

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
1
votes

Actually, there are several ways to do this.

First up, you can (and should) simply launch the form using a “where” clause. This will filter the form. So in effect, don’t use the forms “filter”, but use the “where” clause when you open the form.

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. 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.