Question

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!

Était-ce utile?

La solution

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

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

Autres conseils

Use CDATE for your Date :

   "INSERT INTO Test ([Text], [Date], [Number]) 
              VALUES ('testing',  CDATE('1990-06-02 00:00:00'), 5)"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top