SQL INSERT INTO statement for MS Access

gromit1 picture gromit1 · Mar 7, 2013 · Viewed 15.4k times · Source

I am trying to learn how to use MS Access with my VB.net program. I am practicing learning how to use the INSERT INTO statement but I keep getting an error.

Here is my code:

Imports System.Data.OleDb

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myConnection As OleDbConnection
    Dim DBpath As String = "C:\Documents and Settings\nordeen1\Desktop\Test.mdb"
    Dim sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBpath & ";Persist Security Info=True"
    myConnection = New OleDbConnection(sConnectionString)
    myConnection.Open()
    Dim SQLstr As String
    SQLstr = "INSERT INTO Test (Text, [Date], Number) VALUES ('testing', #2/6/1990#, 5)"
    Dim cmd As New OleDbCommand(SQLstr, myConnection)
    cmd.ExecuteNonQuery()
End Sub
End Class

I get this error "OleDbException was unhandled. Syntax error in INSERT INTO statement." at cmd.ExecuteNonQuery()

Any suggestions are greatly appreciated! Thanks!

Answer

John Woo picture John Woo · Mar 7, 2013

TEXT and NUMBER are also reserved so they should be delimited,

SQLstr = "INSERT INTO Test ([Text], [Date], [Number]) VALUES ('testing', #2/6/1990#, 5)"