Solved

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

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

Expert Comment

by:balugaa
ID: 2579559
balugaa changed the proposed answer to a comment
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 88
sameEnds challenge 3 133
sumNumber challenge 16 110
Problem to event 3 76
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…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 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

20 Experts available now in Live!

Get 1:1 Help Now