Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to open an Access report using MFC?

Status
Not open for further replies.

lklkl

Technical User
Oct 8, 2001
2
MY
If yes, please show me the way. If no, then are they any other options to create a report and implement it to MFC?

Help would be much appreciated. Thanks a lot. :)
 
Yes, it is definitely possible. There are several ways to do it. Here's a quick overview of what will probably be the easiest way:

1. First search for "*.olb" files on your system. On my PC, which has Access 97, the appropriate file is c:\msoffice\office\msacc8.olb. The file name will be different if you're using a different Access version.

2. Create a wrapper class for the type library. To do this, start Class Wizard and go to the Class Info tab. Pick Add Class and then "from a type library". Select the appropriate .olb file.

3. Use the functions provided by the wrapper classes. For instance, you might produce some code like this:

//Start a new instance of Microsoft Access
_Application oAccessApp;
if ( !oAccessApp.CreateDispatch( "Access.Application", NULL ) )
{
AfxMessageBox( "Failed to run Access." );
}
else
{
oAccessApp.OpenCurrentDatabase( "c:\\my database.mdb", FALSE );

CString sReport( "MyReport" );
COleVariant vOpt( (long)DISP_E_PARAMNOTFOUND, VT_ERROR );
COleVariant vReportName( sReport );
IDoCmd oIDoCmd = oAccessApp.GetDoCmd( );
oIDoCmd.OpenReport( vReportName, 2, vOpt, vOpt );
oAccessApp.Echo( 1, "Preview Report" ); // Show user the report
oIDoCmd.Maximize( );
oAccessApp.SetVisible( TRUE );

// do other stuff here, and then maybe some cleanup???
}

I didn't even try to compile this, though, so... no guarantees!

MSDN has a great deal of documentation on automating Access and the other Office apps. For some excellent MSDN links, please see this forum's FAQ on automating Office.


One final note: The MSDN documentation I have found will give you tons of info on how to automate an app, but I have been unable to locate much info on the individual functions (such as the OpenReport fxn in the above code) provided by the wrapper classes. The best source of info I have found on them (I'm probably missing something!) is to go into Access' Module Editor, type the function name, hit F1, and hope that you get help on the equivalent VBA function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top