Solved How to print a report for currently opened record in form using a button. (1 Viewer)

Local time
Today, 22:23
Joined
Aug 19, 2021
Messages
212
Hi, I need help.
I have a form DebitVoucherForm and a report DebitVoucherReport and I have a button in my form Print Voucher. I Want to print DebitVoucherReport for currently opened record in my form by clicking on the button Print Voucher. I don't want to give VoucherID again and again to tell Ms Access that report for which record I want to print.
Please guide me how to do this.

Thank you so much.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:23
Joined
Oct 29, 2018
Messages
21,358
Hi. The OpenReport method has a WhereCondition argument that you can use to limit the records on the report. Have you tried that?
 
Local time
Today, 22:23
Joined
Aug 19, 2021
Messages
212
Hi. The OpenReport method has a WhereCondition argument that you can use to limit the records on the report. Have you tried that?
Thank you dear but I did not tried it before. Can you show me some screenshot? or Explain in detail. Thank you in advance.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:23
Joined
Oct 29, 2018
Messages
21,358
Thank you dear but I did not tried it before. Can you show me some screenshot? or Explain in detail. Thank you in advance.
Sure, for example, if you have a button to print the voucher, you might be using something like this.
Code:
DoCmd.OpenReport "VoucherReport"
That code might print the voucher for all customers. All I'm saying is you could limit the records/pages in the printed voucher if you use the WhereCondition argument, which might look like this.
Code:
DoCmd.OpenReport "VoucherReport", , , "CustomerID=17"
That code will print the voucher only for the customer whose CustomerID is 17.
 
Local time
Today, 22:23
Joined
Aug 19, 2021
Messages
212
Sure, for example, if you have a button to print the voucher, you might be using something like this.
Code:
DoCmd.OpenReport "VoucherReport"
That code might print the voucher for all customers. All I'm saying is you could limit the records/pages in the printed voucher if you use the WhereCondition argument, which might look like this.
Code:
DoCmd.OpenReport "VoucherReport", , , "CustomerID=17"
That code will print the voucher only for the customer whose CustomerID is 17.
Ok and If I want to print the VoucherReport for the record opened currently. I dont want to give CustomerID. Access should take the CustomerID of currently opened record automatically from my form and print its report.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:23
Joined
Oct 29, 2018
Messages
21,358
Ok and If I want to print the VoucherReport for the record opened currently. I dont want to give CustomerID. Access should take the CustomerID of currently opened record automatically from my form and print its report.
Right. It might look something like this.
Code:
DoCmd.OpenReport "VoucherReport", , , "CustomerID=" & Nz(Me.CustomerID,0)
 
Local time
Today, 22:23
Joined
Aug 19, 2021
Messages
212
Right. It might look something like this.
Code:
DoCmd.OpenReport "VoucherReport", , , "CustomerID=" & Nz(Me.CustomerID,0)
1632049102324.png
Hi, I could not do this. Can yo please explain where this code should put?
1632049167577.png

An Error is appearing when I am saving this.
 
Local time
Today, 22:23
Joined
Aug 19, 2021
Messages
212
Right. It might look something like this.
Code:
DoCmd.OpenReport "VoucherReport", , , "CustomerID=" & Nz(Me.CustomerID,0)
My issue resolved by using this code in VB
Code:
Docmd.OpenReport "PrintVoucher",acViewReport, , "[VoucherID]=" & Me.VoucherID, acWindowNormal
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:23
Joined
Sep 21, 2011
Messages
14,044
Yes, use VBA, learn VBA and try and leave macroes alone.
FWIW you might have got away with
Code:
"[VoucherID]=" & Forms!WhateverYouFormnames!VoucherID
In the where field, but would have to test that theory to be sure?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:23
Joined
Oct 29, 2018
Messages
21,358
My issue resolved by using this code in VB
Code:
Docmd.OpenReport "PrintVoucher",acViewReport, , "[VoucherID]=" & Me.VoucherID, acWindowNormal
Glad to hear you got it sorted out. Good luck with your project.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:23
Joined
Feb 19, 2002
Messages
42,971
One other important point-- ALWAYS save the current record before opening a report or second form for the first form's record. If you don't, the new report/form will not show any changes just made to the record but not yet saved.

Make this a habit.

Code:
If Me.Dirty then
    DoCmd.RunCmd acCmdSaveRecord
End If
 
Local time
Today, 22:23
Joined
Aug 19, 2021
Messages
212
One other important point-- ALWAYS save the current record before opening a report or second form for the first form's record. If you don't, the new report/form will not show any changes just made to the record but not yet saved.

Make this a habit.

Code:
If Me.Dirty then
    DoCmd.RunCmd acCmdSaveRecord
End If
Thank you
 

Users who are viewing this thread

Top Bottom