Results 1 to 3 of 3

Thread: Should i rs.close and Set rs = Nothing in each loop?

  1. #1

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    Should i rs.close and Set rs = Nothing in each loop?

    Hi,
    The code below simply creates a fixed-field string from the data in a database.
    It reads a list of item codes from another file for the where clause of the query.

    Question:
    Should I close the recordset and set it to nothing before re-setting the query with the new where clause?

    It doesn't seem to matter whether I do or don't.

    Thanks,
    Al.

    Code:
    Open "d:\focus\itemcode.prn" For Input As #1
    While Not EOF(1)
    Line Input #1, ItemCode
    Set rs = cn.Execute("Select t$item,t$desc,t$cost,t$whse,t$quan " _
    & "from tiitm001 where t$item='" & ItemCode & "'")
    Do Until rs.EOF = True
        FocusString = Space(80)
        Mid(FocusString, 1, 20) = rs.Fields(0).Value
        Mid(FocusString, 21, 30) = rs.Fields(1).Value
        Mid(FocusString, 51, 10) = rs.Fields(2).Value
        Mid(FocusString, 61, 10) = rs.Fields(3).Value
        Mid(FocusString, 71, 10) = rs.Fields(4).Value
        Print #2, FocusString
        rs.MoveNext
    Loop
    ' **************************************
    ' Do I need the following 2 lines for each iteration of the While/Wend loop?
    rs.Close
    Set rs = Nothing
    ' **************************************
    Wend
    cn.Close
    Close #1
    Close #2
    Set rs = Nothing
    Set cn = Nothing
    A computer is a tool, not a toy.

  2. #2
    Hyperactive Member SjR's Avatar
    Join Date
    Jul 2001
    Location
    Birmingham, UK
    Posts
    336
    You should close before executing another command, but you don't need to set the recordset to nothing until you've completely finished using it.

    Another satisfied customer

  3. #3
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    I'd set it to nothing each time, because you're generating new recordsets with the cn.execute rather than re-using the same recordset object. I'd personally modify the code to use rs.Open for each query, that way you're using the same object.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

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