Results 1 to 5 of 5

Thread: [RESOLVED] Multiple Record set in one execute

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Location
    AZ
    Posts
    67

    Resolved [RESOLVED] Multiple Record set in one execute

    Looking for information on how to place two select statement within a loop

    The first is to look up the balence for each customer then the second is to insert that balance into another row in a another table...

    I have this though the second Select Statement is still connected so I can not rs.movenext from the first select statement...

    Let me know what you think, Thank you


    Code Snippet

    ##################################################

    Private Sub RUNBATCH()

    conn.Open sConnString
    Set cmd.ActiveConnection = conn

    'SELECT STATEMENT

    cmd.CommandText = "SELECT BatchID, CustID, Balance " & _
    " From CUSTBAL" 'WHERE CustID = '" & Val(txtCustID.Text) & "'"



    cmd.CommandType = adCmdText
    Set rs = cmd.Execute


    Do Until rs.EOF

    [INDENT]strIBatchID = rs!BatchID
    [INDENT]strICustID = rs!CustID
    strIBal = rs!Balance



    'INSERT VALUES FROM FIRST SELCT STATEMENT - CURRENT RECORD
    cmd.CommandText = "INSERT INTO TBLINVOICE (BID, CID, TOTBILL)" & _
    " VALUES (" & Val(strIBatchID) & " , " & Val(strICustID) & " , " & Val(strIBal) & ")"


    'EXECUTES SECOND INSERT STATEMENT

    cmd.CommandType = adCmdText
    Set rs = cmd.Execute



    'MOVE TO NEXT RECORD - NEEDS TO BE FROM FIRST STATEMENT
    rs.MoveNext


    'LOOPS
    Loop


    'CLEARS
    Set rs = Nothing
    Set cmd = Nothing
    conn.Close
    Set conn = Nothing



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

    Re: Multiple Record set in one execute

    I'm not sure why this thread is buried in the COM and ActiveX section of the forum - we do have a very active DATABASE DEVELOPMENT forum...

    I would execute an action query first - such as:

    Code:
    INSERT INTO TBLINVOICE (BID, CID, TOTBILL) SELECT BatchID, CustID, Balance " & _
    " From CUSTBAL" 'WHERE CustID = '" & Val(txtCustID.Text) & "'"
    That way the database work was already done - the TBLINVOICE was populated with the rows desired.

    No iterative/looping action against the database.

    Why do you need to process the recordset in VB?

    *** 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
    Lively Member
    Join Date
    Jun 2005
    Location
    AZ
    Posts
    67

    Re: Multiple Record set in one execute

    The program is being done in VB6.. Thanks

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Multiple Record set in one execute

    Quote Originally Posted by Zoila
    The program is being done in VB6.. Thanks
    Yes, but you can write your database code in SQL Server and call it from VB.

  5. #5
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Multiple Record set in one execute

    . . . and this code really needs to be in transaction, too. . . .

    Definately repost to the boys in the DB development bit. Those guys live for this sort of stuff . . . .
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

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