Previous PageNext Page

9.8.3 ActiveConnection Property

Specifies which Connection object the Recordset object currently belongs.
 

Return Values

Sets or returns a String containing the definition for a connection or a Connection object. Default is a Null object reference.
 

Remarks

Use the ActiveConnection property to determine the Connection object over which the specified Command object will execute or the specified Recordset will be opened.
 
For open Recordset objects or for Recordset objects whose Source property is set to a valid Command object, the ActiveConnection property is read-only. Otherwise, it is read/write.
 
You can set this property to a valid Connection object or to a valid connection string. In this case, the provider creates a new Connection object using this definition and opens the connection. Additionally, the provider may set this property to the new Connection object to give you a way to access the Connection object for extended error information or to execute other commands.
 
If you use the ActiveConnection argument of the Open method to open a Recordset object, the ActiveConnection property will inherit the value of the argument.
 
If you set the Source property of the Recordset object to a valid Command object variable, the ActiveConnection property of the Recordset inherits the setting of the Command object's ActiveConnection property.
 

Example

This Visual Basic example uses the ActiveConnection, CommandText, CommandTimeout, CommandType, Size, and Direction properties to execute a stored procedure:
 

Public Sub ActiveConnectionX()

Dim cnn1 As ADODB.Connection

Dim cmdByRoyalty As ADODB.Command

Dim prmByRoyalty As ADODB.Parameter

Dim rstByRoyalty As ADODB.Recordset

Dim rstAuthors As ADODB.Recordset

Dim intRoyalty As Integer

Dim strAuthorID As String

Dim strCnn As String

` Define a command object for a stored procedure.

Set cnn1 = New ADODB.Connection

strCnn = "driver={SQL Server};server=srv;" & _

"uid=sa;pwd=;database=pubs"

cnn1.Open strCnn

Set cmdByRoyalty = New ADODB.Command

Set cmdByRoyalty.ActiveConnection = cnn1

cmdByRoyalty.CommandText = "byroyalty"

cmdByRoyalty.CommandType = adCmdStoredProc

cmdByRoyalty.CommandTimeout = 15

` Define the stored procedure's input parameter.

intRoyalty = Trim(InputBox( _

"Enter royalty:"))

Set prmByRoyalty = New ADODB.Parameter

prmByRoyalty.Type = adInteger

prmByRoyalty.Size = 3

prmByRoyalty.Direction = adParamInput

prmByRoyalty.Value = intRoyalty

cmdByRoyalty.Parameters.Append prmByRoyalty

` Create a recordset by executing the command.

Set rstByRoyalty = cmdByRoyalty.Execute()

` Open the Authors table to get author names for display.

Set rstAuthors = New ADODB.Recordset

rstAuthors.Open "authors", strCnn, , , adCmdTable

` Print current data in the recordset, adding

` author names from Authors table.

Debug.Print "Authors with " & intRoyalty & _

" percent royalty"

Do While Not rstByRoyalty.EOF

strAuthorID = rstByRoyalty!au_id

Debug.Print , rstByRoyalty!au_id & ", ";

rstAuthors.Filter = "au_id = '" & strAuthorID & "'"

Debug.Print rstAuthors!au_fname & " " & _

rstAuthors!au_lname

rstByRoyalty.MoveNext

Loop

rstByRoyalty.Close

rstAuthors.Close

cnn1.Close

End Sub


Copyright © 2000 Chili!Soft

Previous PageTop Of PageNext Page