Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:14:37+00:00 2026-06-18T07:14:37+00:00

I’m trying to insert a new record into a database using an SQL command

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:14:38+00:00Added an answer on June 18, 2026 at 7:14 am

    The core of your statement should be written in this way

    Dim sqlInsert As String = "INSERT INTO questionDatabase(QuestionName, Question, " +
          "[Answer 1], [Answer 2], [Answer 3], [Answer 4], [Correct answer], " +
          "[Difficulty ID]) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"
    InsertComm = New OleDb.OleDbCommand(sqlInsert, dbConn)
    InsertComm.Parameters.AddWithValue("@p1", arrquestion.questionname)
    InsertComm.Parameters.AddWithValue("@p2", arrquestion.question )
    InsertComm.Parameters.AddWithValue("@p3", arrquestion.ans1)
    InsertComm.Parameters.AddWithValue("@p4", arrquestion.ans2)
    InsertComm.Parameters.AddWithValue("@p5", arrquestion.ans3)
    InsertComm.Parameters.AddWithValue("@p6", arrquestion.ans4)
    InsertComm.Parameters.AddWithValue("@p7", arrquestion.correctans)
    InsertComm.Parameters.AddWithValue("@p8", arrquestion.difficulty)
    InsertComm.ExecuteNonQuery()
    

    As you can see, the first thing is to encapsulate every field name with square brackets to resolve the problem of spaces in field names.
    The second point is to use parametrized query to avoid parsing problems (quotes in strings, date, decimals etc..) and most important Sql Injection

    Also note that the parameters in an OleDb environment should be added to the ParametersCollection in the same order in which their respective placeholders (?) appear in the sql text

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.