|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
RDO: Opening A ConnectionTo open an RDO connection to a database, you can use the RDO OpenConnection method. OpenConnection is a method of the rdoEnvironment object, and youll find a collection of those objects in the rdoEngine objects rdoEnvironments collection. To add the RDO objects to a program, select the Project|References menu item in Visual Basic, select the Microsoft Remote Data Object entry in the References dialog box, and click on OK. Now were free to use rdoEnvironment methods like OpenConnection: workspace.OpenConnection(datasource, [prompt, [read-only, [connect, _ [options]]]]) Here are the arguments to OpenConnection:
Lets see an example. In our RDO code example, the rdocode project (see A Full-Scale RDO Example earlier in this chapter), we create an rdoEnvironment object named re this way when the form loads:
Dim re As Object
Private Sub Form_Load()
Set re = rdoEngine.rdoEnvironments(0)
...
End Sub
Now we open a connection named db to the ODBC source (we set up this ODBC source in the previous chapter) this way:
Dim re As Object
Dim db As rdoConnection
Private Sub Form_Load()
Set re = rdoEngine.rdoEnvironments(0)
Set db = re.OpenConnection("db")
End Sub
Thats itnow we have a connection to our ODBC data source in the rdoConnection object named db. How do we access the records in that source? Well look into that next. RDO: Creating A Result SetAfter opening an ODBC data source and creating an rdoConnection object, we can create an RDO result set to start working with the records in that data source. To create a result set, we can use the rdoConnection method OpenResultset: Set resultset = rdoConnection.OpenResultset ( name, [type, [locktype,_ [ options]]]) Here are the arguments for OpenResultset:
Here are the possible values for the type argument:
Lets see an example. Here, well create an SQL-based result set in our RDO code example, the rdocode project (see A Full-Scale RDO Example earlier in this chapter), when the form loads, using the rdoConnection object weve createddb. In this case, well set up an SQL statement, SQLSel, to place all the fields from the data sources table named students in the result set:
Dim re As Object
Dim db As rdoConnection
Dim SQLSel As String
Private Sub Form_Load()
SQLSel = "Select * from students"
Set re = rdoEngine.rdoEnvironments(0)
Set db = re.OpenConnection("db")
...
Now we use OpenResultset to create an rdoResultset object, resultset:
Dim re As Object
Dim db As rdoConnection
Dim resultset As rdoResultset
Dim SQLSel As String
Private Sub Form_Load()
SQLSel = "Select * from students"
Set re = rdoEngine.rdoEnvironments(0)
Set db = re.OpenConnection("db")
Set resultset = db.OpenResultset(SQLSel, rdOpenKeyset)
...
Now that weve opened a result set, we can use rdoResultset methods like MoveFirst to move to the first record and display the data in that records Name and Grade fields with the rdocode projects text boxes, Text1 and Text2:
Dim re As Object
Dim db As rdoConnection
Dim resultset As rdoResultset
Dim SQLSel As String
Private Sub Form_Load()
SQLSel = "Select * from students"
Set re = rdoEngine.rdoEnvironments(0)
Set db = re.OpenConnection("db")
Set resultset = db.OpenResultset(SQLSel, rdOpenKeyset)
resultset.MoveFirst
Text1.Text = resultset("Name")
Text2.Text = resultset("Grade")
End Sub
And thats itweve opened an RDO result set and displayed some of the data in that result set.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |