Preview doesn't open current record

C

CW

I have the basic details of our invoices (which are reports) displayed on a
continuous form i.e. date, number, customer, amount. At the end of each row I
have a button so that we can open the form on which the invoice was created,
to modify it. That works fine, it opens the selected invoice.
I also have a button that should open a preview of the selected invoice
(rpt). However, I can't get this to fire the correct record - it insists on
opening the last (most recently created) invoice, no matter on which line the
record selector may be.
This is the code I am using on the Click event of that button:

Private Sub cmdPreviewInvoice_Click()
On Error GoTo Err_cmdPreviewInvoice_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "rptInvoicePrint"

stLinkCriteria = "[InvNo]=" & Me![InvNo]
DoCmd.OpenReport stDocName, , , stLinkCriteria, acPreview

Exit_cmdPreviewInvoice_Click:
Exit Sub

Err_cmdPreviewInvoice_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewInvoice_Click

End Sub


Where am I going wrong??
Thanks a lot
CW
 
F

fredg

I have the basic details of our invoices (which are reports) displayed on a
continuous form i.e. date, number, customer, amount. At the end of each row I
have a button so that we can open the form on which the invoice was created,
to modify it. That works fine, it opens the selected invoice.
I also have a button that should open a preview of the selected invoice
(rpt). However, I can't get this to fire the correct record - it insists on
opening the last (most recently created) invoice, no matter on which line the
record selector may be.
This is the code I am using on the Click event of that button:

Private Sub cmdPreviewInvoice_Click()
On Error GoTo Err_cmdPreviewInvoice_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "rptInvoicePrint"

stLinkCriteria = "[InvNo]=" & Me![InvNo]
DoCmd.OpenReport stDocName, , , stLinkCriteria, acPreview

Exit_cmdPreviewInvoice_Click:
Exit Sub

Err_cmdPreviewInvoice_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewInvoice_Click

End Sub

Where am I going wrong??
Thanks a lot
CW

I see 2 errors.
1) Access doesn't save new data unless you navigate to another record,
close the form, or explicitly tell it to save the data.

Add one line of code to your existing code (Marked with '*** below):

Private Sub cmdPreviewInvoice_Click()
On Error GoTo Err_cmdPreviewInvoice_Click

DoCmd.RunCommand acCmdSaveRecord '***

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "rptInvoicePrint"

stLinkCriteria = "[InvNo]=" & Me![InvNo]

etc..... See #2 below

2) Also Note that your OpenReport syntax is not correct.

You have...
DoCmd.OpenReport stDocName, , , stLinkCriteria, acPreview

It should be....
DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria

etc........
 
C

CW

I'm always happy to get an answer from you Fred, cos I know it's always going
to work - thanks a lot
CW

fredg said:
I have the basic details of our invoices (which are reports) displayed on a
continuous form i.e. date, number, customer, amount. At the end of each row I
have a button so that we can open the form on which the invoice was created,
to modify it. That works fine, it opens the selected invoice.
I also have a button that should open a preview of the selected invoice
(rpt). However, I can't get this to fire the correct record - it insists on
opening the last (most recently created) invoice, no matter on which line the
record selector may be.
This is the code I am using on the Click event of that button:

Private Sub cmdPreviewInvoice_Click()
On Error GoTo Err_cmdPreviewInvoice_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "rptInvoicePrint"

stLinkCriteria = "[InvNo]=" & Me![InvNo]
DoCmd.OpenReport stDocName, , , stLinkCriteria, acPreview

Exit_cmdPreviewInvoice_Click:
Exit Sub

Err_cmdPreviewInvoice_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewInvoice_Click

End Sub

Where am I going wrong??
Thanks a lot
CW

I see 2 errors.
1) Access doesn't save new data unless you navigate to another record,
close the form, or explicitly tell it to save the data.

Add one line of code to your existing code (Marked with '*** below):

Private Sub cmdPreviewInvoice_Click()
On Error GoTo Err_cmdPreviewInvoice_Click

DoCmd.RunCommand acCmdSaveRecord '***

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "rptInvoicePrint"

stLinkCriteria = "[InvNo]=" & Me![InvNo]

etc..... See #2 below

2) Also Note that your OpenReport syntax is not correct.

You have...
DoCmd.OpenReport stDocName, , , stLinkCriteria, acPreview

It should be....
DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria

etc........
 

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