DAO_OCX.EXE ActiveX Control Uses DAO to Query Database
This article was previously published under Q152116 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 files are available for download from the Microsoft Download Center: Dao_ocx.exe (http://download.microsoft.com/download/vc40std/sample/6/win98/en-us/dao_ocx.exe) For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base: 119591 (http://support.microsoft.com/kb/119591/EN-US/) How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most
current virus-detection software that was available on the date that the file
was posted. The file is stored on security-enhanced servers that help to
prevent any unauthorized changes to the file.
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:
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.
| Article Translations
|

Back to the top
