Creating Skeleton of Your Application
MFC AppWizard adds DAO support to your application by selecting option Database Support on AppWizard's page 2.
Select a project type MFC AppWizard(exe) and give your project name.

Select SDI Support.

Select Database view without file support option.

Click DAO radio button and select your access database. My database is "D:\mcb.krz" which is an access database. Here I have shown mcb.krz as mcb.mdb.

Select a table from your database.

Leave other AppWizard options as default and Click Finish. Build and Run the project. You should be able to run the project with no errors.
Under the Hood
Under the hood, AppWizard has added a class called CAddDaoSpSet. This class is derived from CDaoRecordset.
CAddDaoSpSet class
CAddDaoSpSet class is a CDaoRecordset derived class. Declaration header file looks like this:
class CAddDaoSpSet : public CDaoRecordset
{
public:
CAddDaoSpSet(CDaoDatabase* pDatabase = NULL);
DECLARE_DYNAMIC(CAddDaoSpSet)
// Field/Param Data
//{{AFX_FIELD(CAddDaoSpSet, CDaoRecordset)
CString m_Template;
CString m_SearchName;
CString m_Search;
//}}AFX_FIELD
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAddDaoSpSet)
public:
virtual CString GetDefaultDBName(); // REVIEW: Get a comment here
virtual CString GetDefaultSQL(); // default SQL for Recordset
virtual void DoFieldExchange(CDaoFieldExchange* pFX); // RFX support
//}}AFX_VIRTUAL
// Implementation
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
};
This class has a member variable corresponding to each column of the table. Besides this, it has three pure virtual functions with a default constructor, AssertValid, and Dump function. Here is implementation of this class:
// CAddDaoSpSet implementation
IMPLEMENT_DYNAMIC(CAddDaoSpSet, CDaoRecordset)
CAddDaoSpSet::CAddDaoSpSet(CDaoDatabase* pdb)
: CDaoRecordset(pdb)
{
//{{AFX_FIELD_INIT(CAddDaoSpSet)
m_Template = _T("");
m_SearchName = _T("");
m_Search = _T("");
m_nFields = 3;
//}}AFX_FIELD_INIT
m_nDefaultType = dbOpenDynaset;
}
CString CAddDaoSpSet::GetDefaultDBName()
{
return _T("D:\\mcb.krz");
}
CString CAddDaoSpSet::GetDefaultSQL()
{
return _T("[Hunt]");
}
void CAddDaoSpSet::DoFieldExchange(CDaoFieldExchange* pFX)
{
//{{AFX_FIELD_MAP(CAddDaoSpSet)
pFX->SetFieldType(CDaoFieldExchange::outputColumn);
DFX_Text(pFX, _T("[Template]"), m_Template);
DFX_Text(pFX, _T("[SearchName]"), m_SearchName);
DFX_Text(pFX, _T("[Search]"), m_Search);
//}}AFX_FIELD_MAP
}
// CAddDaoSpSet diagnostics
#ifdef _DEBUG
void CAddDaoSpSet::AssertValid() const
{
CDaoRecordset::AssertValid();
}
void CAddDaoSpSet::Dump(CDumpContext& dc) const
{
CDaoRecordset::Dump(dc);
}
#endif //_DEBUG
GetDefaultConnect returns the database name.
GetDefaultSQL returns the table name which you are connected to.
DoFieldExchange connects table fields to a member of the class. See CDaoRecordset class in MSDN for more details.
CAddDaoSpDoc Class Besides this, AppWizard has added a member variable in Doc class of application.
CAddDaoSpSet m_addDaoSpSet;
CAddViewSpView Class
View class has more additions than the doc. One variable of CDaoRecordset* type has added to the view class of the project.
CAddDaoSpSet* m_pSet;
Which is being initialized in the constructor:
CAddDaoSpView::CAddDaoSpView()
: CDaoRecordView(CAddDaoSpView::IDD)
{
//{{AFX_DATA_INIT(CAddDaoSpView)
m_pSet = NULL;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
OnInitialUpdate is overridden with this code:
void CAddDaoSpView::OnInitialUpdate()
{
m_pSet = &GetDocument()->m_addDaoSpSet;
CDaoRecordView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
and a new function OnGetRecordset has been added to the class which returns pointer to CDaoRecorset.
CDaoRecordset* CAddDaoSpView::OnGetRecordset()
{
return m_pSet;
}
Resource Additions
AppWizard has added a dialog template, one menu item 'Record' with four submenus and four tool bar buttons. We will see all these in our sample project.
Customizing The Project
Database support has been added to our application. Now let's customize the application according to our needs.
Add Controls to The Dialog
This is what my application will look like. MFC AppWizard has added a dialog to your application. Go to Resources from ClassView and double click on newly added dialog. Add three edit fields, three static fields, a list box, and a button. Three exit boxes will show three fields of the table which we can move by using menu or toolbar options.

FillList button click will add one column's data of the table to the list box.
Add Members
Add member variables corresponding to all three edit boxes by using ClassWizard. Click ClassWizard from the menu and select Member Variable Tab.

Now Click Add Variable button and add three variables corresponding to IDC_EDIT1, IDC_EDIT2, and IDC-EDIT3. Instead of adding new names, select m_pSet members from the drop-down list.

After adding these data members, your ClassWizard would look like this:

Now write a command handler for FillList button by double clicking it and write this code:
CListBox* list = (CListBox*)GetDlgItem(IDC_LIST1) ;
list->ResetContent();
ASSERT ( ! m_pSet->IsEOF() ) ;
while ( ! m_pSet->IsEOF() )
{
list->AddString( m_pSet->m_SearchName );
m_pSet->MoveNext();
}
m_pSet->MoveFirst();
UpdateData(FALSE);
Build and Run the application. Here is how output looks like:
