Results 1 to 4 of 4

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
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Post

    The sting QQQ needs to be in quotes
    I usualy do it liek this:

    const Quote = """"

    DB.Execute "INSERT INTO MyTable(MyField) VALUES(" & quote & QQQ & quote & ");"
    Mark
    -------------------

  3. #3
    Member
    Join Date
    Jan 2000
    Location
    Southbridge, MA, USA
    Posts
    40

    Post

    Michelle--
    Mark is correct that QQQ needs "'s.
    The following snippet is a little more complicated than his, but I think by actually writing out your SQL statement as below, it's a little clearer what's going on... I hope.
    --Carl

    Dim dbInsert As Database
    Dim qdfInsert As QueryDef
    Dim insSQL As String ' SQL statement for appending data
    Dim QQQ As String
    QQQ = "test"
    insSQL = "INSERT INTO MyTable (MyField) VALUES ('" & QQQ & "');"
    Set dbInsert = OpenDatabase("c:\q.mdb")

    ' Create the QueryDef object to append the data
    Set qdfInsert = dbInsert.CreateQueryDef("", insSQL)
    qdfInsert.Execute
    qdfInsert.Close


    Edited by cfmoxey on 03-10-2000 at 05:02 PM

  4. #4

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

    Post

    Hello Mark Sreeves & cfmoxey,

    Thanks for your information.

    Michelle.

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