![]() |
Database »
Database »
General
Intermediate
The CAccessReports classBy Tom ArcherA class for Printing and Viewing MS Access Reports |
SQL, VC6, SQL Server, MFC, DBA, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

This article was written based on Access '97 (I believe) and I haven't updated it since. It will probably not work with other versions of Access but I welcome others to update the code to work with newer versions of Access.
This class (CAccessReports) was created for those of us who have the misfortune of having to 1) use the MS Access database engine and 2) use the MS Access database engine from a Visual C++ application. The CAccessReports class uses the Access Automation objects to open a specified database, run a report (within Access), print the report and save the Access database in HTML format so that it can be viewed in a Visual C++ application. You can accomplish each of these tasks with a single line of code. Here are some examples of how to use the CAccessReports class.
When you download the source code for the CAccessReports, you will also find a full-fledged test application. However, it's always nice to be able to see what you're getting before you invest the time in downloading, unzipping and running someone else's code. Therefore, here are some code snippets that show how easy the CAccessReports class is to use.
How you choose to instantiate CAccessReports object depends on how you plan to use it in your application. The first argument of the CAccessReports is the fully qualified name of the database file. The second argument specifies whether you want the CAccessReports class to preload all of the report names. An example of when you would want the class to retrieve all of the report names is if your application needs to display a list of the reports to the end-user. Otherwise, if you are instantiating a CAccessReports object in order to run, print or display specific reports whose names are known at compile-time, you can pass a value of FALSE for this argument.
// Have the CAccessReports object load the report names CAccessReports accessReports(YOUR_ACCESS_DATABASE, TRUE); // Simply attach to the specified Access database CAccessReports accessReports(YOUR_ACCESS_DATABASE, FALSE);
If you specified a value of TRUE for the second argument of the CAcessReport constructor, you can then retrieve the list of reports for a given Access database. Here's an example of a simple loop to retrieve and display all of the report names.
CAccessReports accessReports(YOUR_ACCESS_DATABASE, TRUE); for (int i = 0; i < accessReports.m_reports.GetSize(); i++) { AfxMessageBox(accessReports.m_reports[i]); }
While the CAccessReports class does allow you to display an Access report from a Visual C++ application (shown below), there are still valid reasons to run the report from within Access. Therefore, the RunReport member function does exactly that.
CAccessReports accessReports(YOUR_ACCESS_DATABASE, FALSE); accessReports.RunReport(YOUR_REPORT_NAME);
The PrintReport function takes as its only argument the name of a report to print. This function calls RunReport and then uses Automation to print the report.
CAccessReports accessReports(YOUR_ACCESS_DATABASE, FALSE); accessReports.PrintReport(YOUR_REPORT_NAME);
Access Automation doesn't allow for the ability to redirect the output of a report to a given window. However, it does allow for a report to be run and then saved in HTML format. That combined with the new Visual C++ 6.0 CHtmlView gives you the ability to run an Access report and display it in a Visual C++ application. In the example below, CAccessReportView is a CHtmlView derived class.
void CAccessReportView::OnInitialUpdate()
{
CHtmlView::OnInitialUpdate();
CAccessReports accessReports(YOUR_ACCESS_DATABASE, FALSE);
accessReports.SaveAsHtml(YOUR_REPORT_NAME, DESTINATION_FOLDER);
Navigate2(FULLY_QUALIFIED_FILE_NAME, NULL, NULL);
}
Unfortunately, Visual C++ 5.0 does not have the CHtmlView class. However, you can still display Access reports using the WebBrowser control (in the Visual C++ Component Gallery). In order to display the Access report in a Visual C++ 5 application, simply instantiate a CAccessReports object, call its SaveAsHtml member function and then call the WebBrowser control's Navigate function specifying the name of the HTML file.
Access Automation can not be used to get the entire list of reports for a an Access database. Therefore, if you specify TRUE to the second argument of the CAccessReports constructor, the class attempts to use the MFC DAO classes to access the msysobjects table of the specified database. In order to accomplish this, you must give the Admin user "read" priveleges to the database's msysobjects table (see below). If your application is printing or viewing a specific report from a specific database and you don't need the list of reports, then this limitation does not apply. If anyone knows of a better way of doing this, I'm always open to learning new things :)
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Mar 2000 Editor: Chris Maunder |
Copyright 2000 by Tom Archer Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |