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.


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] –

  • [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)

// 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(CMDIChildWnd),  // standard MDI child frame

// enable file manager drag/drop and DDE Execute open

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
  • 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
  • "Create_new"           
    Create new MFC DIBLOOK document

  • "Browse"               
    Find DIBLOOK document (*.dib, *.bmp) with browser

  • "Connect"              
    Open DIBLOOK document with known path

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()
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.


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.


Demo project – 183 Kb

MFC Adapter for MS Access – 2 388 Kb


Adapter’ API Reference

More by Author

Must Read