con1.Open()
Dim com2 As New OleDb.OleDbCommand("insert into Department(DNo,DName,Manager,AddressL1,AddressL2,City,Telephone,E-mail)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "')", con1)
com2.ExecuteNonQuery()
MessageBox.Show("Succsessfuly Inserted", "Insert", MessageBoxButtons.OK)
con1.Close()
Me.Hide()
Form2.Show()

it will display the INSERT INTO Syntax error messaage
any one can help me....

Recommended Answers

All 11 Replies

Show the full error message.

What database are you using?

For debugging, create your query in a variable so you can output it to check for errors.

the error like this

Syntax error in INSERT INTO statement.

I'm using MsAcess datbase

Put your query in a variable so you can display it, and then post it here. Without the query there's no telling what the error is.

i'm a beginer i dont'know how to do that can u help me?

Public Class Form5

    Dim con1 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\DELL\Desktop\Peyroll Sytem22\Peyroll Sytem\Peyroll System\bin\Debug\payroll.mdb")
    Dim com1 As OleDb.OleDbCommand
    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form2.Show()
        Me.Hide()
        Exit Sub
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" And TextBox2.Text = "" Then

            MsgBox("Enter Department Infromation", MsgBoxStyle.Information, "Department")
        Else


            If TextBox1.Text = "" Then
                MsgBox("Enter Department ID", MsgBoxStyle.Information, "Department")
            Else

                'If 
                'Else
                'MsgBox("Enter Department ID", MsgBoxStyle.Information, "Department")
                If TextBox2.Text = "" Then
                    MsgBox("Enter Department Name", MsgBoxStyle.Information, "Department")
                Else

                    If TextBox4.Text = "" And TextBox5.Text = "" And TextBox6.Text = "" Then
                        MsgBox("Enter Department Address", MsgBoxStyle.Information, "Department")
                    Else

                        If IsNumeric(TextBox7.Text) = False Then

                            MsgBox("Enter Department Contact No", MsgBoxStyle.Information, "Department")
                        Else

                            If TextBox1.Text <> "" And TextBox2.Text <> "" Then


                                con1.Open()
                                Dim com1 As New OleDb.OleDbCommand("insert into Department(DNo,DName,Manager,AddressL1,AddressL2,City,Telephone,E-mail)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "' )", con1)
                                com1.ExecuteNonQuery()

                                MessageBox.Show("Succsessfuly Inserted", "Insert", MessageBoxButtons.OK)
                                con1.Close()
                                Me.Hide()
                                Form2.Show()

                            End If
                        End If
                    End If
                End If
            End If
        End If
        'End If
    End Sub


End Class

Here is my full coding...

Try this :

Dim com1 As New OleDb.OleDbCommand
com1 = con1.CreateCommand
com1.CommandText = "insert into Department(DNo,DName,Manager,AddressL1,AddressL2,City,Telephone,E-mail)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "' )"

what about the
com1.ExecuteNonQuery() is it want?

i tryed that but again same result

Dim qry As String = "insert into Department(DNo,DName,Manager,AddressL1,AddressL2,City,Telephone,E-mail)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "' )"
Debug.WriteLine(qry)

Then copy the output and post it here. In any case, you should be using parameterized queries. See examples here

I tried the code with SQL Server and it worked fine. I think that there is a different way to connect it with MSAccess.

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.