Query timeout


I have a sql query that takes 2 minutes to run. It runs fine but when I use it in VBA it gives me a query timeout error. Is there a setting that I need to change or include some code in vba.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kelvin SparksCommented:
I'm guessing that your VBA is using an ODBC connection. Br default this has a timeout of 60 seconds. Generally, you can alter this in the registry to a higher value. I'm not sure if you're using Excel or Access to run this against SQL Server

camper12Author Commented:
I am using excel.
1 - You can use the CommandTimeout property (as shown below, adopted from this site) on the Command object. You mention that you are using VBA, so am assuming ADODB here. Excel itself has a fairly large timeout value (e.g. when refreshing a pivot table or data table linked to a dataset)
     Set cmdByRoyalty = New ADODB.Command
    Set cmdByRoyalty.ActiveConnection = Cnxn
    ' Set the criteria
    strSQLByRoyalty = "byroyalty"
    cmdByRoyalty.CommandText = strSQLByRoyalty
    cmdByRoyalty.CommandType = adCmdStoredProc
    cmdByRoyalty.CommandTimeout = 15

Open in new window

2 - Two minutes is quite long :( :( :( ... your Excel GUI will likely be in a "Not Responding"state waiting for this to finish... is there really nothing that can be done to optimise this SQL and make it run faster?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelvin SparksCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.