I'm in the process of migrating a MS Access 2003 application to VB .Net
(2005). How can I run the reports in Access from the VB application?
Try the following. It requires Microsoft's Access to be installed on the
user's system. (Watch out for wordwrap.)
Public Sub PrintAccessReport(ByVal sReportName As String, Optional ByVal
sQryName As String = "", Optional ByVal sSQL As String = "")
Dim oAccess As New Access.ApplicationClass
oAccess.Visible = True
oAccess.OpenCurrentDatabase(gsCurLocation & gsCurDatabase)
oAccess.DoCmd.Minimize()
'Create new query if needed
If sQryName.Length > 0 And sSQL.Length > 0 Then
Try
'oAccess.CurrentDb.QueryDefs.Delete(sQryName)
oAccess.DoCmd.DeleteObject(Access.AcObjectType.acQuery, sQryName)
Catch
'Close and reopen the Access object if above statement failed
oAccess.Visible = False
oAccess.Quit(Access.AcQuitOption.acQuitSaveNone)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
oAccess.Visible = True
oAccess.OpenCurrentDatabase(gsCurLocation & gsCurDatabase)
oAccess.DoCmd.Minimize()
End Try
oAccess.CurrentDb.CreateQueryDef(sQryName, sSQL)
End If
'Preview the report
oAccess.DoCmd.OpenReport(sReportName, Access.AcView.acViewPreview, , ,
Access.AcWindowMode.acDialog)
'Close the Access Instance
If Not oAccess Is Nothing Then
' Call Access Quit method without saving any changes.
oAccess.Quit(Access.AcQuitOption.acQuitSaveNone)
' Use Marshal class' ReleaseComObject to release the Access instance.
System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
' Dereference the oAccess variable.
oAccess = Nothing
End If
End Sub