speed up select query

User avatar
Van
2StarLounger
Posts: 114
Joined: 28 Sep 2010, 21:13
Location: Melbourne, Oz

speed up select query

Post by Van »

hi all,

I have a field in a query (ID_Seen_By_Staff) with a parameter that refers to a text box on a form.

when the query is run with the parameter as: [Forms]![frm_Reports]![text65] , the time taken is very slow (slower than what I would expect).

however, if just replace the parameter with the ID number of the staff member, the query runs in the blink of eye.

I can't seem to work out what I'm doing incorrectly. any help would be much appreciated.

Thanks,
Van :scratch:

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: speed up select query

Post by HansV »

My guess would be that the presence of a parameter prevents the query from being optimized.

I cannot guarantee that it will help, but you could try the following:

1) Open the query in design view.
Click Parameters in the Show/Hide group of the Design tab of the ribbon.
If you haven't done so already, enter [Forms]![frm_Reports]![text65] in the Parameter column, and select the appropriate type in the Data Type column (presumably Long Integer).
Click OK, then save and close the query.

Does that improve performance?

If not, remove the parameter again.

2) Make a copy of the query for if you accidentally mess up the query. That way, you have a backup.
Open the query in design view, then switch to SQL View. You'll see something like

SELECT ...
FROM ...
WHERE [ID_Seen_By_Staff]=[Forms]![frm_Reports]![text65]

Replace the line breaks with spaces, so that the SQL statement is a single line (it might still be displayed across multiple lines):

SELECT ... FROM ... WHERE [ID_Seen_By_Staff]=[Forms]![frm_Reports]![text65]

Also replace all double quotes in the SQL statement, if present, with single quotes (apostrophes).

Copy the modified SQL statement to the clipboard.
Close the query without saving it.

In the On Click event of a command button on the form frm_Reports, change the SQL of the query:

Code: Select all

Private Sub cmdReport_Click()
    Dim strSQL As String
    strSQL = "SELECT ... FROM ... WHERE [ID_Seen_By_Staff]=" & Me.text65
    CurrentDb.QueryDefs("qrySomething").SQL = strSQL
    ' Code that uses the query directly or indirectly goes below
    ' For example, opening a report that uses the query as record source
    ...
End Sub
Substitute the name of the query.
As you can see, the value of the text box has been placed outside the quotes. When you click the button, the SQL of the query uses the value of text65 as a fixed value.

See if this improves performance.

If not, you can use the backup copy to restore the original query.
Best wishes,
Hans

User avatar
Van
2StarLounger
Posts: 114
Joined: 28 Sep 2010, 21:13
Location: Melbourne, Oz

Re: speed up select query

Post by Van »

Hi Hans,

WOW! the first suggestion did the trick! works so fast now I don't have time to even blink!

I'm not sure how typing [Forms]![frm_Reports]![text65] in the Parameter column of Parameters worked, but it sure does.
should I be doing this for all parameters is queries? is this best practice?

thanks again,

Van :cheers:

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: speed up select query

Post by HansV »

It is generally a good idea to specify parameters explicitly in the Parameters dialog.
Best wishes,
Hans

Mark L
3StarLounger
Posts: 331
Joined: 11 Feb 2010, 03:55
Location: Land O Lakes, FL

Re: speed up select query

Post by Mark L »

FWIW, I tend to use the 2nd method Hans suggested. I find it a lot more flexible and easier to manage, especially for queries behind reports.

Actually, I take it 1 step further. I have a table of the SQL statements (the PK is the query name). In the example Hans gave, my saved SQL would be:
SELECT ... FROM ... WHERE 1=1

I then replace 1=1 with whatever I need, even a complex statement involving several fields. Then I update the query and run the report.
Mark Liquorman
Land O Lakes, FL
see my website http://www.liquorman.net for Access Tips and Tricks, and for my Liquorman Utilities.