
Introduction
Why to mix?
The winning combination of MS Access/VBA and Visual C++/MFC allows developers to resolve effectively tasks of any complexity and in any directions. This alliance is especially effective for developing data mapping systems, such as graphics, multimedia, GIS, CAD etc. The implantation of a MFC application into a high performance infrastructure of MS Access relieves the VC programmer of routine work that can be imposed on VBA companion. 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, it is not enough to create a good application, it is also necessary to ensure its maintenance. This is most important for applications founded on extensive and frequently changing data. Integration into MS Access provides VC programmers with sparing regime for product maintenance: in most cases, the problem of product updating can be solved by VBA-programmers (or even by the end-users). This is far from all benefits that VC developers can expect by combining the MS Access/VBA with Visual C++/MFC tools.
How to mix?
The existing COM-based technologies (ActiveX, Add-Ins etc.) usually used for developing the integrated, multi-purpose applications are not always adequate for the programmers' specific tasks and often do not correspond to their skills.
SoftAdapter's programming tool, MFC Adapter for MS Access, enables to mix the MS Access/VBA and Visual C++/MFC applications not involving COM-based technologies, thus providing the developers with a unique ability to easily and quickly create the powerful mixed applications intended to solve the wide range of various tasks. Adapter allows programmers to assemble the "double body" application (bb-application). Such applications 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 the 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 what kind of MDI child window is active. 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's 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()
{
LoadStdProfileSettings();
m_pMainWnd = new CMainFrame;
mfc4acc_Init();
AddDocTemplate(new CMultiDocTemplate(IDR_DIBTYPE,
RUNTIME_CLASS(CDibDoc),
RUNTIME_CLASS(CMDIChildWnd),
RUNTIME_CLASS(CDibView)));
m_pMainWnd->DragAcceptFiles();
EnableShellOpen();
RegisterShellFileTypes(TRUE);
return CWinApp::InitInstance();
}
int CDibLookApp::ExitInstance()
{
mfc4acc_Exit();
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
- initializes the MFC sub application
sa_Term
- terminates the MFC sub application
sa_NewDoc
- creates a new document
sa_OpenDoc
- opens 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)
Private Sub Turn_On_Click()
sa_Init Me.Application
End Sub
Private Sub Turn_Off_Click()
sa_Term (False)
End Sub
Private Sub Turn_Off_and_Exit_Click()
sa_Term (True)
End Sub
Private Sub Create_new_Click()
sa_NewDoc
End Sub
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
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 some bitmap file (*.bmp, *.dib) and open it. If you work with the 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 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 combined the general (non-COM-based) MFC application and MS Access database. You can now develop the MFC sub application with an active involving the resources of MS Access. You can now effectively share the work between the VC and the VBA programmers. You have unrestricted abilities to organize the bi-directional interaction between the VC and the VBA program modules. Really, you can:
- extend external API of MFC sub application for more flexible control within VBA modules;
- use automation controller abilities within MFC application to share the MS Access objects;
- implement dynamic data exchange between the VC and the VBA program modules by using database tables and queries.
Besides, you are saved 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. Thus, MFC Adapter for Access will save much, your time and efforts.
Good Luck!
Related Links: