This forum is in permanent archive mode. Our new active community can be found here.

VB Help: OleDbException was Unhandled

edited May 2008 in Technology
Getting a error that I haven't been able to figure out. Goggled it with no luck hope someone can help.

Adding a contact to the access mdb database and checking the Contact_Id if the user exists.

Syntax error in INSERT INTO statement

image

Imports System.Data
Imports System.Data.OleDb
Public Class Modify
Private _contactID As Int32 = 0

Public Property Contact_ID() As Int32
Get
Return _contactID
End Get
Set(ByVal value As Int32)
_contactID = value
End Set
End Property



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=" _
& Application.StartupPath & "\Contacts.mdb")

Dim sql As String = String.Empty
If _contactID = 0 Then
sql = "INSERT INTO Contacts (First Name, Last Name, Address, Phone Number, Email Address, Cell Phone) VALUES ('" & tbFName.Text & "','" & tbLName.Text & "','" & tbAddress.Text & "','" & tbPhoneNum.Text & "','" & tbEmail.Text & "','" & tbCellNum.Text & "')"
End If

'Opens the database and then opens the sql command
conn.Open()

'sets command as new instance of OleDbCommand and then runs ExecuteNonQuery
'Closes connection
Dim command As New OleDbCommand(sql, conn)
command.ExecuteNonQuery()
conn.Close()

'Closes the forum
Me.Close()


End Sub
End Class

Comments

  • Sorry dude, it's been 3 years since I last looked at VB code. I'm rolling on javascript, HTML, & CSS now.
  • Your problem is that your SQL is incorrect. First of all, what kind of SQL database lets you have spaces in field names? Secondly, why are you taking values directly from text fields and putting them into the SQL statement? You're just begging for some SQL injection attacks. Escape your inputs and fix your SQLs.
  • You're connecting to a JET database, so you almost certainly have Access installed on your PC. Open the database, select Query from the home menu, and hit Add with Designer. Right-click in the designer window, hit SQL, and paste your statement in there. Save and run your query. You've probably got a mistyped field name - if you do, Access will prompt you to enter a value for [DatabaseName].[IncorrectFieldName]
Sign In or Register to comment.