Using DAO and RDO to Access Data

Embed Size (px)

Citation preview

  • 8/3/2019 Using DAO and RDO to Access Data

    1/6

    Using DAO and RDO to Accessdata

    To Access Data using DAO

    To demonstrate how you might put DAO to work, lets create a simple VB project to accessthe 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 inFigure D. (Depending on the version of VB you are using, you will have a correspondingDAO Object Library version, so if you don't have DAO 3.6, use an earlier versioninstead.)

    Figure D

    DAO object library selected in Project References

    3. Add two combo boxes (cboLastNameJetand cboLastNameODBCDirect) and twocommand buttons (cmdGetDataJetand cmdGetDataODBCDirect) to the form.

  • 8/3/2019 Using DAO and RDO to Access Data

    2/6

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

    Figure E

    We've added some objects to our form.

    5. Add the code in to the cmdGetDataJet_Click() event.

    6. Add the code shown to thePrivate SubcmdGetDataODBCDirect_Click() event.

    7. ModifystrLocation 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("SELECTLastName 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 usingMicrosoft Jet and ODBCDirect, respectively.

    12. You should see a screen like the one shown in Figure F.

    Figure F

  • 8/3/2019 Using DAO and RDO to Access Data

    3/6

    To Access Data using DAORDO is designed to access remote ODBC relational data sources. It's smaller and faster thanDAO and offers more flexibility in handling various types of result sets. Our look at data accesssolutions continues with an intro to RDO and a little code demo.Remote Data Objects RDO is anobject-oriented...

    Start a new VB project, and from the Components dialog box (invoked from the Project -> Componentsmenu), 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:

  • 8/3/2019 Using DAO and RDO to Access Data

    4/6

    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 DataSource DataField

    txtAuthor rdoBiblio Author

    txtAuID rdoBiblio Au_ID

    txtYearBorn rdoBiblio Year Born

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

  • 8/3/2019 Using DAO and RDO to Access Data

    5/6

    Now change the SQL property of the data control to select * from authors order by authorandrun the program again. Notice the difference.

    Change the SQL property back to what it was and add three command buttons to the form, and settheir Name and Caption properties as follows:

    Name Caption

    cmdNameOrder Order by NamecmdYearOrder Order by Year cmdIDOrder Order by ID

    Put the following code in the cmdNameOrder_Clickevent:

    rdoBiblio.SQL = "select * from authors order by author"

    rdoBiblio.Refresh

    Put the following code in the cmdYearOrder_Clickevent:

    rdoBiblio.SQL = "select * from authors order by [year born]"

    rdoBiblio.Refresh

    Put the following code in the cmdIDOrder_Clickevent:

    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.

  • 8/3/2019 Using DAO and RDO to Access Data

    6/6

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

    below: