Printing an Access 2010 Report via .NET without Access ....
Hello all. I'm working on an app in VB .NET 2005. One of the requirements is that it be able to print reports stored in an Access 2010 database (..accdb). I know there are ways to automate Access via .NET to print them, but I am not finding a way to do so if the user does not have Access 2010 on their PC.
Is there anyway at all to accomplish this? Is their, perhaps, a third party tool we can acquire that would help with this?
If we absolutely, positively, have to have Access 2010 on the end user's box, is there a way to print (or output to PDF) an Access 2010 report silently? As in the user never sees Access come up?
We've considered migrating the Access reports to something else, but have decided not to for a variety of reasons (there are 120+ complex reports, most conversion tools didn't work, lacked certain features, etc.)
Re: Printing an Access 2010 Report via .NET without Access ....
You should make use of Access automation. This means you should import : Microsoft.Office.Interop.Access in code as well as the necessary Project Reference.
Then, you should make use of the Microsoft.Office.Interop.Access.AcView.acViewPreview command which you could execute through the Access object's DoCmd method.
Let me have a look....
10 MINUTES LATER
Have a look at this exaqmple :
Code:
Imports Microsoft.Office.Interop.Access
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub buttonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
Dim AccObj As New Microsoft.Office.Interop.Access.Application()
AccObj.Visible = False
Dim strFileName As String = txtSource.Text.Trim()
Dim dlgReport As New OpenFileDialog()
dlgReport.Filter = "Microsoft Access (*.accdb)|*.accdb"
If strFileName.Length > 0 Then
dlgReport.FileName = strFileName
End If
If dlgReport.ShowDialog() = DialogResult.Cancel Then
Return
End If
txtSource.Text = dlgReport.FileName
lstAccReports.Items.Clear()
AccObj.OpenCurrentDatabase(dlgReport.FileName, False, "")
If Not AccObj.Visible = False Then
AccObj.Visible = False
End If
Dim sql As String = "SELECT [Name] FROM MSysObjects WHERE Type = -32764"
Dim dbTemp As dao.Database = AccObj.CurrentDb()
Dim rsTemp As dao.Recordset = dbTemp.OpenRecordset(sql, Type.Missing, Type.Missing, Type.Missing)
While Not rsTemp.EOF
lstAccReports.Items.Add(rsTemp.Fields(0).Value)
rsTemp.MoveNext()
End While
rsTemp.Close()
dbTemp.Close()
AccObj.CloseCurrentDatabase()
rsTemp = Nothing
dbTemp = Nothing
AccObj = Nothing
End Sub
Private Sub buttonPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim AccObj As New Microsoft.Office.Interop.Access.Application()
Dim rptAccReport As String = lstAccReports.SelectedItem.ToString()
AccObj.OpenCurrentDatabase(txtSource.Text.Trim(), False, "")
If Not AccObj.Visible = False Then
AccObj.Visible = False
End If
AccObj.Visible = False
AccObj.DoCmd.OpenReport(rptAccReport, Microsoft.Office.Interop.Access.AcView.acViewPreview, Type.Missing, Type.Missing, AcWindowMode.acWindowNormal, Type.Missing)
AccObj.DoCmd.PrintOut(AcPrintRange.acPrintAll, Type.Missing, Type.Missing, AcPrintQuality.acHigh, Type.Missing, Type.Missing)
AccObj.CloseCurrentDatabase()
AccObj = Nothing
End Sub
End Class
I have attached the sample as well.
EDIT: I just saw that you are one of very few people posting which version of VB they're using. The attached file is made with VB 2010, but the code should still work.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.