I am a new learner to the Dao database operation through MFC access.For now I am trying to write a program, saving two values in two edit boxes to a column of MS Access 2000 table. There are not many samples available. Could any one write little sample codes or guide me to a tutorial. The msdn explanation seems a bit difficult and abstract for me.

Dani AI

Generated

— a short, practical MFC + DAO recipe that saves two edit-box values into an Access 2000 table, plus a few troubleshooting tips. ’s pointer to the MSDN tutorial is useful for background; the snippet below shows a minimal, runnable pattern you can drop into a dialog button handler.

#include <afxdao.h>

// class members bound via DDX: CString m_val1, m_val2

void CMyDlg::OnBnClickedSave()
{
    UpdateData(TRUE); // pull text from edit controls into m_val1 / m_val2

    CDaoDatabase db;
    CDaoRecordset rs(&db);

    try
    {
        db.Open(_T("C:\\path\\yourdb.mdb"));              // full path to .mdb
        rs.Open(AFX_DAO_OPEN_TABLE, _T("YourTableName")); // table name
        rs.AddNew();
        rs.SetFieldValue(_T("Field1"), COleVariant(m_val1));
        rs.SetFieldValue(_T("Field2"), COleVariant(m_val2));
        rs.Update();
        rs.Close();
        db.Close();
    }
    catch (CDaoException* e)
    {
        e->ReportError(); // shows DAO error
        e->Delete();
    }
}

Practical notes and pitfalls: include <afxdao.h>; call UpdateData(TRUE) before inserting; use the exact table/field names from Access; use COleVariant for text and numeric variants for numbers; make sure the .mdb path is correct and the file is not opened exclusively by Access. If you need both edit values stored in a single column, concatenate them before SetFieldValue. Wrap DB calls in the CDaoException catch to see DAO error messages.

If starting new work today, consider ADO or OLE DB instead of DAO (DAO/Jet is legacy but fine for Access 2000). — this is on-topic for C++/MFC; the MFC DAO wrappers above are exactly the typical approach.

Recommended Answers

All 4 Replies

But this is c/c++ forum.

But this is c/c++ forum.

I thought it was C++ programming
then where should I put it?

Sorry didn't understood your post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.