INSERT INTO语句中的语法错误。 vb(Microsoft Access)

时间:2014-03-29 10:37:17

标签: database vb.net

我正在尝试将记录添加到我的访问数据库中,并且一直收到错误消息。我对VB很新,我不知道我哪里出错了,可能是一件非常简单但我似乎无法弄清楚的事情。谢谢。

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
       Dim dbprovider = "PROVIDER = Microsoft.ACE.OLEDB.12.0;"
        Dim dbsource = "Data Source = c:\users\oliverwilson118\documents\visual studio 2013\Projects\Enrolment_Tracker_OW\Enrolment_Tracker_OW\EnrolmentDatabase.accdb"
        con = New OleDb.OleDbConnection(dbprovider & dbsource)



        Dim sql = "INSERT INTO Applicants ([Course ID], [fName], [lName], [Gender], [DOB], [Address Line 1], [Address Line 2], [Town or City], [Postal Code], [Email], [Telephone], [Mobile], [Nat Insurence Num], [Employment Status], [Emergency Contact fName], [Emergency Contact lName], [Emergency Contact Telephone Num], [Relationship with Applicant]) Values ('" & Course_ID_Combobox.Text & "','" & First_NameTextBox.Text & "', '" & Second_NameTextBox.Text & "','" & GenderTextBox.Text & "','" & DOBDateTimePicker.Text & "','" & Address_Line_1TextBox.Text & "','" & Address_Line_2TextBox.Text & "','" & Town_CityTextBox.Text & "','" & Postal_CodeTextBox.Text & "','" & EmailTextBox.Text & "','" & TelephoneTextBox.Text & "','" & MobileTextBox.Text & "','" & National_Insurence_NoTextBox.Text & "','" & Employment_Status_Combobox.Text & "','" & Emergency_Contact_First_NameTextBox.Text & "','" & Emergency_Contact_Second_NameTextBox

.Text & "','" & Emergency_Contact_Telephone_NumberTextBox.Text & "','" & Relaitonship_to_ApplicantTextBox.Text & "',)"

        Using con = New OleDb.OleDbConnection(dbprovider & dbsource)
            Using cmd = New OleDb.OleDbCommand(sql, con)
                con.Open()
                MsgBox("Connection Open")
                cmd.Parameters.AddWithValue("@p2", "Value for [Course ID]")
                cmd.Parameters.AddWithValue("@p3", "Value for [fName]")
                cmd.Parameters.AddWithValue("@p4", "Value for [sName]")
                cmd.Parameters.AddWithValue("@p5", "Value for [Gender]")
                cmd.Parameters.AddWithValue("@P6", "Value for [DOB]")
                cmd.Parameters.AddWithValue("@p7", "Value for [Address Line 1]")
                cmd.Parameters.AddWithValue("@P8", "Value for [Address Line 2]")
                cmd.Parameters.AddWithValue("@P9", "Value for [Town City]")
                cmd.Parameters.AddWithValue("@P10", "Value for [Postal Code]")
                cmd.Parameters.AddWithValue("@P11", "Value for [Email]")
                cmd.Parameters.AddWithValue("@P12", "Value for [Telephone]")
                cmd.Parameters.AddWithValue("@P13", "Value for [Mobile]")
                cmd.Parameters.AddWithValue("@P14", "Value for [Nat Insurence Num]")
                cmd.Parameters.AddWithValue("@P15", "Value for [Employment Status]")
                cmd.Parameters.AddWithValue("@P22", "Value for [Emergency Contact fName]")
                cmd.Parameters.AddWithValue("@P23", "Value for [Emergency Contact lName]")
                cmd.Parameters.AddWithValue("@P24", "Value for [Emergency Contact Telephone Num]")
                cmd.Parameters.AddWithValue("@P25", "Value for [Relationship with Applicant]")

                MsgBox(cmd.ExecuteNonQuery.GetTypeCode())
                cmd.ExecuteNonQuery() <-------- THIS IS WHERE I GET MY ERROR
            End Using
        End Using
        MsgBox("Connection Closed. Record Added")

    End Sub

0 个答案:

没有答案