How do I do a print preview using Access Automation in VB? Print works fine.

  • Thread starter Thread starter Greg Teets
  • Start date Start date
G

Greg Teets

This opens the report and prints it out:

objAccess.DoCmd.OpenReport ("Invoice")

But I can't seem to use any options.

How can I print preview. I get a syntax error when I use the
acViewPreview option.

Is there a good site where I can learn about Automation?

Is this the right group for these questions? If not, please tell me
what group is.

Thanks.
Greg Teets
Cincinnati Ohio USA
 
ACC: How to Use Automation to Print Microsoft Access Reports:
http://support.microsoft.com/?id=145707
ACC2000: How to Use Automation to Print Microsoft Access Reports
http://support.microsoft.com/?id=210132
ACC2002: How to Use Automation to Print Microsoft Access Reports
http://support.microsoft.com/?id=296586
ACC: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=147816
ACC2000: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=210111
 
ACC2000: How to Use Automation to Print Microsoft Access Reports

I've read that. I can't get it working.

This opens the report and prints it out:

objAccess.DoCmd.OpenReport ("Invoice")

However, I get a syntax error when I use this:

objAccess.DoCmd.OpenReport ("Invoice", acPreview)

or

objAccess.DoCmd.OpenReport ("Invoice", acViewPreview)

Thanks.
Greg Teets
Cincinnati Ohio USA
 
If you're using late binding, you'll need to use literal values instead of
intrinsic constants and enums. The literal value of acViewPreview is 2.
 
Automation won't know what the VBA constants are unless you set a reference
to ACCESS. So use the actual number for the constant:

objAccess.DoCmd.OpenReport ("Invoice", 2)
 
Automation won't know what the VBA constants are unless you set a reference
to ACCESS. So use the actual number for the constant:

objAccess.DoCmd.OpenReport ("Invoice", 2)


This is my code. I get a syntax error on the OpenReport line:

Private Sub cmdRunInvoice_Click()
On Error GoTo ExecuteError

Dim objAccess As Access.Application
Set objAccess = New Access.Application

objAccess.OpenCurrentDatabase "C:\Current Database\old.mdb"
objAccess.DoCmd.OpenReport ("Invoice", 2)

Exit Sub

ExecuteError:
Debug.Print "*** Error executing command in RunInvoice_Click ***"
& Err.Description

End Sub


Greg Teets
Cincinnati Ohio USA
 
This is my code. I get a syntax error on the OpenReport line:

Private Sub cmdRunInvoice_Click()
On Error GoTo ExecuteError

Dim objAccess As Access.Application
Set objAccess = New Access.Application

objAccess.OpenCurrentDatabase "C:\Current Database\old.mdb"
objAccess.DoCmd.OpenReport ("Invoice", 2)

Exit Sub

ExecuteError:
Debug.Print "*** Error executing command in RunInvoice_Click ***"
& Err.Description

End Sub


Greg Teets
Cincinnati Ohio USA

I have a reference set to the Microsoft Access 9.0 Object Library.


Greg Teets
Cincinnati Ohio USA
 
That's the parentheses - VBA expects you to assign the result of a function
to something when you enclose the arguments to the function in parentheses.
Try ...

objAccess.DoCmd.OpenReport "Invoice", 2

... or ...

Call objAccess.DoCmd.OpenReport("Invoice", 2)
 
objAccess.DoCmd.OpenReport "Invoice", 2 This gave a syntax error.
.. or ...

Call objAccess.DoCmd.OpenReport("Invoice", 2)
This ran but nothing happened.

Greg Teets
Cincinnati Ohio USA
 
You won't see anything unless you make the Access application visible ...

Sub Test2()

On Error GoTo ExecuteError

Dim objAccess As Access.Application
Set objAccess = New Access.Application

objAccess.OpenCurrentDatabase "C:\new.mdb"
Call objAccess.DoCmd.OpenReport("rptTest", 2)
objAccess.Visible = True '<-----------------------------------------
Exit Sub

ExecuteError:
Debug.Print "*** Error executing command in RunInvoice_Click ***" &
Err.Description


End Sub
 
You won't see anything unless you make the Access application visible ...

Sub Test2()

On Error GoTo ExecuteError

Dim objAccess As Access.Application
Set objAccess = New Access.Application

objAccess.OpenCurrentDatabase "C:\new.mdb"
Call objAccess.DoCmd.OpenReport("rptTest", 2)
objAccess.Visible = True '<-----------------------------------------
Exit Sub

ExecuteError:
Debug.Print "*** Error executing command in RunInvoice_Click ***" &
Err.Description


End Sub


When I set visible to True and comment out the line causing the error,
Access just flashes on and off one time on the screen.

The Open Report command still creates an error, though.

I just used the Access help option to repair my installation and that
didn't help either.
Greg Teets
Cincinnati Ohio USA
 
The code below works for me, called from Word. How does your code differ
from this?
 
When I set visible to True and comment out the line causing the error,
Access just flashes on and off one time on the screen.

The Open Report command still creates an error, though.

I just used the Access help option to repair my installation and that
didn't help either.
Greg Teets
Cincinnati Ohio USA


Here's my code. I don't see any differences, other than variable
names, do you?

Private Sub cmdRunInvoice_Click()
On Error GoTo ExecuteError

Dim objAccess As Access.Application
Set objAccess = New Access.Application
'MsgBox objAccess.Reports.Count

objAccess.OpenCurrentDatabase "C:\Current Database\old.mdb"
objAccess.Visible = True
objAccess.DoCmd.OpenReport("Invoice", 2)


Exit Sub

ExecuteError:
Debug.Print "*** Error executing command in RunInvoice_Click ***"
& Err.Description

End Sub
Greg Teets
Cincinnati Ohio USA
 
Here's my code. I don't see any differences, other than variable
names, do you?

Private Sub cmdRunInvoice_Click()
On Error GoTo ExecuteError

Dim objAccess As Access.Application
Set objAccess = New Access.Application
'MsgBox objAccess.Reports.Count

objAccess.OpenCurrentDatabase "C:\Current Database\old.mdb"
objAccess.Visible = True
objAccess.DoCmd.OpenReport("Invoice", 2)


Exit Sub

ExecuteError:
Debug.Print "*** Error executing command in RunInvoice_Click ***"
& Err.Description

End Sub

Yes, you still have the parentheses in this line ...

objAccess.DoCmd.OpenReport("Invoice", 2)

It needs to be either
objAccess.DoCmd.OpenReport "Invoice",2
or
Call objAccess.DoCmd.OpenrReport("Invoice",2)
in other words, either take out the parentheses, or leave them in but add
'Call' at the start.
 
Yes, you still have the parentheses in this line ...

objAccess.DoCmd.OpenReport("Invoice", 2)

It needs to be either
objAccess.DoCmd.OpenReport "Invoice",2
or
Call objAccess.DoCmd.OpenrReport("Invoice",2)
in other words, either take out the parentheses, or leave them in but add
'Call' at the start.

It's working now. Thank you very much.

I thought I tried all those combinations before, but I must not have.

Thanks for your patience in helping me.
Greg Teets
Cincinnati Ohio USA
 
Back
Top