Print and Close Command

  • Thread starter Thread starter Guest
  • Start date Start date
The wizard will walk you through building a print button and also a close
button. Just copy and paste the code to combine it into one button.
 
I had those two command in different buttons, how can I mix both to get the
print and close.

Private Sub Command1_Click()
On Error GoTo Err_Command1_Click
DoCmd.PrintOut
Exit_Command1_Click:
Exit Sub
Err_Command1_Click:
MsgBox Err.Description
Resume Exit_Command1_Click
End Sub

Private Sub Command2_Click()
On Error GoTo Err_Command2_Click
DoCmd.Close
Exit_Command2_Click:
Exit Sub
Err_Command2_Click:
MsgBox Err.Description
Resume Exit_Command2_Click
End Sub
 
Private Sub Command1_Click()
On Error GoTo Err_Command1_Click
DoCmd.PrintOut
DoEvents
DoCmd.Close

Exit_Command1_Click:
Exit Sub
Err_Command1_Click:
MsgBox Err.Description
Resume Exit_Command1_Click
End Sub

HTH
Van T. Dinh
MVP (Access)
 
Rick - Can't seem to get to post here any other way - hope you see this. Am
trying to create a command button on a data entry form that once I have
filled in the data, I can click on and it will print JUST that record on
another form in the DB. HELP!
 
Van - Sorry - could not get the system to let me post any other way - hope
you see this. Am trying to create a command button on a data entry form that
once I have filled in, I can click on, and it will print out another form for
only the data in the current record. Can't seem to figure it out/ HELP!
 
Rick - Can't seem to get to post here any other way - hope you see this. Am
trying to create a command button on a data entry form that once I have
filled in the data, I can click on and it will print JUST that record on
another form in the DB. HELP!

Don't print Forms - print REPORTS.

Are you storing the data in a Table (I hope)? or are you just trying
to print the volatile, unstored data on the form?

John W. Vinson[MVP]
 
John - yes - absolutely all is stored in tables, no transient data. I can
get it to print a report, but it tries to print all 806 records, not just the
currently selected record
 
John - yes - absolutely all is stored in tables, no transient data. I can
get it to print a report, but it tries to print all 806 records, not just the
currently selected record

Two options:

1. Base the Report on a Query which references a control on the form
as a criterion, using the syntax

=[Forms]![YourFormName]![YourControlName]

2. Edit the wizard-generated OpenReport code in the click event to
include a WhereCondition. It should have a line like

stCriteria = ""

Edit this to something like

stCriteria = "[ID] = " & Me.txtID

using your own field and control names.

John W. Vinson[MVP]
 
John -

I don't know if you remember or not, but I corresponded with you on a
problem I was having getting a command button on a Form to display a report
based ONLY on the current record. I have gotten OH SO CLOSE, but am still
apparently oh so far away, and am once again stymied. I am hoping you will
be able to help me find "the error of my ways"...

I own and run a small business that does covenant enforcement for Home
Owners Associations here in Colorado. I built this Access database a long
time ago to help me manage the many violations I have to write, and decided
recently, "Why am I writing these? I could just enter the data on the
computer in my vehicle and have it print each citation on the fly for me!?"
It has turned out for my feeble coding skills to be much easier said then
done, but I am determined to somehow lick it.

If you remember, I had a data entry form up, that I wanted to place a
command button on to print a report (a parking violation citation to be
precise) that when I clicked it, it sent the citation for that record to the
printer.

I entered the following code for the command button:

Private Sub CitationPrintPreview_Click()

Dim strReportName As String
Dim strCriterea As String

If NewRecord Then
MsgBox "This record contains no data. Please select a record to
print or Save this record.", vbInformation, "Invalid Action"
Exit Sub
Else
strReportName = "Citation"
strCriterea = "[Cite_#]= " & Me![Cite_#]
'strCriterea = "[Cite_#]='" & Me![Cite_#] & "'"

DoCmd.OpenReport strReportName, acViewPreview, , strCriterea

End If
End Sub

It's the closest I have gotten to success so far, however, when I execute
it, I am presented with the error message that says:

Run Time Error '3075';

Syntax Error (missing operator) in query
expression'([Cite_#]=20010317DD090032)'

The number displayed (20010317DD090032) is the correct citation number for
the record that is currently being displayed on the form that I want to print
a citation for, but I can't for the LIFE of me figure out what I'm missing or
what I have screwed up. Can you help me sir, please? Thank you SO much in
advance for your time and help with this - I really do appreciate it.

- Doug Brown


John Vinson said:
John - yes - absolutely all is stored in tables, no transient data. I can
get it to print a report, but it tries to print all 806 records, not just the
currently selected record

Two options:

1. Base the Report on a Query which references a control on the form
as a criterion, using the syntax

=[Forms]![YourFormName]![YourControlName]

2. Edit the wizard-generated OpenReport code in the click event to
include a WhereCondition. It should have a line like

stCriteria = ""

Edit this to something like

stCriteria = "[ID] = " & Me.txtID

using your own field and control names.

John W. Vinson[MVP]
 
I entered the following code for the command button:

Private Sub CitationPrintPreview_Click()

Dim strReportName As String
Dim strCriterea As String

If NewRecord Then
MsgBox "This record contains no data. Please select a record to
print or Save this record.", vbInformation, "Invalid Action"
Exit Sub
Else
strReportName = "Citation"
strCriterea = "[Cite_#]= " & Me![Cite_#]

This will work only if the [Cite_#] field is numeric. Looking at the
examples below it's clear that it's not.
'strCriterea = "[Cite_#]='" & Me![Cite_#] & "'"

This *should* work - just put a ' before the strCriteria on the line
above and remove the ' in front of this one. Does that do the job for
you?
DoCmd.OpenReport strReportName, acViewPreview, , strCriterea

End If
End Sub

It's the closest I have gotten to success so far, however, when I execute
it, I am presented with the error message that says:

John W. Vinson[MVP]
 
Closer, but still not it. The code now looks like:

Private Sub CitationPrintPreview_Click()

Dim strReportName As String
Dim strCriterea As String

If NewRecord Then
MsgBox "This record contains no data. Please select a record to
print or Save this record.", vbInformation, "Invalid Action"
Exit Sub
Else
strReportName = "Citation"
strCriterea = "[Cite_#]= " & Me![Cite_#]
'strCriterea = "[Cite_#]='" & Me![Cite_#] & "'"

DoCmd.OpenReport strReportName, acViewPreview, , strCriterea

End If
End Sub

At first, when I pulled it up, the line 'strCriterea = "[Cite_#]='" &
Me![Cite_#] & "'" as all in green (and remained so). I checked it to be
exactly as you wrote. Based on what I thought I understood you to say, I
removed the line strCriterea = "[Cite_#]= " & Me![Cite_#] that was
immediately above it, and I still got all 834 records. When I put it back
in, I get an Enter Parameter box A10 asking me to enter a value ( NO idea
what this is), if I just hit enter, it gives me a single rport, but
absolutely devoid of any data. Obviously, I am still screwing something up.

- Doug

John Vinson said:
I entered the following code for the command button:

Private Sub CitationPrintPreview_Click()

Dim strReportName As String
Dim strCriterea As String

If NewRecord Then
MsgBox "This record contains no data. Please select a record to
print or Save this record.", vbInformation, "Invalid Action"
Exit Sub
Else
strReportName = "Citation"
strCriterea = "[Cite_#]= " & Me![Cite_#]

This will work only if the [Cite_#] field is numeric. Looking at the
examples below it's clear that it's not.
'strCriterea = "[Cite_#]='" & Me![Cite_#] & "'"

This *should* work - just put a ' before the strCriteria on the line
above and remove the ' in front of this one. Does that do the job for
you?
DoCmd.OpenReport strReportName, acViewPreview, , strCriterea

End If
End Sub

It's the closest I have gotten to success so far, however, when I execute
it, I am presented with the error message that says:

John W. Vinson[MVP]
 
Well, scratch my last - am still getting the same error message - Syntax
error, missing operator. Is still selecting ht eright record, debug mode
indicates the problem is with my DoCmd line - at least that's what comes up
highlighted.......

- Doug

John Vinson said:
I entered the following code for the command button:

Private Sub CitationPrintPreview_Click()

Dim strReportName As String
Dim strCriterea As String

If NewRecord Then
MsgBox "This record contains no data. Please select a record to
print or Save this record.", vbInformation, "Invalid Action"
Exit Sub
Else
strReportName = "Citation"
strCriterea = "[Cite_#]= " & Me![Cite_#]

This will work only if the [Cite_#] field is numeric. Looking at the
examples below it's clear that it's not.
'strCriterea = "[Cite_#]='" & Me![Cite_#] & "'"

This *should* work - just put a ' before the strCriteria on the line
above and remove the ' in front of this one. Does that do the job for
you?
DoCmd.OpenReport strReportName, acViewPreview, , strCriterea

End If
End Sub

It's the closest I have gotten to success so far, however, when I execute
it, I am presented with the error message that says:

John W. Vinson[MVP]
 
Closer, but still not it. The code now looks like:

The code should be:

Private Sub CitationPrintPreview_Click()

Dim strReportName As String
Dim strCriterea As String

If NewRecord Then
MsgBox "This record contains no data. Please select a record
to
print or Save this record.", vbInformation, "Invalid Action"
Exit Sub
Else
strReportName = "Citation"
strCriterea = "[Cite_#]='" & Me![Cite_#] & "'"

DoCmd.OpenReport strReportName, acViewPreview, , strCriterea

End If
End Sub


if I understand your structure correctly. Bear in mind: I cannot see
your database. I do not know anything about your form or your table
structure, so I cannot be *sure* that opening the report and selecting
the value of the form control [Cite_#] will get the correct record or
records - but the above is the proper syntax if that is what you want
to do.

John W. Vinson[MVP]
 
IT WORKED!!! IT WORKED!!!! THANK YOU THANK YOU THANK YOU IT WORKED!!!!

I owe you good and most kind sir - Thank you SO much for all of your time
and effort leading a blind man through the fog!

- Doug

John Vinson said:
Closer, but still not it. The code now looks like:

The code should be:

Private Sub CitationPrintPreview_Click()

Dim strReportName As String
Dim strCriterea As String

If NewRecord Then
MsgBox "This record contains no data. Please select a record
to
print or Save this record.", vbInformation, "Invalid Action"
Exit Sub
Else
strReportName = "Citation"
strCriterea = "[Cite_#]='" & Me![Cite_#] & "'"

DoCmd.OpenReport strReportName, acViewPreview, , strCriterea

End If
End Sub


if I understand your structure correctly. Bear in mind: I cannot see
your database. I do not know anything about your form or your table
structure, so I cannot be *sure* that opening the report and selecting
the value of the form control [Cite_#] will get the correct record or
records - but the above is the proper syntax if that is what you want
to do.

John W. Vinson[MVP]
 
Back
Top