HOWTO: Create a VB Component that Returns a Recordset in RDSID: Q166277
|
There are two ways to pass a recordset back from your server to the client with Remote Data Service (RDS). One is to use RDS DataFactory, and the second way is to create a custom ActiveX DLL. This article describes both methods.
<HTML>
<HEAD></HEAD>
<BODY>
<!-- RDS.DataSpace -->
<OBJECT ID="RDS1" WIDTH=1 HEIGHT=1
CLASSID="CLSID:BD96C556-65A3-11D0-983A-00C04FC29E36">
</OBJECT>
<SCRIPT LANGUAGE="VBScript">
Option Explicit
Sub Window_OnLoad()
dim RDF1
dim myRS
set RDF1 = RDS1.CreateObject("RDSServer.DataFactory", _
"http://<server name>")
set myRS = RDF1.Query("DSN=pubs;UID=sa;PWD=;", _
"select * from Authors")
MsgBox myRS.Fields("au_lname")
End Sub
</SCRIPT>
</BODY>
</HTML>
In this example you are going to create a Visual Basic ActiveX DLL that is installed on the server and runs under RDS from Internet Explorer or a Visual Basic client application. In these steps assume that your Visual Basic development computer is also your Internet Information Server (IIS)/RDS server, so you do not have to cover the steps of Visual Basic application distribution. If you are not working from your IIS server, then you just need to correctly register and mark the .dll file safe for launching, as explained later in this document.
Public Function SumValues(lngVal1 As Integer, lngVal2 As Integer) _
As Integer
'This procedure is to test for minimum functionality.
SumValues = lngVal1 + lngVal2
End Function
Public Function ExecuteSQL(strConnect As Variant, strSQL As Variant)
_ As Variant
'Executes an action query, returns RecordsAffected.
On Error GoTo ehExecuteSQL
Dim cn As New ADODB.Connection
cn.Open strConnect
cn.BeginTrans 'Begin a transaction.
cn.Execute strSQL, ExecuteSQL 'RecordsetAffected is returned.
cn.CommitTrans 'No errors, commit.
Exit Function
ehExecuteSQL:
'If transaction is not committed, it will be rolled back.
ExecuteSQL = -2 '-2 indicates error condition.
End Function
Public Function ReturnRs(strConnect As Variant, strSQL As Variant) _
As ADODB.Recordset
'Returns an ADODB recordset.
On Error GoTo ehGetRecordset
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.Open strConnect
'These are not listed in the typelib.
rs.CursorLocation = adUseClient
'Using the Unspecified parameters, an ADO/R recordset is returned.
rs.Open strSQL, cn, _
adOpenUnspecified, adLockUnspecified, adCmdUnspecified
Set ReturnRs = rs
Exit Function
ehGetRecordset:
Err.Raise Err.Number, Err.Source, Err.Description
End Function
Dim rs As Object 'ADO DB Recordset
Dim rds As Object 'RemoteDataSpace
Dim bo As Object 'Business object
Private Sub Form_Load()
'Un-comment the next line to test locally.
Set bo = CreateObject("RDSTestObj.Class1") 'For local component.
'Un-comment the next 3 lines to test over HTTP.
'Set rds = CreateObject("RDS.DataSpace")
'Set bo = rds.CreateObject("RDSTestObj.Class1", _
' " <http://[SERVER]> ")
End Sub
Private Sub Command1_Click()
'Minimum functionality test.
MsgBox bo.SumValues(2, 3)
End Sub
Private Sub Command2_Click()
'Return a recordset.
'NOTE: Change the Dsn, Uid, Pwd to match yours.
Set rs = bo.ReturnRs("dsn=pubs;uid=sa;pwd=", _
"select * from authors")
List1.Clear
Debug.Print rs(0)
While Not rs.EOF
List1.AddItem rs("au_lname")
rs.movenext
Wend
End Sub
Private Sub Command3_Click()
'Execute SQL within a transaction.
'NOTE: Change the Dsn, Uid, Pwd to match yours.
Dim strSQL As Variant, lngRetVal As Long
strSQL = "Update authors set au_lname = au_lname + 'x' " & _
"Where au_id Like '172-32-1176'"
lngRetVal = bo.ExecuteSQL("dsn=pubs;uid=sa;pwd=", strSQL)
MsgBox "RecordsAffected: " & CStr(lngRetVal) & " (-2 is an error)"
End Sub
REGEDIT4
;This entry should be on one line
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC
\Parameters\ADCLaunch\RDSTestObj.Class1]
REGEDIT4
;This entry should be on one line
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC
\Parameters\ADCLaunch\RDSTestObj.Class1]
If you want to later deploy this business object using DCOM instead of HTTP
there are some additional steps that need to be done. These steps differ
between RDS version 1.5 and RDS 2.0.
Remote Data Services version 1.5 Readme.txt file: \\[SERVER DIR]\Msadc\Docs\Readme.txt
Data Access Software Development Kit (SDK) version 2.0 DASDKReadme.txt
file: \\MSDASDK\doc\DASDKReadme.txt
ActiveX Data Objects version 2.0 ADOReadme.txt file: \\[SERVER
DIR]\ado\ADOreadme.txt
Remote Data Services version 1.5 Help: \\[SERVER
DIR]\Msadc\Docs\default.htm
Data Access Software Development Kit (SDK) Help: \\MSDASDK\doc\dasdk.chm
For additional information, please see the following articles in the
Microsoft Knowledge Base:
ARTICLE-ID: Q181092
TITLE : FAQ: Remote Data Service (RDS) Frequently Asked Questions
For additional information, please see the following World Wide Web URL:
http://microsoft.com/data/rds/
For additional information, please see the following public newsgroup:
microsoft.public.ado.rds
Additional query words: MDAC ADC kbRDS150 kbRDS200 kbClient kbDCOM kbNetwork
Keywords : iisMisc
Version : WINDOWS:1.5,2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: June 11, 1999