How to ADD a record in a MS access

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\VB.net\addressbook\bin\AddressBook.mdb;Persist Security Info=True")

Dim sql As String = String.Empty

'determine if we are adding a new record or editing an
'existing record based on the value of the _contactID
'Variable
If _contactID = 0 Then
'Adding a new record

'Error is coming in the below insert statement
sql = "insert into Contacts(Title,FirstName,LastName)" & "values(" '&txtTitle.text&'","'&txtFirstName.text&'","'&txtLastName.text&'")""
End If

'Open the connection to the databaes and execute the
'SQL Statement
conn.Open()
Dim command As New OleDbCommand(sql, conn)
command.ExecuteNonQuery()
conn.Close()

'close the form
Me.Close()
End Sub

[1047 byte] By [BobbyDreamer] at [2007-12-28]
# 1

what error do you get? in addition you should be using parameterized queries like so:

Dim titleParam as new OleDbParameter("@title", Me.txtTitle.Text)

Dim firstnameParam as new OleDbParameter("@firstName", Me.txtFirstName.Text)

Dim lastnameParam as new OleDbParameter("@lastName", Me.txtLastName.Text)

Dim theOleDbCommand as new OleDbCommand("INSERT INTO [Contacts] (Title, FirstName, LastName) VALUES (?, ?, ?)", new OleDbConnection(ConnectionString))

theOleDbCommand.Parameters.Add(titleParam)

theOleDbCommand.Parameters.Add(firstnameParam)

theOleDbCommand.Parameters.Add(lastnameParam)

'open connection and executenonquery

'close connection

ahmedilyas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...