Resolved Inserting a record into Access database programatically using Parameters

thebatfink

Well-known member
Joined
Mar 29, 2012
Messages
47
Programming Experience
Beginner
Hi, I am trying to insert a record into an Access .mdb database. Now it is inserting a record, however, the fields aren't being populated with the correct values. For instance,

Contacts.Title field is being populated with the text value of Me.txtCompanyName.Text?? Several of these 'mix ups' are occuring...

VB.NET:
Private Sub btnSaveAndExit_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveAndExit.Click
        Dim InsertQuery As String
        Dim strConnectionString As String = "provider = microsoft.jet.oledb.4.0; data source = " & Application.StartupPath & "\dsbdatabase.mdb"
        Dim con As New OleDbConnection(strConnectionString)
        InsertQuery = "INSERT " _
        & "INTO " _
        & "Contacts (LastName,FirstName,EmailAddress,Title,JobTitle,BusinessPhone,HomePhone,MobilePhone,FaxNumber,Address,City,County,AddressLine2,Postcode,Webpage,Notes,CompanyName) " _
        & "VALUES " _
        & "(@LastName,@FirstName,@EmailAddress,@Title,@JobTitle,@BusinessPhone,@HomePhone,@MobileNumber,@FaxNumber,@Address,@City,@County,@AddressLine2,@Postcode,@Webpage,@Notes,@CompanyName)"
        con.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(InsertQuery, con)
        cmd.Parameters.AddWithValue("@FirstName", Me.txtFirstName.Text)
        cmd.Parameters.AddWithValue("@LastName", Me.txtLastName.Text)
        cmd.Parameters.AddWithValue("@EmailAddress", Me.txtEmailAddress.Text)
        cmd.Parameters.AddWithValue("@Title", Me.cboxTitle.Text)
        cmd.Parameters.AddWithValue("@JobTitle", Me.txtJobTitle.Text)
        cmd.Parameters.AddWithValue("@BusinessPhone", Me.txtWorkNumber.Text)
        cmd.Parameters.AddWithValue("@HomePhone", Me.txtHomeNumber.Text)
        cmd.Parameters.AddWithValue("@MobileNumber", Me.txtMobileNumber.Text)
        cmd.Parameters.AddWithValue("@FaxNumber", Me.txtFaxNumber.Text)
        cmd.Parameters.AddWithValue("@Address", Me.txtAddressLine1.Text)
        cmd.Parameters.AddWithValue("@City", Me.txtCity.Text)
        cmd.Parameters.AddWithValue("@County", Me.cboxCounty.Text)
        cmd.Parameters.AddWithValue("@AddressLine2", Me.txtAddressLine2.Text)
        cmd.Parameters.AddWithValue("@Postcode", Me.txtPostcode.Text)
        cmd.Parameters.AddWithValue("@Webpage", Me.txtWebsite.Text)
        cmd.Parameters.AddWithValue("@Notes", Me.txtNotes.Text)
        cmd.Parameters.AddWithValue("@CompanyName", Me.txtCompanyName.Text)
        cmd.ExecuteNonQuery()
        con.Close()
        MsgBox("Contact successfully added.", vbInformation, "CONTACT ADDED")
End Sub


Why is it doing this? I'm scratching my head. I have checked the names of the TextBoxes and they are all written correctly in my code?!

Any help would be much appreciated! Thanks!
 
Last edited:
Scratch that :/ I may of forgotten to build the application after editing and retrying my app - Stupid.

Anyhow, some working code above if anyone finds it useful :)
 
Back
Top