wwwMarvsdotcom
wwwMarvsdotcom

Reputation: 72

INSERT into error: Syntax error and Unknown field

I keep getting this error when inserting new record. I forgot to input the father field in the access database so I insert new row for this field. Then when I run my project, I am getting the error. Here's the INSERT command:

 Private Sub Add1_Click(sender As Object, e As EventArgs) Handles Add1.Click

    Try

        Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
            conn.Open()
            Dim command As New OleDbCommand("insert into employee ([EmployID], Last_Name, First_Name, MI, Bday, Age, Nationality, [Contact_Number], Sex, Address, Department, Position, TIN, SSS, Pagibig, PhilHealth, Termi_Resi, [Date_hired], Rate_Month, [Date_End], Status, Father_Name, Mother_Name, Civil, Spouse, [Number_of_Children], Child1, Child2, Child3, Child4, Child5, Child6, Child7, Child8, Child9) values (@numberemp, @lname, @fnamemi, @bday, @age, @natio, @contact, @gender, @address, @department, @position, @tin, @sss, @pagibig, @phh, @termiresi, @datehired, @rm, @dateTermiResi, @status, @father, @mother, @civil, @spouse, @numberchild, @child1, @child2, @child3, @child4, @child5, @child6, @child7, @child8, @child9)", conn)
            With command.Parameters
                .AddWithValue("@numberemp", numberemp.Text)
                .AddWithValue("@lname", lname.Text)
                .AddWithValue("@fname", fname.Text)
                .AddWithValue("@mi", mi.Text)
                .AddWithValue("@bday", bday.Text)
                .AddWithValue("@age", age.Text)
                .AddWithValue("@natio", natio.Text)
                .AddWithValue("@contact", contact.Text)
                .AddWithValue("@gender", gender.Text)
                .AddWithValue("@address", address.Text)
                .AddWithValue("@department", department.Text)
                .AddWithValue("@position", position.Text)
                .AddWithValue("@tim", tin.Text)
                .AddWithValue("@sss", sss.Text)
                .AddWithValue("@pagibig", pagibig.Text)
                .AddWithValue("@phh", phh.Text)
                .AddWithValue("@termiresi", termiresi.Text)
                .AddWithValue("@datehired", datehired.Text)
                .AddWithValue("@rm", rm.Text)
                .AddWithValue("@dateTermiResi", dateTermiResi.Text)
                .AddWithValue("@status", status.Text)
                .AddWithValue("@father", father.Text)
                .AddWithValue("@mother", mother.Text)
                .AddWithValue("@civil", civil.Text)
                .AddWithValue("@spouse", spouse.Text)
                .AddWithValue("@numberchild", numberchild.Text)
                .AddWithValue("@child1", child1.Text)
                .AddWithValue("@child2", child2.Text)
                .AddWithValue("@child3", child3.Text)
                .AddWithValue("@child4", child4.Text)
                .AddWithValue("@child5", child5.Text)
                .AddWithValue("@child6", child6.Text)
                .AddWithValue("@child7", child7.Text)
                .AddWithValue("@child8", child8.Text)
                .AddWithValue("@child9", child9.Text)
            End With
            command.ExecuteNonQuery()
            MessageBox.Show("Employee's Informations Successfuly Recorded!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            command.Dispose()
            conn.Close()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

Here's my table structure: enter image description here

My error is:

INSERT into statement contains the following unknown field name: 'father'. Make sure you have typed the name correctly, and try the operation again.

When I fixed that, now I keep getting this error:

Syntax error in INSERT INTO statement

Am I missing something?

Upvotes: 1

Views: 287

Answers (1)

user6432984
user6432984

Reputation:

This should correct it. I split @fnamemi into @fname, @mi.


Dim command As New OleDbCommand("INSERT INTO employee ([EmployID], Last_Name, First_Name, MI, Bday, Age, Nationality, [Contact_Number], Sex, Address, Department, Position, TIN, SSS, Pagibig, PhilHealth, Termi_Resi, [Date_hired], Rate_Month, [Date_End], Status, Father_Name, Mother_Name, Civil, Spouse, [Number_of_Children], Child1, Child2, Child3, Child4, Child5, Child6, Child7, Child8, Child9) VALUES (@numberemp, @lname, @fname, @mi, @bday, @age, @natio, @contact, @gender, @address, @department, @position, @tin, @sss, @pagibig, @phh, @termiresi, @datehired, @rm, @dateTermiResi, @status, @father, @mother, @civil, @spouse, @numberchild, @child1, @child2, @child3, @child4, @child5, @child6, @child7, @child8, @child9)", conn)

You have First_Name and MI fields but only one parameter to represent both @fnamemi. You're receiving the error because you have one more field then you have values.


[EmployID]  @numberemp
 Last_Name   @lname
 First_Name  @fnamemi
 MI 
 Bday    @bday
 Age     @age
 Nationality     @natio
 [Contact_Number]    @contact
 Sex     @gender
 Address     @address
 Department  @department
 Position    @position
 TIN     @tin
 SSS     @sss
 Pagibig     @pagibig
 PhilHealth  @phh
 Termi_Resi  @termiresi
 [Date_hired]    @datehired
 Rate_Month  @rm
 [Date_End]  @dateTermiResi
 Status  @status
 Father_Name     @father
 Mother_Name     @mother
 Civil   @civil
 Spouse  @spouse
 [Number_of_Children]    @numberchild
 Child1  @child1
 Child2  @child2
 Child3  @child3
 Child4  @child4
 Child5  @child5
 Child6  @child6
 Child7  @child7
 Child8  @child8
 Child9  @child9

Upvotes: 2

Related Questions