This example demonstrates the Source property by opening three Recordset objects based on different data sources.
'BeginSourceVB
'To integrate this code
'replace the data source and initial catalog values
'in the connection string
Public Sub SourceX()
Dim Cnxn As ADODB.Connection
Dim rstTitles As ADODB.Recordset
Dim rstPublishers As ADODB.Recordset
Dim rstPublishersDirect As ADODB.Recordset
Dim rstTitlesPublishers As ADODB.Recordset
Dim cmdSQL As ADODB.Command
Dim strCnxn As String
Dim strSQL As String
' Open a connection
Set Cnxn = New ADODB.Connection
strCnxn = "Provider=sqloledb;Data Source=MyServer;Initial Catalog=Pubs;User Id=sa;Password=; "
Cnxn.Open strCnxn
' Open a recordset based on a command object
Set cmdSQL = New ADODB.Command
Set cmdSQL.ActiveConnection = Cnxn
strSQL = "Select title, type, pubdate FROM Titles ORDER BY title"
cmdSQL.CommandText = strSQL
Set rstTitles = cmdSQL.Execute()
' Open a recordset based on a table
Set rstPublishers = New ADODB.Recordset
strSQL = "Publishers"
rstPublishers.Open strSQL, Cnxn, adOpenStatic, adLockReadOnly, adCmdTable
'rstPublishers.Open strSQL, Cnxn, , , adCmdTable
' the above two lines of code are identical
' Open a recordset based on a table
Set rstPublishersDirect = New ADODB.Recordset
rstPublishersDirect.Open strSQL, strCnxn, , , adCmdTableDirect
' Open a recordset based on an SQL string.
Set rstTitlesPublishers = New ADODB.Recordset
strSQL = "SELECT title_ID AS TitleID, title AS Title, " & _
"publishers.pub_id AS PubID, pub_name AS PubName " & _
"FROM publishers INNER JOIN Titles " & _
"ON publishers.pub_id = Titles.pub_id " & _
"ORDER BY Title"
rstTitlesPublishers.Open strSQL, strCnxn, , , adCmdText
' Use the Source property to display the source of each recordset.
MsgBox "rstTitles source: " & vbCr & _
rstTitles.Source & vbCr & vbCr & _
"rstPublishers source: " & vbCr & _
rstPublishers.Source & vbCr & vbCr & _
"rstPublishersDirect source: " & vbCr & _
rstPublishersDirect.Source & vbCr & vbCr & _
"rstTitlesPublishers source: " & vbCr & _
rstTitlesPublishers.Source
' clean up
Cnxn.Close
rstTitles.Close
rstPublishers.Close
rstTitlesPublishers.Close
Set Cnxn = Nothing
Set rstTitles = Nothing
Set rstPublishers = Nothing
Set rstTitlesPublishers = Nothing
End Sub
'EndSourceVB
Recordset Object | Source Property (ADO Recordset)
© 1998-2001 Microsoft Corporation. All rights reserved.