I'm trying to delete all data from a table in an Access (*.mdb) database in VB.NET, then reinsert new data in it however I get the error "Syntax error in INSERT INTO statement" when executing the following code:
Imports System
Imports System.Data
Imports System.Data.SqlClient
...
'test.mdb file
'with a table "MyTable"
'with two fields:
'"name" (Memo), "value" (Memo)
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:\Users\Max\Desktop\test.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM MyTable"
'select data from database, fill and clear dataset to get the table schema
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "MyTable")
ds.Clear()
'add a new row in the dataset
Dim dsNewRow As DataRow = ds.Tables("MyTable").NewRow()
dsNewRow.Item("name") = "TESTNAME"
dsNewRow.Item("value") = "TESTVALUE"
ds.Tables("MyTable").Rows.Add(dsNewRow)
'update the database
Dim objCommandBuilder As New OleDb.OleDbCommandBuilder(da)
da.Update(ds, "MyTable")
ds.Dispose()
da.Dispose()
con.Close()
con.Dispose()
You indicated those steps eliminated the error.
The problem was that both
name
andvalue
are reserved words. In some situations, the Access db engine can accept reserved words as field names without complaining. However, it seems less forgiving when OleDb is involved.Ideally, avoid reserved words for Access db object names. That linked page includes a link to a free utility (DbIssueChecker.zip) you can use to examine your Access db file for problem names and other issues.