You are on page 1of 1

As you may know, Microsoft Access provides a much more robust reporting system

than Visual Basic. As a result, if you use Access as a back-end to your


application, you may want to print Access reports from your VB application.
Fortunately, you can do just that with Automation.

The following code shows one way to do so, using late binding:

Dim objAccess As Object

Private Sub Command1_Click()


Dim dbName As String
Dim rptName As String
Dim Preview As Long
Const acNormal = 0
Const acPreview = 2

dbName = "D:\PathToDB\db1.mdb"
rptName = "MyReportName"
Preview = acPreview 'acNormal

With objAccess
.OpenCurrentDatabase filepath:=dbName
If Preview = acPreview Then
.Visible = True
.DoCmd.OpenReport rptName, Preview
Else
.DoCmd.OpenReport rptName
End If
End With
End Sub

Private Sub Form_Load()


Set objAccess = CreateObject("Access.Application")
End Sub

Private Sub Form_Unload(Cancel As Integer)


On Error Resume Next
objAccess.Quit
On Error GoTo 0
Set objAccess = Nothing
End Sub

You might also like