Solved

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

Posted on 2000-03-03
8
335 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 140
method notes when mouse over in eclipse 5 90
Window placement 17 95
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 116
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: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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