CreganTur
07-10-2008, 05:55 AM
I've got a best practice question regarding ADO connections when working with ADODB.Recordsets.
I've got 2 procedures that accomplish the same thing. The only difference is that one of them opens an ADODB.Connection, and then the Recordset, while the second procedure opens just the Recordset.
This version opens a Connection, and then opens the Recordset
Sub RecSetOpen2()
'exports entire recordset into .dat file
Dim rst As ADODB.Recordset
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\Acc07_ByExample\Northwind.mdb"
Set rst = New ADODB.Recordset
With rst
.Open "SELECT * FROM Customers", _
conn, adOpenForwardOnly
.Save "C:\Acc07_ByExample\MyRst.dat"
.Close
End With
Set rst = Nothing
End Sub
This version opens just the Recordset:
Sub RecSetOpen()
'exports entire recordset into .dat file
Dim rst As ADODB.Recordset
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\Acc07_ByExample\Northwind.mdb"
Set rst = New ADODB.Recordset
With rst
.Open "SELECT * FROM Customers", _
strConnection, adOpenForwardOnly
.Save "C:\Acc07_ByExample\MyRst.dat"
.Close
End With
Set rst = Nothing
End Sub
Is there any benefit to opening a Connection, as shown in the first procedure? To me it seems like extraneous code, since the exact same thing can be accomplished by just opening the recordset directly, as shown in the second procedure.
This may be one of those 'personal choice' issues, but I do want to know what best practice would be.
I've cross posted this issue here: http://www.proofficedev.com/members/forums/showthread.php?p=5428#post5428
I've got 2 procedures that accomplish the same thing. The only difference is that one of them opens an ADODB.Connection, and then the Recordset, while the second procedure opens just the Recordset.
This version opens a Connection, and then opens the Recordset
Sub RecSetOpen2()
'exports entire recordset into .dat file
Dim rst As ADODB.Recordset
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\Acc07_ByExample\Northwind.mdb"
Set rst = New ADODB.Recordset
With rst
.Open "SELECT * FROM Customers", _
conn, adOpenForwardOnly
.Save "C:\Acc07_ByExample\MyRst.dat"
.Close
End With
Set rst = Nothing
End Sub
This version opens just the Recordset:
Sub RecSetOpen()
'exports entire recordset into .dat file
Dim rst As ADODB.Recordset
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\Acc07_ByExample\Northwind.mdb"
Set rst = New ADODB.Recordset
With rst
.Open "SELECT * FROM Customers", _
strConnection, adOpenForwardOnly
.Save "C:\Acc07_ByExample\MyRst.dat"
.Close
End With
Set rst = Nothing
End Sub
Is there any benefit to opening a Connection, as shown in the first procedure? To me it seems like extraneous code, since the exact same thing can be accomplished by just opening the recordset directly, as shown in the second procedure.
This may be one of those 'personal choice' issues, but I do want to know what best practice would be.
I've cross posted this issue here: http://www.proofficedev.com/members/forums/showthread.php?p=5428#post5428