In form design view, click the command button to select it, then click View
Properties. This opens what is known as the Property Sheet. Click the
Event tab. You will see, among other things, "On Click". Click the row (it
may already contain the words [Event Procedure], then click the three dots.
If it says [Event Procedure] the Visual Basic editor will appear. If not,
you would choose Code Builder, then OK to open the VB editor.
I think this is what you have already described.
You have a command button called Print_Official_License. It's Click event
starts with:
Private Sub Print_Official_License_Click()
The On Error line of code is part of the error handling. If there is an
error, the code will shoot down to the Err_Print_Official_License_Click line
(which is what the OnError line specified). After that is the declaration
of the variable stDocName, and its definition as "Official License", which I
assume is the name of the report you wish to print. Replace acNormal with
acViewPreview, and the report will open in print preview so that you can
view it before deciding to print it. With acNormal, as I recall, it just
prints immediately. Add a comma, then another, the put it the Where
condition as I have written it, except use the relevant field name from your
own database. If there is a primary key field you can use that in the
expression. Your line of code may end up looking something like:
DoCmd.OpenReport stDocName, acViewPreview, , "LicenseID = " & Me.LicenseID
Note the double comma. If there is to be a named filter it would go between
those commas. If there is no filter, the comma is still needed as a
placeholder.
What you are saying here is "Open the report to the record in which the
field LicenseID is the same as LicenseID on this form."
The next bit of code that starts with Private Sub
Print_Business_License_Click() seems to be for another command button named
Print_Business_License, and is the same except that you are opening the form
in preview mode (that is, it is displayed on the screen rather than being
printed immediately).
If you have two command buttons, one to print the form and the other to
preview it first, you will need both Click events, one for each button.
You may do well to use some more helpful error handling code. Instead of:
MsgBox Err.DESCRIPTION
you could have something like:
MsgBox "Error #" & Err.Number & " (" & Err.Description & ") in
Print_Business_License_Click"
KyBoy1976 said:
This makes some sense to me, however, I'm confused as where I insert the
lines of code. When I go to the Event for the print button function and
click the "..." to edit i go into visual basic editor. There are many
many
lines of code there, how do I know where to put this line of code you gave
me?
What is there now:
Option Compare Database
Private Sub Print_Official_License_Click()
On Error GoTo Err_Print_Official_License_Click
Dim stDocName As String
stDocName = "Official License"
DoCmd.OpenReport stDocName, acNormal
Exit_Print_Official_License_Click:
Exit Sub
Err_Print_Official_License_Click:
MsgBox Err.DESCRIPTION
Resume Exit_Print_Official_License_Click
End Sub
Private Sub Print_Business_License_Click()
On Error GoTo Err_Print_Business_License_Click
Dim stDocName As String
stDocName = "Official License"
DoCmd.OpenReport stDocName, acPreview
Exit_Print_Business_License_Click:
Exit Sub
Err_Print_Business_License_Click:
MsgBox Err.DESCRIPTION
Resume Exit_Print_Business_License_Click
End Sub