Você está na página 1de 6

Using DAO and RDO to Access data

To Access Data using DAO


To demonstrate how you might put DAO to work, lets create a simple VB project to access the data stored in Microsoft's sample Northwind database. 1. Fire up VB and start a new project.

2. Go to Project References and select Microsoft DAO 3.6 Object Library, as shown in Figure D. (Depending on the version of VB you are using, you will have a corresponding DAO Object Library version, so if you don't have DAO 3.6, use an earlier version instead.) Figure D

DAO object library selected in Project References 3. Add two combo boxes (cboLastNameJet and cboLastNameODBCDirect) and two command buttons (cmdGetDataJet and cmdGetDataODBCDirect) to the form.

4.

Your screen should resemble the form shown in Figure E. Figure E

We've added some objects to our form. 5. 6. Add the code in to the cmdGetDataJet_Click() event. Add the code shown to the Private SubcmdGetDataODBCDirect_Click() event.

7. Modify strLocation to reflect the location use .mdb database and Set dbJet = wrkJet.OpenDatabase(strLocation &".mdb") to reflect the name of the database. 8. Modify strConn to reflect the DSN name and Password of a remote database.

9. Modify the query in Set rsODBCDirect = conODBCDirect.OpenRecordset("SELECT LastName FROM Employees", dbOpenDynamic) to reflect the query you'd like to run. 10. Press [Ctrl][F5] to run the project. 11. Click the Get Data Jet button and the Get Data ODBC Direct button to obtain data using Microsoft Jet and ODBCDirect, respectively. 12. You should see a screen like the one shown in Figure F. Figure F

To Access Data using DAO


RDO is designed to access remote ODBC relational data sources. It's smaller and faster than DAO and offers more flexibility in handling various types of result sets. Our look at data access solutions continues with an intro to RDO and a little code demo.Remote Data Objects RDO is an object-oriented...
Start a new VB project, and from the Components dialog box (invoked from the Project -> Components menu), select Microsoft RemoteData Control 6.0 (SP3) as shown below and click OK.

The RDO Data Control should appear in your toolbox as shown below:

Put a remote data control (RDC) on your form, and set the properties as follows:

Property Value Name rdoBiblio DataSourceName Biblio SQL select * from authors
Now put three text boxes on the form, and set their Name, DataSource, and DataField properties as follows:

Name txtAuthor txtAuID txtYearBorn

DataSource rdoBiblio rdoBiblio rdoBiblio

DataField Author Au_ID Year Born

Save and run the program. Notice how it works just like the other data control.

Now change the SQL property of the data control to select * from authors order by author and run the program again. Notice the difference. Change the SQL property back to what it was and add three command buttons to the form, and set their Name and Caption properties as follows:

Name cmdNameOrder cmdYearOrder cmdIDOrder

Caption Order by Name Order by Year Order by ID

Put the following code in the cmdNameOrder_Click event: rdoBiblio.SQL = "select * from authors order by author" rdoBiblio.Refresh

Put the following code in the cmdYearOrder_Click event: rdoBiblio.SQL = "select * from authors order by [year born]" rdoBiblio.Refresh

Put the following code in the cmdIDOrder_Click event: rdoBiblio.SQL = "select * from authors order by au_id" rdoBiblio.Refresh

Save and run the program and see what happens when you click the buttons.

A screen-shot of the sample app at run-time is shown

below:

Você também pode gostar