Mix the MFC Document/View and MS Access Database Applications



Click here for larger image

Environment: VC 5.0/6.0, MS Access 97/2000/2002.

Introduction

Why to mix?

Winning combination of MS Access/VBA and Visual C++/MFC allows developers to accomplish effectively the tasks of any complexity and in any direction. This alliance is suitable perfectly for developing data mapping systems, such as graphics, multimedia, GIS, CAD etc. The implantation of an MFC application into a high performance infrastructure of MS Access relieves the VC programmer of routine work that can be redirected to VBA co-developer. At the same time, extending the MS Access database application with MFC Document/View ability can solve some problems of VBA programming limitations. Next, developers know very well that it is not enough to create a good product - it is also necessary to provide its maintenance. This is most important for the applications founded on extensive and frequently changing data. Cooperation with MS Access abilities provides VC programmer with sparing regime for compound product maintenance: in most cases, the product updating/upgrading can be performed by VBA co-developer (or even by the end-user).
This is far from all benefits of combine using the MS Access/VBA with Visual C++/MFC tools.

How to mix?

The existing COM-based technologies (such as ActiveX, Add-Ins etc.) which are usually used for developing the compound applications, are not always correspond exactly to solution of specific tasks. SoftAdapter' programming tool, MFC Adapter for MS Access allows mix the MS Access/VBA and Visual C++/MFC applications not involving COM-based technologies.

Adapter opens up for programmers the opportunity to assemble the "double body" application (bb-application). Such application consists of two sections: MS Access database and Visual C++/MFC application. Note that these sections are not container and component as usual. They are two standalone applications! Nominally, MS Access part acts as a mainframe application, while the VC part is a sub application. In fact, each of two constituents of bb-application operates in autonomous mode. "Double body" application has a united "circulatory system" (message loop/processing), and also a common "skeleton": both parts share a common MDI frame and client windows. BB-application appears in dual image: either as MS Access or as VC application. Its state depends on it is active what kind of MDI child window. In transparent mode, Adapter implements control flow needed for proper work of bb-application. Adapter provides ideal conditions for bi-directional exchange of resources between MS Access and Visual C++ applications.

Step-by-Step preparation of the mixed application.

This sample shows how the programmer can combine the well known and frequently cited DIBLOOK application (presented in MFC->General category of samples collection and included in all versions of VC compiler) with MS Access database by using MFC Adapter.

Step1.  Transform the DIBLOOK project output result from EXE to DLL.

Add _WINDLL and _USRDLL symbols to compile options. Add /dll to link options.

Step2. Add the MFC Adapter library to the project.

Add to project the mfc4acc.h and mfc4acc.def  files stored in Adapter' INCLUDE folder.
Add appropriate mfc4acc[u][d][s].lib stored in Adapter' LIB folder:

  • [u] - with _UNICODE defined ( need for combination of MS Access 2K/XP and Win NT/2K/XP )
  • [d] - with _DEBUG defined
  • [s] - use MFC in a static library

By default, mfc4acc.lib supports _MBCS, NDEBUG,_AFXDLL

Step 3. Code modification.

The important fragments of DIBLOOK source code which need correction are given below.

BOOL CDibLookApp::InitInstance()
{
// Standard initialization
//  (if you are not using these features and wish to reduce
//  the size of your final executable, you should remove 
//  the following initialization

// Load standard INI file options (including MRU)
LoadStdProfileSettings(); 

// create main MDI Frame window
m_pMainWnd = new CMainFrame;


mfc4acc_Init(); // Attention! Adapter'
                // initialization instead of LoadFrame.

// Register document templates which serve as connection
// between documents and views. Views are contained in 
// the specified view

AddDocTemplate(new CMultiDocTemplate(IDR_DIBTYPE,
RUNTIME_CLASS(CDibDoc),
RUNTIME_CLASS(CMDIChildWnd),  // standard MDI child frame
RUNTIME_CLASS(CDibView)));

// enable file manager drag/drop and DDE Execute open
m_pMainWnd->DragAcceptFiles();
EnableShellOpen();
RegisterShellFileTypes(TRUE);

return CWinApp::InitInstance();
}

int CDibLookApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call
//       the base class

mfc4acc_Exit(); // Attention! Adapter' termination.

return CWinApp::ExitInstance();
}

Step 4. Build DIBLOOK.DLL

Run VC IDE command Build->Rebuild All.
Move (or copy) output result diblook.dll into appropriate folder: it must be located either within the scope of the environment variable PATH or in the same folder as msaccess.exe file.
Note that Adapter has added automatically to output result (diblook.dll) an external API to control MFC sub application within MS Access database:

  • sa_Init     - initialize the MFC sub application
  • sa_Term     - terminate the MFC sub application
  • sa_NewDoc   - create a new document
  • sa_OpenDoc  - open an existing document

This API will be used in MS Access frame application as shown below.

Step 5. Create MS Access database frame application.

Create blank database.
Create new form.
In this form, create six buttons with name:

  • "Turn_On"               -  Initialize the MFC sub application
  • "Turn_Off"              -  Terminate the MFC sub application
  • "Turn_Off_and_Exit"     -  Terminate the MFC sub application and close MS Access
  • "Create_new"            -  Create new MFC DIBLOOK document
  • "Browse"                -  Find DIBLOOK document (*.dib, *.bmp) with browser
  • "Connect"               -  Open DIBLOOK document with known path

For each of these buttons, build the event "Click" as shown in VBA code below: 

 

Option Compare Database 
Option Explicit 
Private Declare Sub sa_Init Lib "diblook.dll" _
       (ByVal app As Application) 
Private Declare Sub sa_Term Lib " diblook.dll" _
       (ByVal EndSession As Boolean) 
Private Declare Sub sa_NewDoc Lib " diblook.dll" () 
Private Declare Sub sa_OpenDoc Lib " diblook.dll" _
       (ByVal fname As String)

'Turn On 
Private Sub Turn_On_Click() 
sa_Init Me.Application 
End Sub
' Turn Off 
Private Sub Turn_Off_Click() 
sa_Term (False) 
End Sub
' Turn Off and Exit 
Private Sub Turn_Off_and_Exit_Click() 
sa_Term (True) 
End Sub
' Create New 
Private Sub Create_new_Click() 
sa_NewDoc 
End Sub
' Connect 
Private Sub Connect_Click() 
Dim path As String 
Dim pos As Long 
path = Me.Application.CodeDb.Name 
pos = InStr(1, path, "\diblook") 
path = Left(path, pos) + "diblook\mfc4acc.bmp" 
sa_OpenDoc (path) 
End Sub
' Browse 
Private Sub Browse_Click() 
sa_OpenDoc ("") 
End Sub

Step 6. Run mixed application.

Open form. Press button Turn On and then Find with Browser. Find a some bitmap file (*.bmp, *.dib) and open it. If you work with demo project, you can use "Load with fixed path" button. MS Access database application will be transformed to MFC DIBLOOK.

You're done! Congratulations on creating your first mixed application by using MFC Adapter for MS Access.

Discussion

You have made the first but very important step to create mixed application. You have merged the general (non-COM-based) MFC application and MS Access database. Now you be able to develop the MFC sub application with an active involving the programming resources of MS Access. Also, you can now effectively unite the work of VC and VBA programmers. You have unrestricted abilities to organize the bi-directional interaction between the VC and the VBA program modules. Really, you may:

  • to extend external API of MFC sub application for more flexible control within VBA modules;
  • to use automation controller abilities within MFC application to share the MS Access objects;
  • to implement dynamic data exchange between the VC and the VBA program modules by using database tables and queries.

Besides, you are relieved of high costs of COM-based techniques: no additional skills outside the basics of MFC programming are required for you in case of using the Adapter.

Downloads

Demo project - 183 Kb
MFC Adapter for MS Access - 2 388 Kb

Links

Adapter' API Reference



Comments

  • requesting programes in visual c++

    Posted by ahmed9 on 09/15/2008 03:30am

    i am learning visual c++, please send me programes Addition,Viewing,Editing,Printing, for the sallary pay rolls and invoice.please send me over e-mail... my ID:taa_J9@yahoo.com thanks best regards to the all staffs ... pasha-india

    Reply
  • broken links

    Posted by cilu on 10/10/2005 03:49pm

    The links provided in this article (http://www.softadapter.com/download/download.htm, http://www.softadapter.com/mfc4acc/api.htm) are not reachable.

    Reply
  • Access 97 linked tables

    Posted by Legacy on 04/17/2002 12:00am

    Originally posted by: Russ DePuy

    Hi,
    My question is, is there a way to edit the path to a linked table. I want to link to a hidden share, and don't believe I can do this without editing the path.

    Thanks
    Russ

    Reply
  • How can i see a report from access in VC++ using ADO?

    Posted by Legacy on 01/20/2002 12:00am

    Originally posted by: Nuno Rapaz

    How can i see a report from access in VC++ using ADO?
    I don't no very well how reports do, but what i want is to execute querys from VC++ in reports from access.
    I can do it in tables but i don't no if it's possible to do it in reports.

    thanks

    Reply
  • Disregard bitter comments

    Posted by Legacy on 12/17/2001 12:00am

    Originally posted by: Ian

    Your artical is good, as it promotes thought and invites questions. Occasionally though you will get some bright spark who says this is better that that, mine is better/bigger/etc than yours, etc.
    I thank you for taking the time to submit your article, keep it up! Don't let bitter and moronic criticism interfere with your intentions. His opinion is one of many.

    Cheers,
    Ian

    Reply
  • Got problem while unzip

    Posted by Legacy on 12/13/2001 12:00am

    Originally posted by: Hari RK

    I download the demo project. And i try to unzip, but a message "unable to open, try to download again. What to do? Which type of project i have to use, dialog based or sdi or mdi? Also tell me how to add _WINDLL and _USRDLL symbols to compile options. (Transform the DIBLOOK project output result from EXE to DLL) Add /dll to link options.

    How to add the MFC Adapter library to the project.

    Reply
  • Poor man...

    Posted by Legacy on 12/12/2001 12:00am

    Originally posted by: andr�

    >Winning combination of MS Access/VBA and Visual C++/MFC
    >allows developers to accomplish effectively the tasks of
    >any complexity and in any direction. This alliance is
    >suitable perfectly for developing data mapping systems,
    >such as graphics, multimedia, GIS, CAD etc. The
    >implantation of an MFC application into a high performance
    >infrastructure of MS Access relieves the VC programmer of
    >routine work that can be redirected to VBA co-developer.

    1)
    What are you selling here ? What kind of b--sheet ?
    As a sales man, You forgot to say how much we have to pay for the wonderful 2,5MB EXE SETUP you're selling here,
    what about the licence, copyrights, the msaccess price, etc... a lot of "out of Codeguru things"

    2)
    You forgot to say how slow msaccess is compared with an MFC program accessing directly the same ".mdb" file via the dbdao or a half dozen other methods..

    2-3 years ago we had to translate a library management program written in msaccess: started at 1.5MB it was growing by itself to 45MB and more. It was slooooooow. The library people needed the network, more than one codebar reader, etc. We translated it in MFC, with exactly the same look. It took 780kB only, managging a 1,5-2MB .MDB file 10 times faster, with no need of msaccess at all, with 3-4 barcodes readers and instances of the same program over the network, etc.

    I think the right solution is MFC r-e-p-l-a-c-i-n-g Access, not compromising with, as in your paper.

    The VB people programs access directly. The non-programming people can use wizards and querries. Both do not need such a "programming tool"...

    OK.. I'll stop here.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds