CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    [RESOLVED] "NEW" Recordset

    I have defined my recordsets like this

    Code:
    Public Rs As New ADODB.Recordset
    I Open them like this

    Code:
    Set Rs = New ADODB.Recordset
    Rs.Open StrSql, Db, adOpenStatic, adLockOptimistic
    It seems to me to be a DOUBLE "NEW"

    Should it be the following -

    Code:
    Public Rs As ADODB.Recordset
    .......
    Set Rs = New ADODB.Recordset
    Rs.Open StrSql, Db, adOpenStatic, adLockOptimistic
    What happens when I say "NEW" ?

    What is the "CORRECT" way to handle ADO Records sets ?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: "NEW" Recordset

    I think in the case of a public RS you should use the

    Code:
    Public RS as ADODB.Recordset
    ........
    Set RS = New ADODB.Recordset
    In the case of a Local RS I you could do it this way as well or you could also do it ....

    Code:
    Dim RS as New ADODB.Recordset
    RS.Open ......
    My Understanding is that New creates an object and/or allocates memory for it. If you Dim it as new then Set as new it could be wasting that memory allocated. It is possible even likely that the Set uses the same memory so no memory is lost but I wold think that best case senario would be that the program is slightly slower with the extra call to New which is not needed.

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: "NEW" Recordset

    Actually it is not something to with Public & local. Why initialize it before you are actually using it. I always prefer this style because it makes code much more understandable
    Code:
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    This means I will initialize it only when I need it. Now some may argue that there is no difference other than that. Imagine if you were supposed to test the object = nothing, using Dim As new would always return a false because the object is already initialized.

    There was this nice thread here which discussed about implications of using Dim As New, but i was not able to find it.

  4. #4
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: "NEW" Recordset

    Thanks - you have confirmed what I suspected

    I believe, like you, this is the right way

    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: "NEW" Recordset

    Everytime you do that, you HAVE to destroy it after closing. Add this:
    Code:
    Set rs = Nothing
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: "NEW" Recordset

    Thanks guys - thats all excellent


    Makes a lot of sense

    Set rs = Nothing

  7. #7
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: "NEW" Recordset

    Quote Originally Posted by Shuja Ali View Post
    Actually it is not something to with Public & local. Why initialize it before you are actually using it. I always prefer this style because it makes code much more understandable
    Code:
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    This means I will initialize it only when I need it. Now some may argue that there is no difference other than that. Imagine if you were supposed to test the object = nothing, using Dim As new would always return a false because the object is already initialized.

    There was this nice thread here which discussed about implications of using Dim As New, but i was not able to find it.
    Just to illustrate

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        LooseBinding
        StrongBinding
    End Sub
    
    Private Sub LooseBinding()
        Dim adoRS As New ADODB.Recordset
        Set adoRS = Nothing
        MsgBox adoRS Is Nothing ' returns false
    End Sub
    
    Private Sub StrongBinding()
        Dim adoRS As ADODB.Recordset
        Set adoRS = Nothing
        MsgBox adoRS Is Nothing ' returns true
    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

Featured