Introduction.
Printing an MS Access Report from Excel or from Another Database
In Microsoft Access applications, we often use a Front-End/Back-End design. All the tables are maintained in the Back-End database and linked to the Front-End. We link them because the Front-End frequently needs to retrieve or update data from those tables.
Once linked, external database tables behave just like native tables in the Front-End. You can work with them seamlessly, without noticing any difference.
Note: When linking tables from a network location, always use the full UNC path of the database file, for example:
\\ServerName\FolderName\SubfolderName\DatabaseName.mdb
Instead of a server-mapped drive path, such as:
T:\FolderName\SubfolderName\DatabaseName.mdb
This is important because if the mapped drive letter (e.g., T:\) changes later to K:\, Z:\, or another letter, the links will break. Using the full network address ensures the linked tables remain intact regardless of drive mappings.
Updating a Table not Linked with the Current Database.
When you need to retrieve (or add/update) information in a table that is not linked to the front-end database, we need a VBA program to do that.
A sample procedure is shown below:
Public Function CategoryTable() Dim wsp As Workspace, db As Database Dim rst As Recordset 'Set a reference to the active Workspace Set wsp = DBEngine.Workspaces(0) 'DBEngine.Workspaces(0).Databases(0) is CurrentDB 'Open a second database in Workspace(0).Databases(1) position Set db = wsp.OpenDatabase("C:\mdbs\NWSample.mdb") 'Open Categories recordset from Database(1) Set rst = db.OpenRecordset("Categories") 'Display the CategoryName field value MsgBox rst![CategoryName] rst.Close db.Close 'remove objects and release memory Set rst = Nothing Set doc = Nothing Set db = Nothing Set wsp = Nothing End Function
The Databases Workspace.
Working with Databases in the DBEngine Workspaces Collection
When an Access database is opened in the Application Window, it is actually opened within a Workspace in the Workspaces collection, under the Application.DBEngine object.
The default Workspace is Workspace(0).
The first open database inside that workspace is addressable as Workspaces(0).Databases(0).
You can open more than one database within the same workspace and work with its tables or query recordsets. This approach is often better than permanently linking those tables to the Front-End database, especially if you do not need to use them on a day-to-day basis.
⚠️ However, there is an important limitation: you cannot open Forms or Reports from databases opened this way.
In fact, the object reference:
Application.DBEngine.Workspaces(0).Databases(0)
is equivalent to the CurrentDb object. While several databases can be loaded into Workspaces(0), only the current database will be visible in the Access Application Window. Other databases, if opened, will remain in memory until you explicitly close them.
This means you can:
Read and update tables from an external database without linking them together.
Create new tables or queries in those external databases as needed.
But you cannot:
Open forms or reports stored in those databases through this method.
Creating Queries on a non-linked External Table.
What if we want to create a query using data from an external table that is not linked to the front-end database? Surprisingly, Microsoft Access allows you to create queries without permanently linking external tables to the current database. Curious about how this works? You can learn the trick [here].
Up to this point, our discussion has focused on working with external tables and queries. However, the methods we covered so far will not allow you to open a form or report from another database. Normally, this requires opening the database in a separate Access Application Window.
That said, this statement is not entirely accurate—we’ll explore why in next week’s session.
But before diving into the Excel-based procedure, let’s first see how the same task can be handled from the active database itself.
The simple procedure steps are given below:
Create a separate Access Application Object.
Set its visible property value to Yes, so that we can see the Application Window.
Open the required Access Database within that Application Window.
Open the required Report in Print-mode (acViewNormal) to send the Report directly to the default printer, or in Print-Preview-mode (acViewPreview) to view the report before sending it to the printer manually.
Close the Database first and Quit the Application.
The Sample VBA Code is given below:
' Include the following in Declarations section of module. Dim appAccess As Access.Application Public Function PrintReport() '--------------------------------------------------------- 'Original Code Source: Microsoft Access Help Document '--------------------------------------------------------- Dim strDB As String ' Initialize string to database path. Const strConPathToSamples = "C:\Program Files\Microsoft Office\Office11\Samples\" strDB = strConPathToSamples & "Northwind.mdb" ' Create new instance of Microsoft Access Application. Set appAccess = CreateObject("Access.Application") ' Make Application Window Visible appAccess.Visible = True ' Open database in Microsoft Access window. appAccess.OpenCurrentDatabase strDB ' Open Catalog Report in Print Preview appAccess.DoCmd.OpenReport "Catalog", acViewPreview ' Enable next line of code to Print the Report 'appAccess.DoCmd.OpenReport "Catalog", acNormal 'appAccess.DoCmd.Close acReport, "Catalog", acSaveNo 'appAccess.CloseCurrentDatabase 'appAccess.Quit End Function
Copy and paste the above code into a new Standard Module of your Database.
Make changes to the Path of the Database and Report name, if needed.
Click somewhere in the middle of the Code and press F5 to run the Program.
In Microsoft Excel.
If you were able to run the code successfully and Print/preview your Report in a separate Access Application Window, then you may proceed to do the same thing from Microsoft Excel.
Open Microsoft Excel.
Display VBA Window (Developer ->Visual Basic).
Insert a Standard Module (Insert -> Module) in the VBA Window.
Copy and paste the above Code into the Module and save it.
Before running the code, you must add the Microsoft Access 12.0 Object Library to the Excel Project.
Select the References option from the Tools Menu.
Find Microsoft Access 12.0 Object Library (or whatever version is available on your machine) and put a check mark to select it.
Click the OK Command Button to close the Control.
Click in the middle of the Code and press F5 to run.
You will see the same result you saw when you ran the Code in Microsoft Access.
- Roundup Function of Excel in MS-Access
- Proper Function of Excel in Microsoft Access
- Appending Data from Excel to Access
- Writing Excel Data Directly into Access
- Printing MS-Access Report from Excel
- Copy-Paste Data From Excel to Access 2007
- Microsoft Excel Power in MS-Access
- Rounding Function MROUND of Excel
- MS-Access Live Data in Excel
- Access Live Data in Excel 2
- Opening an Excel Database Directly
- Create an Excel Word File from Access
No comments:
Post a Comment
Comments subject to moderation before publishing.