📄 vcg14.htm
字号:
<P>Access's Database Wizard lets you add and remove columns from a database and define the look and feel of the data entry forms. The columns can be customized to your application's needs. (Having the right mix of columns can be a sticky wicket: What's right for one user usually isn't adequate for another user.) You're not going to use the data entry forms, so the form style isn't relevant in this example. The database is called Address Book, and it contains two tables: Addresses and Switchboard. The Switchboard table is used to manage the forms in the Access version of this database and is of no use to a Visual C++ programmer. Ignore the Switchboard table, but don't remove it.
<BR>
<P>Once the database is created, it's a good idea to print a record of the Addresses table that is created using Access's Tools | Analyze | Documentor menu selection. This will provide you with a printed, permanent record of the Addresses table.
<BR>
<P>Using the Documentor utility, you will have a report that can be printed (or exported to a document). It will list each column's attributes, as shown in Listing 14.1.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 14.1. Access's Documentor output for two columns in the Addresses table.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">Columns
Name Type Size
AddressID Number (Long) 4
Allow Zero Length: False
Attributes: Fixed Size, Auto-Increment
Caption: Address ID
Collating Order: General
Column Hidden: False
Column Order: Default
Column Width: Default
Ordinal Position: 0
Required: False
Source Field: AddressID
Source Table: Addresses
FirstName Text 50
Allow Zero Length: False
Attributes: Variable Length
Caption: First Name
Collating Order: General
Column Hidden: False
Column Order: Default
Column Width: Default
Ordinal Position: 2
Required: False
Source Field: FirstName
Source Table: Addresses</FONT></PRE>
<P>The entire listing for the Addresses table is only six pages long, so it's easily printed. When exported as a Word document (in RTF format, readable by Windows 95's WordPad program), it's saved as doc_rptObjects.rtf in your My Documents directory. This report appears in the CHAPTR14 directory on the CD that comes with this book so that you can peruse it without having to run Access.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>Although the Access Database Wizard creates a complete database application (a basic application, however) when using DAO to create databases, you will create only the database, not an application. For example, DAO typically isn't used to create forms that may be part of an Access database.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Once you've created your application's database, you can use Visual C++'s AppWizard to create the database application itself.
<BR>
<BR>
<A NAME="E69E204"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Using AppWizard to Create a DAO Application</B></FONT></CENTER></H4>
<BR>
<P>Once you've created an initial database for your application, you can use AppWizard to create the DAO application's shell. You should follow typical database application creation steps, making sure to select a data source in the AppWizard database support wizard dialog (Step 2 of 6), as shown in Figure 14.1.
<BR>
<P><B><A HREF="14vcg01.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/14vcg01.gif">Figure 14.1. AppWizard's Step 2 of 6 database support wizard dialog.</A></B>
<BR>
<P>Clicking the Data Source button will display the Database Options dialog box, shown in Figure 14.2, in which you should select DAO as the datasource. Click the ... button.
<BR>
<P><B><A HREF="14vcg06.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/14vcg06.gif">Figure 14.2. The Database Options dialog box.</A></B>
<BR>
<P>A select files dialog box appears, as shown in Figure 14.3, listing Access database files.
<BR>
<P><B><A HREF="14vcg02.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/14vcg02.gif">Figure 14.3. The DAO Open dialog box.</A></B>
<BR>
<P>After you select an Access database, you must select a table to use. After you close the Database Options dialog box, you will see the Select Database Tables dialog box, shown in Figure 14.4. It will have a list of all the tables that are contained within the database that was selected. For this example, there are two tables. You will be working with the Addresses table exclusively.
<BR>
<P><B><A HREF="14vcg03.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/14vcg03.gif">Figure 14.4. Selecting the Addresses table in the Select Database Tables dialog box.</A></B>
<BR>
<P>After you've selected your DAO Access database and table (variables will be bound for each column in the selected table), you can set whatever other options you would like your application to support. For most applications, you should include support for OLE controls, because several of the stock OLE controls are most useful for database applications.
<BR>
<P>After generating your application, you should do a test build to make sure that the creation was successful.
<BR>
<BR>
<A NAME="E69E205"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Binding Variables to Dialog Controls</B></FONT></CENTER></H4>
<BR>
<P>The second step in a DAO program is to bind variables to the user interface dialog controls. A DAO program uses a CDaoRecordView class object for a user interface. The CDaoRecordView object works much like the standard ODBC CRecordView object: Design and lay out a dialog box with controls to match the database table's columns.
<BR>
<P>In this example (a simple address book application), you will affix a number of controls to the dialog box (you won't use every column in the database table) to create a basic functional program. Later, you can add dialog boxes for other, infrequently used controls if you want to.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>There are other alternatives to using a separate dialog box for infrequently used fields that would otherwise clutter up a good user interface. One alternative is to use an MDI model. One window would service the main columns, and other windows would hold other, infrequently used columns in the database. A second alternative would be a tab dialog (like Visual C++'s options dialog boxes).</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Figure 14.5 shows the dialog box controls that are used to allow the user to enter (and retrieve) data in your database.
<BR>
<P><B><A HREF="14vcg04.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/14vcg04.gif">Figure 14.5. A typical user interface for a DAO program.</A></B>
<BR>
<P>As soon as you've created your user interface, you can go on to the more advanced parts of your programming. Since this chapter is about using DAO, it doesn't cover topics such as printing, but you will need to add report generation facilities to virtually any application you create.
<BR>
<BR>
<A NAME="E69E206"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Implementing the File Menu</B></FONT></CENTER></H4>
<BR>
<P>One primary functionality you need is to be able to handle the application's File menu. In a typical application, you would be able to open different files (or databases) and create a new file (database) for the user.
<BR>
<BR>
<A NAME="E70E68"></A>
<H5 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Opening and Closing a DAO Access Database</B></FONT></CENTER></H5>
<BR>
<P>Opening (and closing for MDI applications, if applicable) a database file using DAO is a simple process. In the sample program, you close your database by adding a handler in your document class (CAddressDoc in the sample program) for the menu selections New and Open. Since the sample program is SDI, you don't implement a Close menu item. The Open selection will close an open database before you open a new one.
<BR>
<P>Since default DAO applications created using AppWizard have a "hardwired" default database name and SQL parameter, you must first change these constants to CString class variables. Here's an example of what AppWizard creates (the constants appear in bold):
<BR>
<PRE>
<FONT COLOR="#000080">CAddressesSet::CAddressesSet(CDaoDatabase* pdb)
: CDaoRecordset(pdb)
{
//{{AFX_FIELD_INIT(CAddressesSet)
m_AddressID = 0;
m_FirstName = _T("");
m_LastName = _T("");
m_SpouseName = _T("");
m_Address = _T("");
m_City = _T("");
m_StateOrProvince = _T("");
m_PostalCode = _T("");
m_Country = _T("");
m_EmailAddress = _T("");
m_HomePhone = _T("");
m_WorkPhone = _T("");
m_WorkExtension = _T("");
m_MobilePhone = _T("");
m_FaxNumber = _T("");
m_SendCard = FALSE;
m_Nickname = _T("");
m_Notes = _T("");
m_Hobbies = _T("");
m_nFields = 20;
//}}AFX_FIELD_INIT
m_nDefaultType = dbOpenDynaset;
}
CString CAddressesSet::GetDefaultDBName()
{
return <B>_T("I:\\Database Developers Guide with Visual C++ 4"</B>
<B> "\\Source CD\\CHAPTR14\\Address Book.mdb")</B>;
}
CString CAddressesSet::GetDefaultSQL()
{
return <B>_T("[Addresses]")</B>;
}</FONT></PRE>
<P>You make your database and SQL strings modifiable by adding two CStrings to your document class and initializing them as shown in the following code fragment (changes appear in bold):
<BR>
<PRE>
<FONT COLOR="#000080">CAddressesSet::CAddressesSet(CDaoDatabase* pdb)
: CDaoRecordset(pdb)
{
//{{AFX_FIELD_INIT(CAddressesSet)
m_AddressID = 0;
m_FirstName = _T("");
m_LastName = _T("");
m_SpouseName = _T("");
m_Address = _T("");
m_City = _T("");
m_StateOrProvince = _T("");
m_PostalCode = _T("");
m_Country = _T("");
m_EmailAddress = _T("");
m_HomePhone = _T("");
m_WorkPhone = _T("");
m_WorkExtension = _T("");
m_MobilePhone = _T("");
m_FaxNumber = _T("");
m_SendCard = FALSE;
m_Nickname = _T("");
m_Notes = _T("");
m_Hobbies = _T("");
m_nFields = 20;
//}}AFX_FIELD_INIT
m_nDefaultType = dbOpenDynaset;
<B> m_DefaultDBName = _T("I:\\Database Developers Guide with Visual C++ 4"</B>
<B> "\\Source CD\\CHAPTR14\\Address Book.mdb");</B>
<B> m_DefaultSQL = _T("[Addresses]");</B>
}
CString CAddressesSet::GetDefaultDBName()
{
<B> return m_DefaultDBName;</B>
}
CString CAddressesSet::GetDefaultSQL()
{
<B> return m_DefaultSQL;</B>
}</FONT></PRE>
<P>Don't forget to add m_DefaultDBName and m_DefaultSQL to your class definition's header file. This way, you can close the current database and then open a new one by modifying DefaultDBName and calling the CDaoRecordset::Open() function. First you create a handler for the File | Open menu selection in the CAddressesDoc class.
<BR>
<P>For example, in your document class, your handler for opening a new database is implemented as the following code fragment shows. To create this functionality, you must use ClassWizard to add a handler for the File | Open menu selection and then add code to that handler:
<BR>
<PRE>
<FONT COLOR="#000080">void CAddressesDoc::OnFileOpen()
{
// TODO: Add your command handler code here
<B>// The m_addressSet member variable is a pointer to the CAddressSet class.</B>
<B>// First, get a database file to be opened!</B>
<B> CFileDialog dlg(TRUE,</B>
<B> NULL,</B>
<B> "*.mdb",</B>
<B> OFN_OVERWRITEPROMPT,</B>
<B> "Access Database Files (*.mdb) | *.mdb | "</B>
<B> "All Files (*.*) | *.* ||");</B>
<B> if (dlg.DoModal())</B>
<B> {// User did not cancel the open file dialog:</B>
<B> TRACE("File selected is '%s'\n", dlg.GetPathName());</B>
<B> if (m_addressesSet.IsOpen())</B>
<B> {// If open (avoid errors and crashes!), then close:</B>
<B> m_addressesSet.Close();</B>
<B> if (m_addressesSet.m_pDatabase != NULL)</B>
<B> {// Close the DB too. If not closed, old DB is reopened!</B>
<B> m_addressesSet.m_pDatabase->Close();</B>
<B> }</B>
<B> }</B>
<B> // Save the user's database filename and then (re)open:</B>
<B> m_addressesSet.m_DefaultDBName = dlg.GetPathName();</B>
<B> m_addressesSet.Open(AFX_DAO_USE_DEFAULT_TYPE);</B>
<B> // Refresh the view to reflect the *new* database's data:</B>
<B> CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);</B>
<B> CView * pView = pFrame->GetActiveView();</B>
<B> if (pView)</B>
<B> {</B>
<B> pView->UpdateData(FALSE);</B>
<B> }</B>
<B> }</B>
<B> else</B>
<B> {// User aborted. Do nothing, or clean up if appropriate.</B>
<B> }</B>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -