Solved

MFC + access ?

Posted on 2004-03-20
16
384 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

21 Experts available now in Live!

Get 1:1 Help Now