Does anyone know this error :
Syntax error (missing operator) in query expression 'Budy Soleman' <--( this is content of textbox.text)

This is my code in with ms-access

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Call connect()

    Dim simpan As String

    simpan = "insert into mst_karyawan values(" & TextBox8.Text & "," & TextBox2.Text & "," & _
    TextBox1.Text & "," & ComboBox1.SelectedValue.item(0) & "," & ComboBox2.SelectedValue.item(0) & "," & _
    TextBox5.Text & "," & ComboBox3.SelectedValue.item(0) & "," & ComboBox4.SelectedValue.item(0) & "," & _
    ComboBox5.SelectedValue.item(0) & "," & TextBox10.Text & "," & TextBox3.Text & "," & TextBox4.Text & "," & _
    TextBox6.Text & "," & DateTimePicker1.Value & "," & TextBox7.Text & ")"

    Dim CMD As New OleDb.OleDbCommand(simpan, CONN)

    CMD.Parameters.AddWithValue("@NIK", Me.TextBox8.Text)
        CMD.Parameters.AddWithValue("@Nama", Me.TextBox2.Text)
        CMD.Parameters.AddWithValue("@Alamat", Me.TextBox1.Text)
        CMD.Parameters.AddWithValue("@JKid", Me.ComboBox1.SelectedValue.item(1).ToString)
        CMD.Parameters.AddWithValue("@Darahid", Me.ComboBox2.SelectedValue.item(1).ToString)
        CMD.Parameters.AddWithValue("@Didikid", Me.TextBox5.Text)
        CMD.Parameters.AddWithValue("@Statid", Me.ComboBox3.SelectedValue.item(1).ToString)
        CMD.Parameters.AddWithValue("@Agamaid", Me.ComboBox4.SelectedValue.item(1).ToString)
        CMD.Parameters.AddWithValue("@Kdbagian", Me.ComboBox5.SelectedValue.item(1).ToString)
        CMD.Parameters.AddWithValue("@Jabatan", Me.TextBox10.Text)
        CMD.Parameters.AddWithValue("@Tlp", Me.TextBox3.Text)
        CMD.Parameters.AddWithValue("@HP", Me.TextBox4.Text)
        CMD.Parameters.AddWithValue("@Spouse", Me.TextBox6.Text)
        CMD.Parameters.AddWithValue("@Anak", Me.TextBox7.Text)
    CMD.Parameters.AddWithValue("@Tglmasuk", Me.DateTimePicker1.Value.ToShortDateString())

    Call connect()

        CMD.ExecuteNonQuery() **<---- Error message point this step as an error **

End Sub

Recommended Answers

All 4 Replies

There's no way of telling without seeing the query string. Replace

Dim CMD As New OleDb.OleDbCommand(simpan, CONN)


Dim CMD As New OleDb.OleDbCommand(simpan, CONN)

and post the output here. Unless the strings you are concatenating contain "?", none of the parameters will be added. You may want to look at this to see how parameters work.

Dear Reverend Jim,
This is the content of simpan
simpan = "insert into mst_karyawan values(0738,Budy Soleman,Regensi 2,1,1,S1,1,3,9,Manager,1234567,123456789012,Oey Je Ing,5/01/2015 4:16:57 PM,3)"


The reason for the syntax error is that you have not surrounded any of your values with the correct tokens. By that I mean that a string value must be surrounded by an apostraphe, a date (in Access) with a # character and numerics with nothing. So for example you might have something like INSERT INTO mst_karyawan values(0738, 'Budy Soleman')

The next thing to note with your code is that while you are attempting to use parameters, you have no parameter placeholders in your SQL string and therefore the parameters are completely ignored and instead it is the values of your controls that are being appended to the simpan string.

So to fix your problem, change your SQL statement to include parameter placeholders, such as INSERT INTO mst_karyawan values (@NIK, @Nama......)

You should really take a look at the link that Reverend Jim posted as it explains the use of parameterised queries quite well (including the gotcha regarding OleDb parameters and the order in which they are specified) which will help you in the future.


After following those directions, the problem is solved.
Thank you guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.