Results 1 to 13 of 13

Thread: Help with insert query using VB6

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Help with insert query using VB6

    I can't get the following insert statement to work using VB6. As you can see from the code, I am trying to insert dummy text data into the "Data_Cert_Results" table. Any ideas....?

    My error upon calling Execute is:

    Unable to execute -
    (-2147217900: The name "aaaaaaa" is not permitted in this contect. Valid expressions are constants, contant, expressions, and (in some contects) variables. Column names are not permitted.


    Dim mobjADOConn As adodb.Connection
    Dim dbrs As adodb.Recordset

    Dim sqlstrInsert As String
    Dim td As String
    Dim ca As String
    Dim pc As String

    Set mobjADOConn = New adodb.Connection
    mobjADOConn.ConnectionString = "DSN=aaa;Uid=bbb;Pwd=ccc;"
    mobjADOConn.Open

    ' Create a Recordsets by executing an SQL statement.
    Set dbrs = mobjADOConn.Execute(sqlstr)

    ' Loop through the recordset and show the fields.
    If dbrs.RecordCount >= 1 Then
    dbrs.MoveFirst
    End If
    Do While Not dbrs.EOF
    ' Show recordset fields
    td = "AAAA"
    ca = "BBBB"
    pc = "CCCC"
    sqlstrInsert = "INSERT INTO " & "Date_Cert_Results (Type_of_Data, Check_Area, Product_Code) values (" & td & "," & ca + "," & pc & ")"
    mobjADOConn.Execute (sqlstrInsert)

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Help with insert query using VB6

    When inserting strings (text) into a database they need to be enclosed by single qoutes

    Code:
    sqlstrInsert = "INSERT INTO " & "Date_Cert_Results (Type_of_Data, Check_Area, Product_Code) values ('" & td & "','" & ca + "','" & pc & "')"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Help with insert query using VB6

    Great, thanks for the info....!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Help with insert query using VB6

    Still something appears to be wrong once I build on what you said. I am now getting unclosed quotation mark after the character string ".

    I do not see it, do you?

    sqlstrInsert = "INSERT INTO " & "Data_Cert_Results (Data_Cert_Results_Id, Rn_Descriptor, Rn_Create_Date, Rn_Create_User, Rn_Edit_Date, Rn_Edit_User) values ('" & resultid & "','" & rndesc & "','" & rncreatedate & "','" & rncreateuser & "','" & rneditdate & "','" & rnedituser & "')"

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Help with insert query using VB6

    I hate string.....!

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Help with insert query using VB6

    It's not just String values that need to be delimited in a certain way in SQL statements, similar applies to Date and other data types. For more info, see the article How do I use values (numbers, strings, dates) in SQL statements? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)

    What does sqlstrInsert contain when you get the error? (you can use Debug.Print sqlstrInsert to copy it to the Immediate window, for easy copy & paste here).

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Help with insert query using VB6

    Below is what sqlstrInsert contains..... My ID and User fields should display a hex value. From the output below, they display ' ?'. Could this be the problem?

    INSERT INTO Data_Cert_Results (Data_Cert_Results_Id, Rn_Descriptor, Rn_Create_Date, Rn_Create_User, Rn_Edit_Date, Rn_Edit_User) values (' A','1 - Titles (ex. JR SR II MD).
    Given & Middle Name/Initial comINSERT INTO Data_Cert_Results (Data_Cert_Results_Id, Rn_Descriptor, Rn_Create_Date, Rn_Create_User, Rn_Edit_Date, Rn_Edit_User) values (' A','1 - Titles (ex. JR SR II MD).
    Given & Middle Name/Initial compounded in Given N','1/19/2009 1:15:12 PM',' ?','1/19/2009 1:15:12 PM',' ?')'
    pounded in Given N','1/19/2009 1:15:12 PM',' ?','1/19/2009 1:15:12 PM',' ?')'

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Help with insert query using VB6

    You seem to have tested it twice, and left the cursor in the middle of the text - can you please try again.

    The only thing that pops out at me is that it seem like you have ' after the final bracket, which is not what you should have (and is not in the code you posted before).

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Help with insert query using VB6

    Hi, here is what sqlstrInsert is set to within VB.

    sqlstrInsert = "INSERT INTO " & "Data_Cert_Results (Data_Cert_Results_Id, Rn_Descriptor, Rn_Create_Date, Rn_Create_User, Rn_Edit_Date, Rn_Edit_User) values ('" & resultid & "','" & rndesc & "','" & rncreatedate & "','" & rncreateuser & "','" & rneditdate & "','" & rnedituser & "')'"

    Below is what is shown with the debug.print:

    INSERT INTO Data_Cert_Results (Data_Cert_Results_Id, Rn_Descriptor, Rn_Create_Date, Rn_Create_User, Rn_Edit_Date, Rn_Edit_User) values (' A','1 - Titles (ex. JR SR II MD).
    Given & Middle Name/Initial compounded in Given N','1/19/2009 1:15:12 PM',' ?','1/19/2009 1:15:12 PM',' ?')'

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Help with insert query using VB6

    The problem is as I said:
    Code:
    ... 12 PM',' ?')'
    And it is due to this, which you added since you last posted the code:
    Code:
    ... & rnedituser & "')'"


    I don't know why you have ? for a couple of the fields, you should check the variables that those values are coming from.

  11. #11
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    398

    Re: Help with insert query using VB6

    its hard to identify errors with those concatenation of strings. try to use other code..
    let say
    vb Code:
    1. with dbrs
    2. .open "Select * from TableName"
    3. .addnew
    4. !fieldname = Value
    5. .close
    6. end with
    ________
    Mazda rotary pickup history
    Last edited by rothj0hn; Feb 15th, 2011 at 01:38 PM.

  12. #12
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    398

    Re: Help with insert query using VB6

    i forgot to include the .update

  13. #13
    Lively Member
    Join Date
    Oct 2008
    Location
    Philippines, Surigao city
    Posts
    70

    Re: Help with insert query using VB6

    Code:
    private subcmd_click()
    set rs = new adodb.recordset
    rs.open"select * from tablename",cn,1,2
    rs.addnew
    !fieldname = textname.text
    rs.update
    set rs = nothing
    end sub

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