VB 6.0 - Syntax error in INSERT INTO statement (Run-time error '3134':

Asked By Benedict Silla on 17-Oct-09 07:23 AM
Private Sub Command2_Click()
    If Trim(gStatus) = "ADD" Then
        Data1.Database.Execute "Insert into BIODATAS (Reg_No,Name_Of_Husband,Age,Parents,Born_in,Now_Residing_In,Name_Of_Wife,Age_Of_Wife,Parents_Of_Wife,BPlace,Now_Leaving_in,Solemnized,Date,Witness,DateIssued,Vicar) values ('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "','" & Text4.Text & "','" & text5.Text & "','" & Text6.Text & "','" & Text8.Text & "','" & Text9.Text & "','" & Text10.Text & "','" & Text11.Text & "','" & Text12.Text & "','" & Text13.Text & "','" & Text15.Text & "','" & Text16.Text & "','" & Text17.Text & "','" & Text7.Text & "')"
        Data1.Refresh
        Data1.Recordset.MoveLast
        '--------------
        Command3.Enabled = True
        Command2.Enabled = False
        Command1.Enabled = True
    ElseIf Trim(gStatus) = "EDIT" Then
        Data1.Database.Execute "Update BIODATAS set Reg_No='" & Text1.Text & "',Name_Of_Husband='" & Text2.Text & "',Age='" & Text3.Text & "',Parents='" & Text4.Text & "',Born_in='" & text5.Text & "',Now_Residing_In='" & Text6.Text & "',Name_Of_Wife='" & Text7.Text & "',Age_Of_Wife='" & Text8.Text & "',Parents_Of_Wife='" & Text9.Text & "',BPlace='" & Text10.Text & "',Now_Leaving_in='" & Text11.Text & "',Solemnized='" & Text12.Text & "',Date='" & Text13.Text & "',Witness='" & Text15.Text & "' ,DateIssued='" & Text16.Text & "',Vicar='" & Text17.Text & "'where Reg_No='" & Text0.Text & "'"
        Data1.Refresh
Jonathan VH replied to Benedict Silla on 17-Oct-09 10:31 AM

It sounds like that error is coming from the database rather than VB. Why not try temporarily replacing the execute method with Debug.Print and then copying the resultant string into whatever utility your database provides for directly executing queries?

Debug.Print "Insert into BIODATAS (Reg_No,Name_Of_Husband,Age,Parents,Born_in,Now_Residing_In,Name_Of_Wife,Age_Of_Wife,Parents_Of_Wife,BPlace,Now_Leaving_in,Solemnized,Date,Witness,DateIssued,Vicar) values ('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "','" & Text4.Text & "','" & text5.Text & "','" & Text6.Text & "','" & Text8.Text & "','" & Text9.Text & "','" & Text10.Text & "','" & Text11.Text & "','" & Text12.Text & "','" & Text13.Text & "','" & Text15.Text & "','" & Text16.Text & "','" & Text17.Text & "','" & Text7.Text & "')"

That may provide you a more detailed error message. (I suspect that the table's columns' data types are not all strings, and so having single quotes around all the values in the list is wrong.) Edit the query in the database utility until it works, and then you'll know what to change in your VB code.

One note of concern: Data type mismatch? - [)ia6l0 iii replied to Benedict Silla on 17-Oct-09 12:41 PM

The field "Name_Of_Wife" in your INSERT query takes the value  Text8.Text and the field "Vicar" takes field Text7.Text
The field "Name_Of_Wife" in your UPDATE query takes the value Text7.Text and thee field "Vicar" takes field Text17.Text. 

I feel this could also be sending wrong fields and an error due to the data type mismatch error