Solved How to print a report for currently opened record in form using a button.

Local time
Today, 05:24
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.
 
Hi. The OpenReport method has a WhereCondition argument that you can use to limit the records on the report. Have you tried that?
 
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.
 
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.
 
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.
 
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)
 
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.
 
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
 
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?
 
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
 
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

Back
Top Bottom