Results 1 to 5 of 5

Thread: sql with loop and var

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    18

    sql with loop and var

    Hi,

    I wish to loop around an array putting the values into a mysql table.

    Code:
    For y = LBound(arrquery) To UBound(arrquery)
       
        With rdoQry
        .SQL = "INSERT INTO `Query` ( `Query_ID` , `Query_text` , `Answered` )VALUES ('', 'arrquery(y)', '0')"
        Set .ActiveConnection = cnMySql
        .Execute
        End With
     
        cnMySql.Close
    Next y
    arrquery() is my array, but it doesnt like me putting it in the SQL like that, what do i need to put round the arrquery(y) to make sure that it knows thar it is a variable i am posting to it, not a string.

    Thanks in advance for any help

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: sql with loop and var

    First - not sure if MySql requires it - but single quotes around the table name is not standard SQL...

    Something like:

    Code:
    For y = LBound(arrquery) To UBound(arrquery)
       
        With rdoQry
        .SQL = "INSERT INTO Query ( `Query_ID` , `Query_text` , `Answered` )VALUES ('', '" & arrquery(y) & "', '0')"
        Set .ActiveConnection = cnMySql
        .Execute
        End With
     
        cnMySql.Close
    Next y

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    18

    Re: sql with loop and var

    Thanks, that has half solved my problem.
    The loop works once now, however when it loops the 2nd time i get an error:

    "Object invalid or not set" on the following line of code:

    Code:
    .SQL = "INSERT INTO `Query` ( `Query_ID` , `Query_text` , `Answered` )VALUES ('', '" & arrquery(y) & "', '0')"
    Do you have any idea why this might be?

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: sql with loop and var

    Sorry about that - didn't notice you were closing the connection in the loop...

    Remove that line - actually drop it below the Next statement...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    18

    Re: sql with loop and var

    thanks... it works now

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