Click here to Skip to main content
13,044,115 members (81,879 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hey Guys

I've a database (MS Access) and I created a MFC-Windows Application. Now I want to open the database in my tool and get some values out of it.
How do I have to do it with CDaoDatabase? I have no idea.

Thanks for your help.

Posted 6-May-13 0:30am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

epanjohura 6-May-13 7:18am
I allready tried this.. but it didn't help me
LaxmikantYadav 7-May-13 1:37am
Can you post error or code, which is not working ? So we can help you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

There are many ways to use the CDao* classes. If you only want to read some fields from one or more tables, you can omit the creation of special derived classes to access them:
CDaoDatabase db;
    // Open access file: non-exclusive, read only
    // When db is password protected, pass "PWD=password" as 4th argument
    db.Open(lpszFileName, FALSE, TRUE);
    // Open a table
    CDaoTableDef TableDef(&db);
    // Step through all recordsets in the table and get
    //  the value for a specific field
    CDaoRecordset rc;
    if (rc.IsOpen())
        if (!rc.IsBOF())
            while (!rc.IsEOF())
                // Get the content of the field
                COleVariant var;
                rc.GetFieldValue(lpszFieldName, var);
                // print content if a string or integer 
                if (VT_BSTR == var.vt)
                    CString str = V_BSTR(&var);
                    _tprintf(_T("%s = %s\n"), lpszFieldName, str.GetString());
                else if (VT_I4 == var.vt)
                    _tprintf(_T("%s = %d\n"), lpszFieldName, var.lVal);
    if (TableDef.IsOpen())
    // Execute a SQL command
    CString str;
    str.Format(_T("SELECT * FROM [%s]"), lpszTableName);
    CDaoRecordset rc1(&db);
    rc1.Open(dbOpenDynaset, str.GetString());
    if (rc1.IsOpen())
        // step through the recordsets like in the above code
        //  and access fields by name or index
catch (CDaoException * pe)
    // handle errors here
if (db.IsOpen())

As you can see, the main work is performed using the CDaoRecordset class. Pass a pointer to your database to the constructor when executing SQL commands with the Open() function or pass a pointer to a table or query definition when opening these. Then step through the recordsets using the Move...() functions and retrieve (or set) fields by accessing them by their name or index.

Before proceeding you should also read about the VARIANT type and the MFC COleVariant encapsulation.
epanjohura 6-May-13 10:07am
Thank you very much for your help. I'll try it now and accept it if it works..
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I would try Googling for[^] that.
epanjohura 6-May-13 7:19am
I did! I still have no idea how to do it.
CPallini 6-May-13 7:57am
There are many many (did I write 'many'?) examples available, cannot you modify one of them in order to fit your needs?
epanjohura 6-May-13 7:58am
I'm trying.. but until now I didn't understand anything..!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 6 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100