redundant New: Dim rs As New ADODB.Recordset 
Author Message
 redundant New: Dim rs As New ADODB.Recordset

Hi,

What do you think?

--------------------------------------------------------
Redundant use of New:
------------------------

Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset     'redundant New

...
Set rs = cmd.Execute     'for this
...
rs.Close

--------------------------------------------------------
This is Ok:
-----------
Dim rs As New ADODB.Recordset

rs.Open "SELECT * FROM Authors", "DSN=AdoDemo;UID=ad;PWD=;"
...
rs.Close
Set rs = Nothing

--------------------------------------------------------
Thanks in advance
Zsolt



Wed, 11 Jun 2003 07:29:04 GMT  
 redundant New: Dim rs As New ADODB.Recordset
The 2nd example is fine, but ...

It depends on what you are doing with the recordset.

If you are just going to do a single pass through the records then

    dim rs as object

    set rs = <connection>.execute("<sql>")

    do until rs.eof
        ...
        rs.movenext
    loop

    set rs = nothing

is fine. (assumes that <connection> has already been established)


Quote:
> Hi,

> What do you think?

> --------------------------------------------------------
> Redundant use of New:
> ------------------------

> Dim cmd As New ADODB.Command
> Dim rs As New ADODB.Recordset     'redundant New

> ...
> Set rs = cmd.Execute     'for this
> ...
> rs.Close

> --------------------------------------------------------
> This is Ok:
> -----------
> Dim rs As New ADODB.Recordset

> rs.Open "SELECT * FROM Authors", "DSN=AdoDemo;UID=ad;PWD=;"
> ...
> rs.Close
> Set rs = Nothing

> --------------------------------------------------------
> Thanks in advance
> Zsolt



Wed, 11 Jun 2003 07:53:39 GMT  
 redundant New: Dim rs As New ADODB.Recordset
The first example is Ok without New in the 2nd Dim

Like this:

Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset     'no New
...
Set rs = cmd.Execute     'for this
...
rs.Close

Thanks
Zsolt



Quote:
> Hi,

> What do you think?

> --------------------------------------------------------
> Redundant use of New:
> ------------------------

> Dim cmd As New ADODB.Command
> Dim rs As New ADODB.Recordset     'redundant New

> ...
> Set rs = cmd.Execute     'for this
> ...
> rs.Close

> --------------------------------------------------------
> This is Ok:
> -----------
> Dim rs As New ADODB.Recordset

> rs.Open "SELECT * FROM Authors", "DSN=AdoDemo;UID=ad;PWD=;"
> ...
> rs.Close
> Set rs = Nothing

> --------------------------------------------------------
> Thanks in advance
> Zsolt



Wed, 11 Jun 2003 08:36:21 GMT  
 redundant New: Dim rs As New ADODB.Recordset

Well actually Dim cmd as new adodb.command is slower!

What you are actually saying is .......

If cmd = nothing then set cmd = new adodb.command

You should aways use..........

Dim cmd as Adodb.command
Set cmd = new adodb.command

Phrase from "ADO examples and best practices"

If you follow this best-pratice coding convention then
your applications will run a little faster, and you avoid crippling
some of the features provided by ADO when working with multiple
recordset stored procedures..............

So all I can say is what the other 2 advise is incorrect.........

Regards

VOrtex


Quote:
> Hi,

> What do you think?

> --------------------------------------------------------
> Redundant use of New:
> ------------------------

> Dim cmd As New ADODB.Command
> Dim rs As New ADODB.Recordset     'redundant New

> ...
> Set rs = cmd.Execute     'for this
> ...
> rs.Close

> --------------------------------------------------------
> This is Ok:
> -----------
> Dim rs As New ADODB.Recordset

> rs.Open "SELECT * FROM Authors", "DSN=AdoDemo;UID=ad;PWD=;"
> ...
> rs.Close
> Set rs = Nothing

> --------------------------------------------------------
> Thanks in advance
> Zsolt



Wed, 11 Jun 2003 20:16:02 GMT  
 redundant New: Dim rs As New ADODB.Recordset
Another more subtle side effect:

Dim cmd As New ADODB.Command
Set cmd = Nothing

After cmd is set to nothing, VB immediately creates a new instance of cmd.
Maybe not a big deal here, but if you have code that makes decisions based
upon whether or not an object exists, you get into big trouble.

 - Tom


Quote:
> Well actually Dim cmd as new adodb.command is slower!

> What you are actually saying is .......

> If cmd = nothing then set cmd = new adodb.command

> You should aways use..........

> Dim cmd as Adodb.command
> Set cmd = new adodb.command

> Phrase from "ADO examples and best practices"

> If you follow this best-pratice coding convention then
> your applications will run a little faster, and you avoid crippling
> some of the features provided by ADO when working with multiple
> recordset stored procedures..............

> So all I can say is what the other 2 advise is incorrect.........

> Regards

> VOrtex



> > Hi,

> > What do you think?

> > --------------------------------------------------------
> > Redundant use of New:
> > ------------------------

> > Dim cmd As New ADODB.Command
> > Dim rs As New ADODB.Recordset     'redundant New

> > ...
> > Set rs = cmd.Execute     'for this
> > ...
> > rs.Close

> > --------------------------------------------------------
> > This is Ok:
> > -----------
> > Dim rs As New ADODB.Recordset

> > rs.Open "SELECT * FROM Authors", "DSN=AdoDemo;UID=ad;PWD=;"
> > ...
> > rs.Close
> > Set rs = Nothing

> > --------------------------------------------------------
> > Thanks in advance
> > Zsolt



Wed, 11 Jun 2003 22:14:30 GMT  
 redundant New: Dim rs As New ADODB.Recordset
Hi Tom,

You are mistaken on the order of creates a new instance of cmd.

1: Dim cmd As New ADODB.Command
2: Set cmd = Nothing
3: Debug.Print TypeName( cmd)   'Displays "Command"

Line 2:   VB creates a new instance of Command object and immediately
destroys it.

Line 3:   Creates a new instans of Command

You are absolutelly right of "decisions based..."

Thanks
Zsolt



Quote:
> Another more subtle side effect:

> Dim cmd As New ADODB.Command
> Set cmd = Nothing

> After cmd is set to nothing, VB immediately creates a new instance of cmd.
> Maybe not a big deal here, but if you have code that makes decisions based
> upon whether or not an object exists, you get into big trouble.

>  - Tom



> > Well actually Dim cmd as new adodb.command is slower!

> > What you are actually saying is .......

> > If cmd = nothing then set cmd = new adodb.command

> > You should aways use..........

> > Dim cmd as Adodb.command
> > Set cmd = new adodb.command

> > Phrase from "ADO examples and best practices"

> > If you follow this best-pratice coding convention then
> > your applications will run a little faster, and you avoid crippling
> > some of the features provided by ADO when working with multiple
> > recordset stored procedures..............

> > So all I can say is what the other 2 advise is incorrect.........

> > Regards

> > VOrtex



> > > Hi,

> > > What do you think?

> > > --------------------------------------------------------
> > > Redundant use of New:
> > > ------------------------

> > > Dim cmd As New ADODB.Command
> > > Dim rs As New ADODB.Recordset     'redundant New

> > > ...
> > > Set rs = cmd.Execute     'for this
> > > ...
> > > rs.Close

> > > --------------------------------------------------------
> > > This is Ok:
> > > -----------
> > > Dim rs As New ADODB.Recordset

> > > rs.Open "SELECT * FROM Authors", "DSN=AdoDemo;UID=ad;PWD=;"
> > > ...
> > > rs.Close
> > > Set rs = Nothing

> > > --------------------------------------------------------
> > > Thanks in advance
> > > Zsolt



Thu, 12 Jun 2003 07:38:46 GMT  
 redundant New: Dim rs As New ADODB.Recordset
Zsolt,
For best performance, always DIM your objects without NEW, then SET AS NEW
http://www.able-consulting.com/ADO_Faq.htm#Q34

--

Thanks,
Carl Prothman
Microsoft Visual Basic MVP
eCodeGen - Your on-line code generator
http://www.able-consulting.com/ecodegen/


Quote:
> Hi,

> What do you think?

> --------------------------------------------------------
> Redundant use of New:
> ------------------------

> Dim cmd As New ADODB.Command
> Dim rs As New ADODB.Recordset     'redundant New

> ...
> Set rs = cmd.Execute     'for this
> ...
> rs.Close

> --------------------------------------------------------
> This is Ok:
> -----------
> Dim rs As New ADODB.Recordset

> rs.Open "SELECT * FROM Authors", "DSN=AdoDemo;UID=ad;PWD=;"
> ...
> rs.Close
> Set rs = Nothing

> --------------------------------------------------------
> Thanks in advance
> Zsolt



Thu, 12 Jun 2003 13:13:34 GMT  
 redundant New: Dim rs As New ADODB.Recordset
I didn't realy get your question but the correct way to declare a recordset
is:

Dim rs as ADODB.Recordset

Set rs = New ADODB.Recordset

If you declare the recordset using  Dim rs as New ADODB.Recordsetm, VB
checks if the object is New everytime the object is used.


Quote:
> Hi,

> What do you think?

> --------------------------------------------------------
> Redundant use of New:
> ------------------------

> Dim cmd As New ADODB.Command
> Dim rs As New ADODB.Recordset     'redundant New

> ...
> Set rs = cmd.Execute     'for this
> ...
> rs.Close

> --------------------------------------------------------
> This is Ok:
> -----------
> Dim rs As New ADODB.Recordset

> rs.Open "SELECT * FROM Authors", "DSN=AdoDemo;UID=ad;PWD=;"
> ...
> rs.Close
> Set rs = Nothing

> --------------------------------------------------------
> Thanks in advance
> Zsolt



Sun, 15 Jun 2003 21:08:13 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Dim Rs as New ADODB.Recordset

2. Dim obj As New Class crt Dim Obj As Class = New Class

3. Dim something as NEW Adodb.Connection

4. PROB with "dim as new ADODB...."

5. PROB with "dim as new ADODB...."

6. PROB with "dim as new ADODB...."

7. dim a = new ADODB.Connection 'causing errors!

8. Dim as New vs. Set = New

9. How to put many new records to a ADODB.recordset

10. kyduke(kyduke@yahoo.com)'s New Logic - New sorting, New paging method

11. New New New

12. NEW NEW NEW Project...

 

 
Powered by phpBB® Forum Software