Results 1 to 5 of 5

Thread: Inserting a variable with SQL

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455

    Post

    Hello VB users,

    I am trying to insert via SQL a record in my table.

    Source:

    Private Sub Form_Load()
    Set DB = OpenDatabase("c:\q.mdb")
    Dim QQQ As String
    QQQ = "test"
    DB.Execute " INSERT INTO MyTable " _
    & "(MyField) VALUES " _
    & "(QQQ);"
    End Sub

    error message: Too few parameters!

    Normaly is variable QQQ a hard coded string like: "Test".

    How do I change the SQL code so it accept a varible?

    Thanks for reading,

    Michelle.

  2. #2
    Hyperactive Member
    Join Date
    Feb 2000
    Posts
    284

    Post

    If you are inserting a string it has to be in single quotes, it doesn't necessarily have to be hard coded. If you hard code it as a variable and insert the variable you will need to concatenate it into your SQL statement along with the single quotes.

  3. #3
    Lively Member
    Join Date
    Jul 1999
    Posts
    78

    Post

    DB.Execute " INSERT INTO MyTable " _
    & "(MyField) VALUES " _
    & "(QQQ);"

    should read

    DB.Execute " INSERT INTO MyTable " _
    & "(MyField) VALUES " _
    & QQQ & ";"

    In one line it looks like this:
    DB.Execute " INSERT INTO MyTable (MyField) VALUES " & QQQ & ";"

  4. #4
    New Member
    Join Date
    Feb 1999
    Location
    Palmdale, CA US
    Posts
    14

    Post how about that semicolon

    JohnAtWork,

    I noticed you took care to maintain the concluding semicolon on the SQL statement. I have been bitten in the b*tt several times by the semicolon - usually I am copying and pasting from the Access SQL builder into VB (using Jet to access Access97 databases) and leave the semicolon on. In VB6/Jet it seems to frequently cause problems.

    Any comment???

  5. #5
    Lively Member
    Join Date
    Jul 1999
    Posts
    78

    Post

    The semicolon functions as it does in C++ . . . it's the terminating character for a statement.
    I.e. it functions as a carriage return.

    Select BLAH
    From BRAP
    Where BOOF < 3;

    is the same as

    Select BLAH From BRAP Where BOOF < 3;

    the semicolon indicates the end of the statement.


    On occasion the semicolon raises errors for me as well, but it hasn't happened in a long time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width