Problem with Quotes around SQL string Variable

Keith

Registered User.
Local time
Today, 02:17
Joined
May 21, 2000
Messages
129
Trying to update a table using vba. I have a problem on how to get quotes around a string variable in the sql statement. when the sql executes I get a popup asking for parameter for whatever word the variable is. If I enter the word the table is updated.
The sql string is:
Code:
    sql = "UPDATE tblMemberBranch SET tblMemberBranch.Test =  " & res & _
        "  WHERE (([tblMemberBranch].[MemberBranchID]= " & OpenArgs & " ));"
The result in the immediate window is:
Code:
Print OpenArgs = 1
Print res = Chairman
Print sql = UPDATE tblMemberBranch SET tblMemberBranch.Test =  Chairman _
WHERE (([tblMemberBranch].[MemberBranchID]= 1 ));
 
Because the value is text you need single quotes around it.
 
Thanks Paul,
This is the solution I found:
Code:
 sql = "UPDATE tblMemberBranch SET tblMemberBranch.Test =  " & Chr(39) & res & Chr(39) & _
        "  WHERE (([tblMemberBranch].[MemberBranchID]= " & OpenArgs & " ));"
 
Happy to help.
 
Keith: You can also use single quotes inside the string, like this

Code:
sql = "UPDATE tblMemberBranch SET tblMemberBranch.Test =  '" & res & _
"'  WHERE (([tblMemberBranch].[MemberBranchID]= " & OpenArgs & " ));"
 
Thanks Sarah, During the several hours that I was pulling my hair out trying to find a solution I did use single quotes at one time but all that did was comment out the rest of the sql string. I can see the logic behind it now. Nearly three score and ten and still at school. :)
 
Keith,

Just started looking at Access VBA again after a 4 year hiatus and ran into the same problem. Sigh... my hair thanks yours!

Thanks also everyone!
 

Users who are viewing this thread

Back
Top Bottom