1

Is their a way to use a declared VBA variable in a SELECT Sql statement as follows (I have a table called myTable and i want to set the referenced field by passing the declared string myVar) : e.g passVar("English")

NB: Am using Microsoft Access

Private Function passVar(myVar as string)

Dim db As Database
Dim qdf As DAO.QueryDef
Dim stmnt As String

Set db = CurrentDb()
Set qdf = db.QueryDefs("SelectSubjectQuery")

stmnt = "SELECT [myTable].[myVar] = " &Chr$(34) & myVar & Chr$(34) & _

         //other SQL statements go here

qdf.SQL = stmnt

End Function

Each time i run the query a dialog box appears asking for the value of mytable.myvar instead of passing myvar i.e Eglish as the referenced field on myTable.

2 Answers 2

3

Yes, build the SQL statement as a string like this:

stmnt = "SELECT " & [myTable] & "." & [myVar] & " = " &Chr$(34) & myVar & Chr$(34) & _

VBA should then fill in the names for MyTable and MyVar when the code is run and the string is built.

Sign up to request clarification or add additional context in comments.

1 Comment

yes I have done it as above but a dialog box appears asking me to enter the parameter value for mytable.English ;please help
2
stmnt = "SELECT [myTable]." & myVar & _

Got it to working by writing as above (no need for the equals sign as the string is passed through the function). Thanks for the insight @rcfmonarch

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.