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 PrintAccessRepo rt(ByVal sReportName As String, Optional ByVal
sQryName As String = "", Optional ByVal sSQL As String = "")
Dim oAccess As New Access.Applicat ionClass
oAccess.Visible = True
oAccess.OpenCur rentDatabase(gs CurLocation & gsCurDatabase)
oAccess.DoCmd.M inimize()
'Create new query if needed
If sQryName.Length 0 And sSQL.Length 0 Then
Try
'oAccess.Curren tDb.QueryDefs.D elete(sQryName)
oAccess.DoCmd.D eleteObject(Acc ess.AcObjectTyp e.acQuery, sQryName)
Catch
'Close and reopen the Access object if above statement failed
oAccess.Visible = False
oAccess.Quit(Ac cess.AcQuitOpti on.acQuitSaveNo ne)
System.Runtime. InteropServices .Marshal.Releas eComObject(oAcc ess)
oAccess.Visible = True
oAccess.OpenCur rentDatabase(gs CurLocation & gsCurDatabase)
oAccess.DoCmd.M inimize()
End Try
oAccess.Current Db.CreateQueryD ef(sQryName, sSQL)
End If
'Preview the report
oAccess.DoCmd.O penReport(sRepo rtName, Access.AcView.a cViewPreview, , ,
Access.AcWindow Mode.acDialog)
'Close the Access Instance
If Not oAccess Is Nothing Then
' Call Access Quit method without saving any changes.
oAccess.Quit(Ac cess.AcQuitOpti on.acQuitSaveNo ne)
' Use Marshal class' ReleaseComObjec t to release the Access instance.
System.Runtime. InteropServices .Marshal.Releas eComObject(oAcc ess)
' Dereference the oAccess variable.
oAccess = Nothing
End If
End Sub
------------------------------------------------------------------------
George Shubin Custom Software Development
dX Software Systems Database Applications
Ph: 503-981-6806 Fax: 503-982-0120
www.dxonline.com ge****@dxonline .com
------------------------------------------------------------------------
"Despite efforts by government to prevent concentration in industry, the
regulators are bringing us to the point where only the largest companies can
survive." -- Henry Ford