Solved

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

Posted on 2000-03-03
8
333 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 31

Expert Comment

by:Zoppo
ID: 2579485
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
ID: 2579496
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
ID: 2579499
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Expert Comment

by:balugaa
ID: 2579559
balugaa changed the proposed answer to a comment
0
 

Expert Comment

by:balugaa
ID: 2579564
Sorry didnt want to lock the Question
0
 
LVL 1

Author Comment

by:TimB
ID: 2579603
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
ID: 2580461
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
ID: 2600898
Thanks balugaa!

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

Thanks v. much

TimB
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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