Solved

MFC + access ?

Posted on 2004-03-20
16
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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…
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

615 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