Microsoft KB Archive/165297

= HOWTO: Use RDS from Inside a Visual Basic Program =

Article ID: 165297

Article Last Modified on 3/2/2005

-

APPLIES TO


 * Microsoft Remote Data Services 2.0
 * Microsoft Remote Data Services 1.5
 * Microsoft Remote Data Services 2.1
 * Microsoft Remote Data Services 2.1
 * Remote Data Service for ADO 2.5
 * Remote Data Service for ADO 2.6

-



This article was previously published under Q165297



SUMMARY
Microsoft Remote Data Service (RDS) can be used inside a Visual Basic program as easily as it can be used from inside Microsoft Internet Explorer. You cannot bind controls to it with the RDS.DataControl as you can in Internet Explorer, but you can use the RDS.DataSpace and the RDSServer.DataFactory objects to return a recordset to your Visual Basic program with a small amount of code. With the returned recordset you can populate unbound controls.



Sample Program
This example uses the CreateObject method of the RDS.DataSpace on the client to create a new instance of the RDSServer.DataFactory object on a remote server over HTTP. The RDSServer.DataFactory object then uses the Query method to execute SQL on the remote server and return an ADO/R recordset to the client.

In order to run the example code you first need to make sure the RDS Server components have been installed on the Internet Information Server (IIS) computer. You can download these from the Microsoft Universal Data Access web page located at the following Microsoft Web site:

http://msdn.microsoft.com/dataaccess

The client components must also be installed on the client computers. If the client computer has Internet Explorer 4.01 or later installed, the RDS 1.5 (or later) client components are included. For clients running Internet Explorer 3.0, the client components must be downloaded to the clients computer. One way of doing this is by using a CODEBASE parameter in the OBJECT tag of an HTML page. To do this go to the sample page on your RDS Server located at:

http://[SERVER]/Msadc/Samples/Adctest.asp

The components will automatically be installed on the client.

Before you run this code, first make change the security settings. For additional information about configuration of RDS on the server side, click the following article number to view the article in the Microsoft Knowledge Base:

250536 HOWTO: Configure RDS for Windows 2000

184606 HOWTO: Use RDS From an IIS 4.0 Virtual Server

230680 INFO: Working with RDS Handlers

251122 Troubleshooting Common Problems with Remote Data Services

This sample uses SQL Server and the Pubs database, but you can change the System DSN, UID, PWD, and SQL to match any ODBC database that you use.

Note You must change Username= and PWD= to the correct values before you run this code. Make sure that Username has the appropriate permissions to perform this operation on the database.  Start a new project in Visual Basic and choose "Standard EXE." Form1 is created by default. Add two Command buttons, named Command1 and Command2, to Form1.  Paste the following code into the General Declarations section of Form1: Dim rs As Object  'Recordset Dim ds As Object  'RDS.DataSpace Dim df As Object  'RDSServer.DataFactory

Private Sub Form_Load Set ds = CreateObject("RDS.DataSpace") Set df = ds.CreateObject("RDSServer.DataFactory", _  "http://myserver") End Sub

Private Sub Command1_Click 'This query returns a recordset over HTTP. Dim strCn As Variant, strSQL As Variant strCn = "dsn=pubs;Username= ;PWD= " strSQL = "select * from authors" Set rs = df.Query(strCn, strSQL) Debug.Print rs(0)    'Print Row 1, Col 1 to Debug window End Sub

Private Sub Command2_Click 'This example executes an action query but does not return 'a recordset. Dim strCn As Variant, strSQL As Variant strCn = "dsn=pubs;Username= ;PWD= " strSQL = "Update authors Set au_fname = 'Jon' Where au_lname" _ & " Like 's%'" df.Query strCn, strSQL End Sub 

Note You must change the DSN to match a System DSN on your IIS/ASP/RDS server before you continue. You also must change the IIS/RDS server name.
 * 1) Start the program or press the F5 key.
 * 2) Click the Command1 button to execute the RDS code that returns a recordset. If it executes successfully, row 1, column 1 will be output to the Debug Immediate window.
 * 3) Click the Command2 button to execute the action query that updates the au_fname column in the authors table. No data is returned from the action query.

