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

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
 
D

Douglas J. Steele

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
 
G

Greg Teets

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
 
B

Brendan Reynolds

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.
 
K

Ken Snell [MVP]

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)
 
G

Greg Teets

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
 
G

Greg Teets

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
 
B

Brendan Reynolds

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)
 
G

Greg Teets

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
 
B

Brendan Reynolds

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
 
G

Greg Teets

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
 
B

Brendan Reynolds

The code below works for me, called from Word. How does your code differ
from this?
 
G

Greg Teets

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
 
B

Brendan Reynolds

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.
 
G

Greg Teets

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top