What's wrong with this INSERT syntax???

puuts

New member
Local time
Today, 02:05
Joined
Jan 20, 2014
Messages
8
Hello,

I'm receiving an error "Syntax error in INSERT INTO statement" for the below code. I can't figure it out where/what is the error. Please help.

Code:
CurrentDb.Execute "INSERT INTO tblTrans(fdsname, project, so) " & _
                " VALUES (" & txt_fds & " ,'" & txt_name & "','" & txt_so & "')"
 
My guess is txt_fds doesn't contain numeric data, but you are inserting it like it does. Of course, pbaldy has the best solution--find out exactly what that SQL is, don't just guess by looking at your code.
 
Hello pbaldy,

Thanks for the reply. I've tried your suggestion but it seems it didn't save in the database? Or do I missed something??


Code:
Private Sub cmdSAVE_Click()
 
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb()
strSQL = "INSERT INTO tblTrans(fdsname, project, so) " & _
                " VALUES(" & txt_fds & ",'" & txt_name & "','" & txt_so & "')"
                Debug.Print strSQL
                
MsgBox ("Add Successfull!")
End Sub
 
Hello plog,

txt_fds contains names, and I've set the fdsname in the table to text. But it seems it didn't populate in the database
 
The SQL should display in the Immediate window, as shown in the link. You still need to execute the SQL; this is just a debugging technique.
 
If txt_fds doesn't contain numeric data, you need to put single quotes around it like you have done for txt_name and txt_so
 
I've now added the execute SQL command, but everytime it runs, it always prompting "to append". I've just only want to insert the record not to append :(


Code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb()
strSQL = "INSERT INTO tblTrans(fdsname,project,so) " & _
                " VALUES(" & cb_fds & "," & txt_name & "," & txt_so & ")"
                Debug.Print strSQL
 
                DoCmd.RunSQL strSQL
 
What would you consider to be the difference? ;)
 

Users who are viewing this thread

Back
Top Bottom