Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Well im trying to make a program just to add a record into the msaccess db,
and I found myself having problems with the INSERT into syntax error (Syntax error in INSERT INTO statement."

I would greatly appreciate if someone helps me with this code. Thnx.
I always get the error in the cmd.ExecuteNonQuery() part.

VB
Dim cmd As New OleDb.OleDbCommand
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As StringDim ds As New DataSet


        If Not con.State = ConnectionState.Open Then
            con.Open()

        End If
        cmd.Connection = con
        cmd.CommandText = "INSERT INTO bene_records(f_name, m_name, l_name, b_date, add, home_num, cell_num)VALUES(@F_nameTextBox, @M_nameTextBox, @L_nameTextBox, @B_dateDateTimePicker, @AddTextBox, @Home_numTextBox, @Cell_numTextBox)"
        cmd.Parameters.AddWithValue("@f_name", F_nameTextBox.Text)
        cmd.Parameters.AddWithValue("@m_name", M_nameTextBox.Text)
        cmd.Parameters.AddWithValue("@l_name", L_nameTextBox.Text)
        cmd.Parameters.AddWithValue("@b_date", B_dateDateTimePicker)
        cmd.Parameters.AddWithValue("@add", AddTextBox.Text)
        cmd.Parameters.AddWithValue("@home_num", Home_numTextBox.Text)
        cmd.Parameters.AddWithValue("@cell_num", Cell_numTextBox.Text)


        cmd.ExecuteNonQuery()

        

        con.Close()
Posted

1 solution

You need to tie the Parameters to the parameter names in your CommandText, not to the field names with an '@' in front!
Your code:
VB
cmd.CommandText = "INSERT INTO bene_records(f_name, m_name, l_name, b_date, add, home_num, cell_num)VALUES(@F_nameTextBox, @M_nameTextBox, @L_nameTextBox, @B_dateDateTimePicker, @AddTextBox, @Home_numTextBox, @Cell_numTextBox)"
cmd.Parameters.AddWithValue("@f_name", F_nameTextBox.Text)
...
Needs to be:
VB
cmd.CommandText = "INSERT INTO bene_records(f_name, m_name, l_name, b_date, add, home_num, cell_num)VALUES(@F_nameTextBox, @M_nameTextBox, @L_nameTextBox, @B_dateDateTimePicker, @AddTextBox, @Home_numTextBox, @Cell_numTextBox)"
cmd.Parameters.AddWithValue("@F_nameTextBox", F_nameTextBox.Text)
...
 
Share this answer
 
Comments
[no name] 30-Oct-14 4:57am    
Just what i was about to write :)
I agree with Griff

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900