Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL INSERT

Status
Not open for further replies.

gustaf111

Programmer
Jan 16, 2011
87
SE
Hello,

How should I write the SQL string to insert a string into a database ? I write this statement to insert values:

strSQL = "INSERT INTO Table1 (Test) VALUES (" & 132123 & ");"

Gustaf
 
If you want to insert a hardcoded string, use this:
Code:
strSQL = "INSERT INTO Table1 (Test) VALUES ('132123');"

If your string is stored in a variable, use this:
Code:
MyVar = "132123"
strSQL = "INSERT INTO Table1 (Test) VALUES ('" & MyVar & "');"

Well, actually no... if your variable has single quotes, your SQL statement will be messed up (Google "SQL Injection")... you should replace single quotes with two single quotes like this:
Code:
MyVar = "132123"
strSQL = "INSERT INTO Table1 (Test) VALUES ('" & Replace(MyVar, "'", "''") & "');"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top