1

Okay this problem of mine is a little bit tricky for the past months now. SUMMARY: When I only have 5 columns in my Access Database my code successfully inserts the new row. But when I add another column in my access database (that will be 6 columns now) I encounter

Syntax Error INSERT INTO Statement

Code:

  Dim cb As New OleDb.OleDbCommandBuilder(SYSTEM_MainClient.dbda)
    Dim dsNewRow As DataRow
    dsNewRow = SYSTEM_MainClient.DataSet.Tables("Database").NewRow()
    dsNewRow.Item("First_Name") = TXT_FirstName.Text
    dsNewRow.Item("Last_Name") = TXT_Surname.Text
    dsNewRow.Item("StudentID") = CInt(TXT_StudentID.Text)
    dsNewRow.Item("Middle_Name") = TXT_MiddleName.Text
    dsNewRow.Item("College") = TXT_College.Text
    SYSTEM_MainClient.DataSet.Tables("Database").Rows.Add(dsNewRow)
    SYSTEM_MainClient.dbda.Update(SYSTEM_MainClient.DataSet, "Database")
   MsgBox("New Record added to the Database")

My database cColumns:

"StudentID" (primary key), 
"First_Name", "Last_Name", "Middle_Name"
"College"

RESULT: This will work okay, and new record is added into the database.

Because there is only 5 columns.

BUT IF I EXCEED:

Code:

  Dim cb As New OleDb.OleDbCommandBuilder(SYSTEM_MainClient.dbda)
    Dim dsNewRow As DataRow
    dsNewRow = SYSTEM_MainClient.DataSet.Tables("Database").NewRow()
    dsNewRow.Item("First_Name") = TXT_FirstName.Text
    dsNewRow.Item("Last_Name") = TXT_Surname.Text
    dsNewRow.Item("StudentID") = CInt(TXT_StudentID.Text)
    dsNewRow.Item("Middle_Name") = TXT_MiddleName.Text
    dsNewRow.Item("College") = TXT_College.Text

    dsNewRow.Item("Section") = TXT_Section.Text 'I added a section insert

    SYSTEM_MainClient.DataSet.Tables("Database").Rows.Add(dsNewRow)
    SYSTEM_MainClient.dbda.Update(SYSTEM_MainClient.DataSet, "Database")
   MsgBox("New Record added to the Database")

My database columns:

"StudentID" (primary key)
"First_Name", "Last_Name", "Middle_Name"
"College", "Section".

It will return an error

Syntax Error INSERT INTO Statement

It's like it can only insert not more than 5 column values, I think the problem is access not VB itself.

I hope you understand my problem, please help me It's my problem for months now, I searched the forums and Googled it, still can't find the answer.

Thank you so much!

5
  • Perhaps, the problem is with update command of the dbda can you show it? if problem be with it we will fix it Commented Jan 17, 2015 at 12:52
  • section is an access reserved word.support2.microsoft.com/default.aspx?scid=kb;en-us;286335. Try changing it to something else Commented Jan 17, 2015 at 12:59
  • This the 'dbda' line . 'Public dbda As OleDb.OleDbDataAdapter' from the top of the class. Thats all it is, there's no update commands, I think the problem is Access itself because when I delete the "Section" column, everything works fine, but If I added a column again, it will return an error. :( Commented Jan 17, 2015 at 13:02
  • Okay, I'll try to change it. Hope it works. Commented Jan 17, 2015 at 13:03
  • 1
    Oh my god guys! Thank you so much! It works! Thank you very much wholeheartedly I never know that "Section" is a reserved word, my head almost explodes when I can't find the solution, thank you very much! a lesson for me about reserved words. Thank you so much, God Bless! Commented Jan 17, 2015 at 13:11

1 Answer 1

4

This happens almost universally because one or more column names are reserved words or contain spaces or other special characters. In your case I suspect that "section" is a reserved word.

The solution is to set the .QuotePrefix and .QuoteSuffix properties of the OleDbCommandBuilder object to escape all column names. For Access, set them to "[" and "]" respectively.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the heads up :) Really appreciate it :)
I should note that the best solution is to avoid column names that will cause an issue in the first place. I would recommend never including spaces or other special characters in a column name and avoiding reserved words. If you can't or won't do that though, QuotePrefix and QuoteSuffix are there to support you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.