I'm trying to insert a new record into a database using an SQL command but each time I run the program and try to add a new record I get an error telling me that there's a syntax error with my "INSERT INTO" statement. The data that I'm inserting is stored in an array + structure:
Structure Question
Dim QuestionName As String
Dim Question As String
Dim Ans1 As String
Dim Ans2 As String
Dim Ans3 As String
Dim Ans4 As String
Dim Difficulty As Integer
Dim CorrectAns As String
End Structure
Dim arrQuestion as Question
and this is the sub im using to insert the record into the database:
Try
Dim InsertComm As New OleDb.OleDbCommand
Dim dbAdap As New OleDb.OleDbDataAdapter
ConnectToDB()
Dim sqlInsert As String = "INSERT INTO questionDatabase(QuestionName, Question,
Answer 1, Answer 2, Answer 3, Answer 4, Correct answer,
Difficulty ID) VALUES(" & Chr(39) & arrquestion.questionname
& Chr(39) & ", " & Chr(39) & arrquestion.question & Chr(39) &
", " & Chr(39) & arrquestion.ans1 & Chr(39) & ", " & Chr(39)
& arrquestion.ans2 & Chr(39) & ", " & Chr(39) &
arrquestion.ans3 & Chr(39) & ", " & Chr(39) &
arrquestion.ans4 & Chr(39) & ", " & Chr(39) &
arrquestion.correctans & Chr(39) & ", " & Chr(39) &
arrquestion.difficulty & Chr(39) & ");"
InsertComm = New OleDb.OleDbCommand(sqlInsert, dbConn)
InsertComm.ExecuteNonQuery()
dbConn.Close()
Catch ex As Exception
MsgBox(Err.Description)
Finally
dbConn.Close()
End Try
I've written and re-written this a bunch of times, googled the error it gives me and tried to copy the solutions that people posted there but I just couldn't get my head around how they wrote the code. Any help would really be appreciated.