I'm running into an interesting situation that I can't seem to solve. I have a set of (unbound) controls with various values provided by the user. When they click a button on the form, I am wanting to place these values into a Access table. The button executes the following code: (note: I have omitted the connection string - I have varified it is not the problem)
---------------
Dim ReportDatabase As New System.Data.OleDb.OleDbConnection(strConn)
Dim sqlSQL As New OleDb.OleDbCommand
ReportDatabase.Open()
sqlSQL.Connection = ReportDatabase
sqlSQL.CommandText = "INSERT INTO tblRequests (User, DateRequested, ReportName, TargetDate, Priority, Format, Status, Instructions) " & "VALUES ('" & strUserId & "', #" & Now.Date & "#, '" & txtReportName.Text & "', #" & dtpTargetDate.Text & "#, '" & cboPriority.Text & "', '" & cboFormat.Text & "', '" & cboStatus.Text & "', '" & txtInstructions.Text & "')"
sqlSQL.ExecuteNonQuery()
ReportDatabase.Close()
----------------
This code throws an OledbException: "Syntax error in INSERT INTO statement." However, I do not think there is a syntax error. Without the variables, the sqlSQL.CommandText expression is this:
-----------------
INSERT INTO tblRequests (User, DateRequested, ReportName, TargetDate, Priority, Format, Status, Instructions)
VALUES ('falsep', #4/6/2007#, 'Test', #4/12/2007#, 'High', 'Crystal Report', 'Unacknowledged', 'This is a test.')
-----------------
I have executed this SQL statement directly with the Access database in question, and things work just fine. There is nothing wrong with the SQL as far as I can tell - this leads me to believe that something is wrong with my code.
Would any
VB database regulars be able to tell me if you see anything out of the ordinary? Any help would be greatly appreciated.
EDIT:
I attempted this same action again, this time using the syntax recommended by Microsoft, and simplifying the INSERT statement to something so simple that it couldn't be screwed up.
------------------
Dim sqlSQL = New OleDbCommand("INSERT INTO tblRequests (User) VALUES ('Crap')", ReportDatabase)
ReportDatabase.Open()
sqlSQL.ExecuteNonQuery()
ReportDatabase.Close()
------------------
Again, I verified that all spelling was correct, and that the database I'm connecting to isn't corrupted. I can connect to the database fine (as I pull values from other tables for this same form). However, even with this completely simplified code, I still get the "Syntax error in INSERT INTO statement". I can run the SQL directly against the database myself and get the expected result.
I'm really at a loss here. Am I crazy? Have I forgotten something important?