' Initialize variables.
Dim cn As New ADODB.Connection
Dim provStr As String
' Specify the OLE DB provider.
cn.Provider = "sqloledb"
' Specify connection string on Open method.
ProvStr = "Server=MyServer;Database=northwind;Trusted_Connection=yes"
cn.Open provStr
Только для иллюстрации ( Ссылка .):
Set rsOrders = New Recordset
rsOrders.Open "Select * from orders", cn
Do While Not rsOrders.EOF
'
' If the order matches some custom business logic then get the details for
' that order, without opening a new connection.
'
If SomeBusinessLogic(rsOrders("CustomerID")) Then
Dim rsDetails As Recordset
Set rsDetails = New Recordset
'
' Open a new recordset using the same connection. Normally it's not
' possible to have two recordsets simultaniously using the same
' connection, but MARS makes this possible
'
rsDetails.Open "Select sum(quantity * unitprice) as total " & _
"from [order details] " & _
"where OrderID=" & rsOrders("OrderID"), _
cn
grandTotal = grandTotal + rsDetails("total")
End If
rsOrders.MoveNext
Loop
lblTotalOrders = grandTotal