Syntax error in string in query expression 'ID = 4

nurana picture nurana · Jan 3, 2013 · Viewed 9.9k times · Source

I am using Visual Basic 2010 express to control a Ms.Access 2007 database. I am struggling in adding a record to the database.

I get the Syntax error in string in query expression 'ID = 4

Here is my code :

 If ComboBox1.Text = "" Or TxtJam.Text = "" Or TxtAudio.Text = "" Then
            MessageBox.Show("Data belum lengkap", "Error", MessageBoxButtons.OK, messageBoxIcon.Error)
        Else

            myqry = "UPDATE TblJadwal SET "
            myqry = myqry + " Hari = '" & ComboBox1.Text & "',"
            myqry = myqry + " Jam = '" & TxtJam.Text & "',"
            myqry = myqry + " Audio = '" & TxtAudio.Text & "'"
            myqry = myqry + "WHERE "

            myqry = myqry + "ID = " & TxtID.Text & "'"
            mycmd = New OleDbCommand(myqry, conn)
            mycmd.ExecuteNonQuery()
            Call Set1()
        End If

and my database

ID , autonumber
Hari, text 
Jadwal, text 
Jam, Text 

Answer

Oded picture Oded · Jan 3, 2013

Here is the problem:

"ID = " & TxtID.Text & "'"

You are adding a ' at the end. Incorrect syntax.

You should either have one at the start as well, or non at all (if ID is numeric).

"ID = '" & TxtID.Text & "'"

Or:

"ID = " & TxtID.Text