Pass Variable to SQL Parameter Query in VBA

Can you pass the value (text) from an InputBox directly to a strSQL query to build a parameter query? I have tried several ways to do this but, to no avail.

Function Pass_String()

Dim strSQL As String
Dim strPrompt As String
Dim strTitle As String
Dim strUserInput As String
Dim strParameter As String

strPrompt = "Enter Customer Name"
strTitle = "Enter Data"

strUserInput = InputBox(Prompt, Title)
strParameter = strUserInput

If strUserInput = "" Then
    Call Get_TECP
        If UserInput <> "" Then
        Call Get_SVPT
    End If
End If
End Function

Function Get_TECP()

strSQL = "SELECT  tblMasterData.[Customer], tblMasterData.[Customer Name], tblMasterData.[Customer Parent]"
strSQL = strSQL & " FROM tblMasterData WHERE tblMasterData.[Customer Parent]="'& strParameter'"&"

DoCmd.RunSQL strSQL

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

Patrick MatthewsCommented:
Hi KFFrench,

strSQL = "SELECT  tblMasterData.[Customer], tblMasterData.[Customer Name], tblMasterData.[Customer Parent]"
strSQL = strSQL & " FROM tblMasterData WHERE tblMasterData.[Customer Parent]='" & strParameter & "'"

YOu were mixing up your single- and double-quotes.


Leigh PurvisDatabase DeveloperCommented:
You can create QueryDef objects in DAO code and pass the parameters in there.
(Not difficult - bound to be loads of examples on EE)

Another way might be to use functions in your query as criteria.
The functions simply retrieve that value of a public variable.
So you'd just set the public variable values.
Then fire your queries.

As someone I know once said...


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
Hi KFFrench,

I'm sure your SQL string is fine.

But you can't run a Select query like this.  
You must either open a recordset based on your string or save you string to a saved query and run that.

Patrick MatthewsCommented:

> But you can't run a Select query like this.  

That loud sound you may have just heard was me banging my head into the wall for forgetting something so elementary :)


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 Access

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.