SAMPLE: DAO_OCX.EXE ActiveX Control Uses DAO to Query Database

ID: Q152116


The information in this article applies to:


SUMMARY

This sample demonstrates querying and displaying the result of the query using MFC's CDao classes from an ActiveX control (OLE Control.)

The DaoOcx control utilizes a saved query in the CUSTOMER.MDB database provided with the sample. This database consists of phone numbers and their associated addresses. Additionally, there may be several people at each address and each individual may be associated with a note.

When the FindNumber() method is called from the client, it queries the database for the currently-entered phone number using the saved query ByPhone, retrieves the data, and then populates the fields with the address. It also populates a List Box with the names of the people at that address. Once the user selects one of the names, it places the note associated with that individual, if there is one, in the note edit control, and then fires an event.

The client application should call the ConnectDataBase method initially and must call the CleanUp method prior to shutdown. In the Visual Basic portion of this sample, the ConnectDataBase call is made in the Form's Load procedure and the CleanUp call is made in the Form's Terminate procedure.

The database name and the name of the saved query are properties of the control and can be set. If the property sheet is used to set the properties, then validation is done. No validation is performed if the properties are not entered using the property sheet provided.

The following file is available for download from the Microsoft Software Library:

~ Dao_ocx.exe
For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online Services


MORE INFORMATION

Once you have unzipped the file, you should have the actual database, CUSTOMER.MDB, that includes the saved query ByPhone. This database was created using Access 2.0 and may also be accessed using Access 95. Also included are the files for the control and a Visual Basic 4.0 (32- bit) project and form that demonstrates how to use the control. You must build and register the control, and then set the location of the CUSTOMER.MDB file in the DaoOcx control on the Visual Basic form before running the Visual Basic client.

Once the client is running, enter one of the following phone numbers (555) 555-5555, (526) 874-4632 or (816) 547-1234. Click the Find button to see some sample data. Once the names are visible, select one and see the note associated with that individual. You will also see a message box caused by an event fired from the control indicating that the data is complete.

The control uses a CDaoRecordSet derived class that is used to query the database. This class can be generated by hand, or you can use the AppWizard to create it as follows:

  1. From the File menu, select New. Select Project Workspace, and then select MFC AppWizard.


  2. Select an MDI application and Database support.


  3. Press the Data Source button.


  4. Select the DAO button.


  5. Select the CUSTOMER.MDB file in the project directory as the database.


AppWizard will create a CDaoRecordSet derived class that can be used almost directly. The only changes needed to be made to the CDaoRecordSet derived class are as follows:

  1. Modify the constructor to take a pointer to the control.


  2. Modify the GetDefaultDBName() function to call the control and retrieve the database name.


  3. Modify the GetDefaultSQL() function to call the control and retrieve the query name.


When Dao is used in a DLL, you must explicitly call AfxDaoTerm() prior to the call to ExitInstance() or you will receive an assertion. In the sample, this behavior is accomplished by calling the automation method, CleanUp, that closes any of the database access items and calls AfxDaoTerm(). MFC will call AfxDaoInit() to initialize the Jet Engine, but because you are calling from a DLL, the "CleanUp" method must call AfxDaoTerm() directly.

The CDao classes are very similar in usage to the ODBC database classes except that the Dao classes only access the Jet Engine, while the ODBC classes use the ODBC layer to allow access to a wide range of database types.

The CDao classes are wrappers around the Dao COM interfaces. For example, the CDaoQueryDef is a wrapper for the DAOQueryDef interface and the class has a member variable, m_pDAOQueryDef, that is a pointer to the interface.

Each object in the hierarchy contains an enumeration interface for listing the next lower interface in the hierarchy. For example, m_pDAOQueryDef (DAOQueryDef*) can be used to call QueryInterface for the DAOFields interface used to enumerate the fields in that query. The DAODatabase interface can be used to get the DAOQueryDefs interface used to list the available queries in the database.

Calling Open on a variable of type CDaoRecordSet causes the SQL query to be generated and the field variables to be filled.

You must call Close on any CDao object (interface) when you are done using it.

Additional query words: 4.10


Keywords          : kbsample CodeSam 
Version           : 4.10
Platform          : NT WINDOWS 
Issue type        : 

Last Reviewed: July 28, 1999