Passing 4 variables into SQL Query from Excel using VBA

Experts,

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
    Range("A8").Select
    
    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
    
    Range("A2").Select
    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
hhnetworksAsked:
Who is Participating?
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.

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

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

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

etc etc
etc

Open in new window

0
regmigrantCommented:
could it be a data type issue? try commenting out the dates because sql expects a ' to show date types
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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?
0
regmigrantCommented:
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'
0

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.