I’m using this code that I found on vb.net Macorotti, which I’ve adapted to run Visual Studio 2019. It works when printing, but when I ask it to preview the AcWindowMode.acWindowNormal
report, it sends it to the printer too. Help me find a solution.
Imports Microsoft.Office.Interop.Access
Public Class Frm_AccessRelatorio
Private Sub Frm_AccessRelatorio_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cboRelatorios.SelectedIndex = 0
BtnFechar.Focus()
End Sub
Private Sub BtnExecutar_Click(sender As Object, e As EventArgs) Handles BtnExecutar.Click
If cboRelatorios.SelectedIndex = -1 Then
MessageBox.Show("Selecione um relatório", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
Try
Dim MSA As New Microsoft.Office.Interop.Access.Application
MSA.Application.Visible = False
MSA.OpenCurrentDatabase(strPath & "Teste.accdb", False)
If rdbVisualizar.Checked Then
MSA.Application.DoCmd.OpenReport(cboRelatorios.Text, Microsoft.Office.Interop.Access.AcWindowMode.acWindowNormal)
'maximiza a janela
MSA.Application.DoCmd.Maximize()
Else
MSA.Application.DoCmd.OpenReport(cboRelatorios.Text, Microsoft.Office.Interop.Access.AcView.acViewNormal)
End If
Catch ex As Exception
MessageBox.Show("Erro : " & ex.Message, "Erro ao acessar o objeto", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
End Class
I have no idea how to solve it.
Manuel Lapa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
It sends to the printer, since that’s what “normal” means in VBA, and hence it means the same thing in vb.net.
Next up, if you trying to preview the report, then you have to make Access visible, and you have visible = False, which of course will defeat your whole goal here to display the report in place of printing the report.
So, this code works for me:
Dim MSA As New Microsoft.Office.Interop.Access.Application
MSA.Application.Visible = True
MSA.OpenCurrentDatabase("c:testtest444.accdb", False)
MSA.Application.DoCmd.OpenReport("rptHotelsA",
Microsoft.Office.Interop.Access.AcView.acViewPreview)
Note the use of “preview” in place of “Normal”, since as noted, normal = send directly to printer.
On the other hand, it not at all clear why you want to launch a copy of Access in VB, and then launch a report, since such a process is far better served by doing this in Access without introduction of VB code.
However, regardless of your goals here, you need to open the report in “preview” mode, not “normal”, and to see the report, you have to set the Access application object with Visible = true.
Edit: Hiding most of the Access interface.
In the comments, the question was how to hide the Access interface.
As I pointed out, you can hide most, if not all of the Access UI, and only show the report.
The settings in Access are:
Use tabbed interface, turn off tabs.
Hence, file->options->Current Database->Document windows options.
So, set tabbed interface (not overlapping windows), and turn off display of tabs.
Hence this:
Then under navigation, turn off display of navigation pane.
Hence this:
As noted, at this point, then the only UI will be the report, and the report ribbon. I often like and want the report ribbon, since it allows the user to send/save the report to a PDF, or print the report.
So, now the result of above is a window with a report, and only the report and the report ribbon displays. And if you use acReportView, then the report ribbon does not even display.
And if you re-size the Access window, the report will always re-size to the given window size you have. The result is this:
As noted, you could I suppose turn off the ribbon.
Hence, with this vb.net code:
Dim MSA As New Microsoft.Office.Interop.Access.Application
MSA.Application.Visible = True
MSA.OpenCurrentDatabase("c:testtest444.accdb", False)
MSA.Application.DoCmd.ShowToolbar("Ribbon", 2)
MSA.Application.DoCmd.OpenReport("rptHotelsA",
Microsoft.Office.Interop.Access.AcView.acViewReport)
And now the result is just a window with the report.
Hence:
So, with just a few simple settings, you can with ease hide most of the Access UI.
5