Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning a connection to a recordset, to set or not to set

Status
Not open for further replies.

frothytoad

Technical User
Aug 1, 2003
32
0
0
US
Here is an odd situation. I wrote a piece of code to open a new recordset. Starting with a previously opened connection:

Set MyConn = New ADODB.Connection
MyConn.ConnectionString = ConnString
MyConn.Open

the code to open the recordset looks like this:

Set rst = New ADODB.Recordset
rst.ActiveConnection = MyConn
rst.CursorLocation = adUseClient
rst.CursorType = adOpenKeyset
rst.LockType = LckType
rst.Source = SelectStatement
rst.Open

What surprised me was that I was expecting an error. MyConn is an object, and so I would have expected that I would need, instead of the second line above, a statement like:

SET rst.ActiveConnection = MyConn

But the code ran fine.

Did I just open a new connection using the connection string of MyConn, or did the new recordset truly open using the existing connection? Also, a second question would be: Is there a way to tell within VB which connection is being used?

Thanks...

-- Jeff

 
COM supports default properties and ADO makes good use of them.

For example you could do this:
[tt]MsgBox rst.Fields.Item("MyField").Value[/tt]

or you could do this:
[tt]MsgBox rst("MyField")[/tt]

and they would come out the same way...

The default property of the recordset object is the Fields collection. The default method of Fields is Item. Item returns a Field object and the default property of the Field object is Value.

It just so happens that the default property of the ADO Connection object is ConnectionString.

So you get the same thing if you use [tt]x = cn[/tt] or if you use [tt]x = cn.ConnectionString[/tt]

But if you use [tt]Set x = cn[/tt] then you get the object.
 
Thanks! That is what I suspected was happening, but one never knows with MS...

-- Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top