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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing values using SQL INSERT statement in VB 1

Status
Not open for further replies.

treyball3

Programmer
Jun 20, 2001
74
0
0
US
I'm trying to write to an access database through a VB program. The user types in values and I want to write these to the database using the INSERT statement. This is what I have:

Set gdbCurrent = _
OpenDatabase(App.Path & "\Recipes.mdb")

pstrSQL = "INSERT INTO tblRecipes(fldName, fldFoodType, fldCookIn, fldTime, fldIngredients, fldInstructions) " & _
"VALUES('txtName.Text', 'cboFoodType.Text', 'cboCookIn.Text', 'cboTime.Text', 'txtIngredients.Text', 'txtInstructions.Text')"

gdbCurrent.Execute pstrSQL

That code just passes what is in the quotes, ie: txtName.Text is written to the database. I also tried it without the single quotes and then I got an error: "Too few arguements."

Is there anyway to pass the values to the database????
 
Without the single quote, but converting the values to appropriate datatypes that match the datatype of the columns in the database, maybe?


Min.
 
Try following:

pstrSQL = "INSERT INTO tblRecipes(fldName, fldFoodType, fldCookIn, fldTime, fldIngredients, fldInstructions) " & _
"VALUES("'" & txtName.Text & "', '" & cboFoodType.Text & "', '" & cboCookIn.Text & "', '" & cboTime.Text & "', '" & txtIngredients.Text & "', '" & txtInstructions.Text & "')"

I assume that all the values that are passed are Strings. If you have to pass numeric values, then do not put "'" before and after the value.
Also use
Debug.Print pstrSQL to view the SQL that U'r passing
 
Thank you for your help. I hadn't used VB in awhile, so I forgot that I would need the extra quotes like that. It works perfectly now. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top