-
April 12th, 2009, 02:34 AM
#1
[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 ?
-
April 12th, 2009, 05:16 AM
#2
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.
-
April 12th, 2009, 06:56 AM
#3
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.
-
April 12th, 2009, 09:14 AM
#4
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
-
April 12th, 2009, 11:00 AM
#5
Re: "NEW" Recordset
Everytime you do that, you HAVE to destroy it after closing. Add this:
-
April 12th, 2009, 09:01 PM
#6
Re: "NEW" Recordset
Thanks guys - thats all excellent
Makes a lot of sense
-
April 15th, 2009, 04:39 PM
#7
Re: "NEW" Recordset
Originally Posted by Shuja Ali
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|