MFC + access ?

can i link my MFC program to access database? if can, can anyone show me how?  thanx very much.
harsherAsked:
Who is Participating?
 
Dj_Fx8Connect With a Mentor Commented:
Hi
In the code you've showen your trying to the name Mercedes into a Field called Mercedes in your table

CString NewValue = "My new data";

myRecord->AddNew();
myRecord->SetFieldValue("First Name", (LPCTSTR)NewValue );  ///Fisrt Name is the field name in the Db table
                                                                                              ///NewValue is what you want  to put into it
myRecord->Update;

0
 
Dj_Fx8Commented:
Hi

This should work for you, excuse the spaceing, it was copied from my code

      CException e;
      m_pDB = new CDaoDatabase;
      m_pDB->Open("C:\MyDb.mdb");
      m_prsRecords = new CDaoRecordset(m_pDB);

      TRY
      {
            m_prsRecords->Open(dbOpenDynaset, m_szSQL);   ///m_szSQL is a std SQL string to select the records you require
      }
      CATCH_ALL (e)
      {
            e->ReportError();
            return;
      }
      END_CATCH_ALL
      if (!m_prsRecords->IsOpen())
            return;
      if (m_prsRecords->IsBOF() && m_prsRecords->IsEOF())
            m_lCount = 0;
      else
      {
            m_prsRecords->MoveLast();
            m_lCount = m_prsRecords->GetRecordCount();
            m_prsRecords->MoveFirst();
      }

0
 
Nass89Commented:
Hi,
Another solution is using App Wizard. There is database support in Step 2. It uses CRecorset class to access database.

Good Luck!
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
harsherAuthor Commented:
thanx everyone.. i will try it out.... if successsfull then i will award the points
0
 
harsherAuthor Commented:
try it out.. none works... btw...dj_fx8, comes out 102 errors... starting from

m_pDB = new CDaoDatabase;

undeclared m_pDB
unidentified CDaoDatabase

should i declare something b4 i can use it?
0
 
Dj_Fx8Commented:
Sorry for not including the declrations, as I said it was copy & pasted from a Class I wrote to make working with Db's simpler, declare then as follows
      CDaoRecordset* m_prsRecords;
      CDaoDatabase* m_pDB;

if you still get the error unidentified CDaoDatabase try including AFXDAO.H
0
 
Dj_Fx8Commented:
Any other problems , feel free to ask
0
 
harsherAuthor Commented:
thanx for the reply. adding the AFXDAO.h works but another problem came up. it says "unrecognised database format C:\flight_data.mdb" i'm using officexp to create the access table. i will increase the points to 200 if u can help me. thanx again
0
 
Dj_Fx8Commented:
Hi

Have a look at this Microsoft Knowledge Base Article - 236991

http://support.microsoft.com/default.aspx?scid=kb;en-us;236991&Product=vst

It should help you on this problem
0
 
harsherAuthor Commented:
i convert the database to access 97 format and it can read now... but how do i load the data from the recordset into a edit box? i'll give u 250 points for helping me out.
0
 
Dj_Fx8Commented:
Hi

Ok after you've opened your Db

      CString szText("");
      COleVariant covVal;
      
      m_prsRecords->GetFieldValue("Artist", covVal);   ///"Atrist" is a example Field name in your Db
      if (covVal.vt != VT_NULL)
            {
            szText = (LPCTSTR)covVal.bstrVal;
            }
Now you have the data in szText, and can use this to display data in a text box

There are a few other points on this

1  This will the data in the Field 'Artist' for the first record, ie the current record, to get it for other records you must make the required record Current by using either MoveNext, Find or FindFirst  (See MSDN  CDaoRecordset)

2 This assumes the Field Artist is a String value, if it was a numerical value just use
                m_prsRecords->GetFieldValue("Disc", covVal);
      szText = covVal.lVal;

0
 
AndyAinscowFreelance programmer / ConsultantCommented:
At the start of your InitInstance of the app

      //ACCESS 2000 SUPPORT THROUGH DAO
      AfxGetModuleState()->m_dwVersion = 0x0601;

I don't know if it will enable your app to open a database in Access XP format but it does work for Access 2000.
0
 
harsherAuthor Commented:
Dj_Fx8, ur code works for me.... i'm increasing the points to 250 if u can help me fix this code.. i wan to add new records into the database... this line is what i coded...

covVal = FirstName;
myRecord->AddNew();
myRecord->SetFieldValue("FirstName", covVal);
myRecord->Update();

if the value in FirstName is "Mercedes"
the entry in the database.... is
'M'

what is the problem?
0
 
harsherAuthor Commented:
Thank you very much!!! Thank you very much!!! Thank you very much!!! Thank you very much!!!

As i promised i will give u 250 points.
0
 
Dj_Fx8Commented:
No problem at all, I'm glad to be able to help, I needed a lot of it myself when starting out, and still do at times :-)

Happy Coding
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.