I have created a database for taking orders. Once the order is taken there is a Print button on the form. The problem I have is getting only the current form to print. They way it is currently, every order we have ever taken prints out.
TIA
Bret
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Printing a form (Access XP)
The Print button probably executes an instruction DoCmd.Printout. If so, add acSelection so that it reads
DoCmd.Printout acSelection
This will make the button print the current record.
Note: forms are meant for entering, editing and viewing data. Although they can be printed, they are not really designed for it. Reports are designed with printing in mind. You could create a report based on the same record source as the form, and then have the command button on the form open the report for the current record.
Thanks Hans,
There is a copy of the VB code after I changed it to what you suggested at the bottom of this message. It still prints all the records in the db. I would do as you suggest, and create a report. I what to keep it as simple and quick as possible. Is there a to make the report print from a button on the order form?
Thanks again,
Bret
Private Sub Command20_Click()
On Error GoTo Err_Command20_Click
Dim stDocName As String
Dim MyForm As Form
stDocName = “Cake Order Form”
Set MyForm = Screen.ActiveForm
DoCmd.SelectObject acForm, stDocName, True
DoCmd.PrintOut acSelection
DoCmd.SelectObject acForm, MyForm.Name, False
Exit_Command20_Click:
Exit Sub
Err_Command20_Click:
MsgBox Err.Description
Resume Exit_Command20_Click
End Sub
1. To make the button print just the current record, take out the two lines with DoCmd.SelectObject.
2. In order to print the current record in a report, you must have a field (or combination of fields) that uniquely identifies a record. If the record source of the form has a primart key (for instance an AutoNumber field), you can use that. In the following example, I will assume that the record source has a numeric field OrderID that acts as unique identifier, and that you have a report named rptOrders, based on the same record source.
The code for a command button cmdReport would look like:
Sub cmdReport_Click()
DoCmd.OpenReport “rptOrders”, acViewPreview, , “[OrderID]=” & Me.[OrderID]
End Sub
This will open the report in preview mode. If you’d rather send it directly to the printer, replace acViewPreview by acViewNormal.
If OrderID is a text field, you must surround the value in quotes; to include a quote in a string in code, use Chr(34):
… , [OrderID]=” & Chr(34) & Me.[OrderID] & Chr(34)
Hans, I am still having difficulties with this printing all the orders in the database. I will attach the zipped database for you to look at if you would please. I did as you suggested before and created a report and the button previews that report. But when I print the report it prints all the orders.
Thanks,
Bret
Bret,
The reason that you get all records is that you haven’t added a where-condition to the DoCmd.OpenReport instruction. the code should look as follows:
Dim stDocName As String, stLinkCriteria As String
stDocName = “Order”
stLinkCriteria = “ID=” & Me.ID
DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
I have done this in the database you posted; the modified version is attached.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications