SQL String variable within another String Variable

S

Steve S

How do I code the following

strInput = "a test"
MsgBox (strInput)
strSQL = "INSERT INTO Comments (Comment) SELECT " & strInput & "as X;"
MsgBox (strSQL)
DoCmd.RunSQL strSQL

so the variable - strInput - is resolved to produce this SQL statement:

INSERT INTO Comments (Comment) SELECT " Steve " as X;

the last line works fine when I hard code the text I want. I just can't get
the variable tobe resolved correctly.

Steve
 
R

RoyVidar

Steve S said:
How do I code the following

strInput = "a test"
MsgBox (strInput)
strSQL = "INSERT INTO Comments (Comment) SELECT " & strInput & "as
X;" MsgBox (strSQL)
DoCmd.RunSQL strSQL

so the variable - strInput - is resolved to produce this SQL
statement:

INSERT INTO Comments (Comment) SELECT " Steve " as X;

the last line works fine when I hard code the text I want. I just
can't get the variable tobe resolved correctly.

Steve

Text will need tex delimiters (single quotes), try

strSQL = "INSERT INTO Comments (Comment) Values ('" & _
strInput & "')"

If there's a possibility that the text could contain single quotes, or
other special characters, you might try "doublequoting"

strSQL = "INSERT INTO Comments (Comment) Values (""" & _
strInput & """)"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top