Passing 4 variables into SQL Query from Excel using VBA


Here is my situation: I have an Excel 2010 Worksheet with a connection and a query to a SQL 2008 DB. There is a command button to execute the query, and 4 textboxes to pass values from. My VBA code for the button is here:
Private Sub QueryBtn_Click()
    QueryBtn.BackColor = &H8080FF
    Dim varStartDate As Date
    Dim varEndDate As Date
    Dim varMake As String
    Dim varModel As String
    varStartDate = StartDate.Value
    varEndDate = EndDate.Value
    varMake = Make.Value
    varModel = Model.Value

'    This next line is where it fails
    Selection.QueryTable.Refresh BackgroundQuery:=False
    QueryBtn.BackColor = &H8000000F
End Sub

Open in new window

In Debug, I can see the parameters are populating from the worksheet. If I omit the parameters then the ' Selection.QueryTable.Refresh' line runs fine.
What is the correct command to pass them to the query?

Thanks in advance
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.

what error gets generated?
hhnetworksAuthor Commented:
Sorry, should have put that in there...'Runtime error 1004 SQL Syntax error'

My initial SQL query starts out like this:
    varStartDate = ?
    varEndDate = ?    
    varMake = ?
    varModel = ?

    vhs.BR AS "Location",
    vhs.TYPE AS "N/U",
    vhs.NO AS "Stock Unit #",
    vhs.MAKE AS "Make",

etc etc

Open in new window

could it be a data type issue? try commenting out the dates because sql expects a ' to show date types
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

hhnetworksAuthor Commented:
Commented out Dates, now get a 1004 Application-defined or object defined error..

What would the correct syntax be to pass the Date values?
you would need to concatenate a single quote on each end and convert the date into a Sql friendly format like 'YYYY-MM-DD HH:MM:SS.mmm'

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
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
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
Visual Basic Classic

From novice to tech pro — start learning today.