|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
ADO: Opening A ConnectionThe Testing Department is calling again. The company is switching to using ActiveX Data Objectshow about setting up an ADO database-editing program? Already on it, you say. The first step in editing an ADO database is to open that database, which is called a data source in ADO terminology, by setting up a Connection object. To use that and other ADO objects in code, you use the Project|References item, select the Microsoft ActiveX Data Objects Library item, and click on OK, adding the ADO Object Library to your program. Now were free to create a new ADO Connection object with the Connection objects Open method: connection.Open ConnectionString [,UserID [, Password [, OpenOptions]]] Here are the arguments for this method:
Lets see an example. When we start our ADO code example, the adocode example (see A Full-Scale ADO Example in this chapter), well establish a connection, db, to the database we built in the previous chapter, db.mdb:
Private Sub Form_Load()
Dim db As Connection
Set db = New Connection
db.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data _
Source=C:\vbbb\adocode\db.mdb;"
...
End Sub
And thats itnow we have a connection to the data source. To actually work with the data in that data source, well create an ADO record set in the next topic. ADO: Creating A Record Set From A ConnectionNow that youve created an ADO connection, you can open a record set from that connection using the Recordset objects Open method: recordset.Open [Source, [ActiveConnection, [Type, [LockType, [Options]]]] Here are the arguments for this method:
Here are the possible values for the Type argument:
Here are the possible values for the LockType argument:
Here are the possible values for the Options argument:
Lets see an example. In our ADO code example, the adocode example (see A Full- Scale ADO Example in this chapter), we create a record set, adoRecordset, by first declaring it as a form-wide variable: Dim adoRecordset As Recordset Next, we select all the records in the students table this way when the form loads, using the Open method:
Private Sub Form_Load()
Dim db As Connection
Set db = New Connection
db.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data _
Source=C:\vbbb\adocode\db.mdb;"
Set adoRecordset = New Recordset
adoRecordset.Open "select Grade, Name from students", _
db, adOpenStatic, adLockOptimistic
...
End Sub
Now that weve opened our result set, we can bind that result set to various controls, like text boxes, as well do in the next topic.
|
|
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. |