Solved

How and I connect to a .MDB file without ODBC?

Posted on 2000-03-03
8
328 Views
Last Modified: 2013-11-20
I need to wite an MFC dialog based program that can connect to a MS access .MDB file.  The problem is, that I cannot use ODBC - this program will be sent out to many clients who are complete computer novices so I can't get them to set up ODBC data sources.

Does anyone know how I can get around this problem?

- Is DAO the answer? I've heard of it, but don't know anything more.

Any help or pointers would be brilliant.

Thanks for any comments,

TimB
0
Comment
Question by:TimB
8 Comments
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Hi TimB,

Just a hint: you could use ODBC and write a small programm which setups the data sources programmatically when installing your software...

ZOPPO
0
 
LVL 1

Author Comment

by:TimB
Comment Utility
Thanks  Zoppo,

I've found some info here:

http://www.codeguru.com/mfc_database/DAO_non_accessdb.shtml

And it lookslike I may be able to use the CDaoDatabase class.

I'll keep looking,  Thanks again,

TimB
0
 

Accepted Solution

by:
balugaa earned 100 total points
Comment Utility
There is one thing you havent mentioned, is the schema of the database known, If it is pre-defined schema then DAO is the answer.

for example:

1. In your dialog class declare a CString variable:

CString m_strSourceDatabase;

2. in the dlg class constructor intialise it :

CString m_strSourceDatabase = _T("");

3. map this variable to possible an edit control: specifying the path of the database or set it your self

e.g m_strDestDatabase = "C:\\anywhere\\something.mdb";

4. Presumably you want a DB manager Class that does all the nifty database stuff.

where you declare:
CDaoRecordset* m_pSet;
CDaoDatabase m_pDatabase;

in the constructor of the database class use:
CDBMarket::CDBMarket(CString path)
{
      m_pSet = NULL;
      
      
      try
      {
            m_pDatabase.Open(path,FALSE,FALSE);
      }
      catch(CDaoException* e)
      {
            
            AfxMessageBox("Error Occurred Openeing Database");
            e->Delete();
            return;
      }
//      AfxMessageBox(m_pDatabase.GetName());
}

5. To Open a record Set:
void CDBMarket::OpenRecordSet(CString strTable)
{
      CString strQuery = _T("");
      strQuery.Format("select * from [%s]",strTable);
//      AfxMessageBox(strQuery);
      
      // initialise recordset
      try
      {
            m_pSet = new CDaoRecordset(&m_pDatabase);
      }
      catch(CDaoException* e)
      {
            AfxMessageBox("Error opening RecordSet");
            m_bConnected = false;
            e->Delete();
            return;
      }
      
      // Open Record Set
      try
      {      
      m_pSet->Open(AFX_DAO_USE_DEFAULT_TYPE, strQuery);
    }
    catch (CDaoException* e)
      {
      m_bConnected = FALSE;
      e->Delete();
      return;
    }



5. To add/ query a specific table you can use for example:

something like: from the dlg class

void CMergeDlg::AddNewRecord(CDBMarket *dbSource, CDBMarket *dbDest)
{
      int a = dbSource->m_pSet->GetFieldCount();
      int b = dbDest->m_pSet->GetFieldCount();
      CString test = _T("");
      test.Format(" Source %d, Destination %d",a,b);
      if( a != b) AfxMessageBox("Merging Different Tables");
//      AfxMessageBox(test);
      COleVariant var;
      try{
            
            if(!dbDest->AddNewRec()) AfxMessageBox("Failed again");
            
            for(int i = 0; i < dbSource->m_pSet->GetFieldCount(); i++)
            {
                  if(i != 0)
                  {
                  var = dbSource->m_pSet->GetFieldValue(i);
                  dbDest->m_pSet->SetFieldValue(i, var);
                  }
            }
            dbDest->m_pSet->Update();
      }
      catch(CDaoException* e)
      {
      //      AfxMessageBox("Error in AddNewRecord");
            e->Delete();
      }

}


Thats pretty much it.
0
 

Expert Comment

by:balugaa
Comment Utility
balugaa changed the proposed answer to a comment
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:balugaa
Comment Utility
Sorry didnt want to lock the Question
0
 
LVL 1

Author Comment

by:TimB
Comment Utility
Thats ok baluga I didn't mind you answering.  I'm just going thru your code right now.....

Tim
0
 
LVL 4

Expert Comment

by:inpras
Comment Utility
Hi

U can configure ODBC programmatically we have developed apps that use odbc and CDatabase, which is released short time a go and up to now we havenot faced any problems with it  all that U have to do is

1. Have a tamplate to copy it  when ever user asks to create new database.

2. use SQLconfigDatasource(..) to programmatically configure the data source of ODBC (U have to supply drivers with installations).

3. Open database ( U can write an app with or without database support for this.

Hope this helps
0
 
LVL 1

Author Comment

by:TimB
Comment Utility
Thanks balugaa!

I'd figured out some of this myself, but your gude helped loads.

Thanks v. much

TimB
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mac OS 9 (Classic Environment) vs. MacBook Pro OS 10 8 36
Unix / Linux grid computing 5 125
ShiftLeft challenge 21 65
Not needed 13 53
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 …
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

11 Experts available now in Live!

Get 1:1 Help Now