Solved

MFC + access ?

Posted on 2004-03-20
16
386 Views
Last Modified: 2013-11-20
can i link my MFC program to access database? if can, can anyone show me how?  thanx very much.
0
Comment
Question by:harsher
  • 7
  • 6
  • 2
  • +1
16 Comments
 

Expert Comment

by:Dj_Fx8
ID: 10642242
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
 
LVL 5

Expert Comment

by:Nass89
ID: 10642751
Hi,
Another solution is using App Wizard. There is database support in Step 2. It uses CRecorset class to access database.

Good Luck!
0
 
LVL 5

Expert Comment

by:Nass89
ID: 10642773
0
 

Author Comment

by:harsher
ID: 10643203
thanx everyone.. i will try it out.... if successsfull then i will award the points
0
 

Author Comment

by:harsher
ID: 10643333
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
 

Expert Comment

by:Dj_Fx8
ID: 10643961
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
 

Expert Comment

by:Dj_Fx8
ID: 10644388
Any other problems , feel free to ask
0
 

Author Comment

by:harsher
ID: 10644644
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:Dj_Fx8
ID: 10645227
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
 

Author Comment

by:harsher
ID: 10645348
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
 

Expert Comment

by:Dj_Fx8
ID: 10645485
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10648375
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
 

Author Comment

by:harsher
ID: 10648868
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
 

Accepted Solution

by:
Dj_Fx8 earned 250 total points
ID: 10650945
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
 

Author Comment

by:harsher
ID: 10650988
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
 

Expert Comment

by:Dj_Fx8
ID: 10651021
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Define unique primary key 9 89
no14 challenge 14 62
abstract class with all non abstract mentods 6 76
canBalance challenge 34 70
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now