I am using VB .Net to loop through a regex Match and generate a sql statement. I’m creating the sql like this
sql = "Insert Into Agencies (Address) Values"
While MatchObj.Success
sql = sql & "(""" & MatchObj.Groups(1).Value & """), "
MatchObj = MatchObj.NextMatch()
End While
sql = sql.Substring(0, Len(sql) - 2) & ";"
so when I print sql to the immediate window after creating it, I get this:
Insert Into Agencies (Address) Values(" 1330 W Indian School Rd, "), (" 3323 E Baseline Rd, "), (" 207 N Gilbert Rd, "), (" 3160 S. Gilbert Rd., Ste. 4, ");
I then create a OleDbCommand using the sql statement. I can connect up to the DB but when I run the .ExecuteNonQuery() I get this error: “Missing semicolon (;) at end of SQL statement”
My goal is to use a single INSERT to put all these values into the DB column “Address”.
Any help is much appreciated.
Edit:
Here is the sub I’m using to open and execute
Public Sub executeSQL(ByVal sql As String)
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Local Projects\AgenciesAZ.mdb'"
Dim conn As OleDbConnection = New OleDbConnection(connString)
Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
conn.Open()
cmd.ExecuteNonQuery() 'error hits here
conn.Close()
End Sub
You need to split your insert statement ,you have to use the “insert into…” every time that you want to insert something in the Address but also if you want to to execute it just once in VB you need to separate each statement with the semicolon (;), it should work
You need to do something like this.
Edit
your while should be
Although I strongly recommend to insert each one in separate statements and in a transaction, but it depends to you.
Edit 2
You can’t insert multiple records at the same time in MS- Access you must do it insert per insert like…