Printing from a 'Form'

B

BetaMike

Hi,
In an effort to reduce switching from 'Forms' to 'Reports' all the time, I
inserted a 'Print' command button on a form that points to the relevant
report.

Can I set it up so that it only prints the current record? It tried to print
105 reports earlier!

Thanks.
 
B

BetaMike

I managed to find a work-around by using a 'YES/NO' tickbox in the Table and
a '=No' criteria in the Query. The 'Form' still shows 105 records but the
'Report' only produces the 'unticked' records as it is based on the Query.
So I can now print directly from the form :)
 
G

Gary Walter

BetaMike said:
In an effort to reduce switching from 'Forms' to 'Reports' all the time, I
inserted a 'Print' command button on a form that points to the relevant
report.

Can I set it up so that it only prints the current record? It tried to
print
105 reports earlier!
Here are 2 simple possible examples.

(change pk, rptMyReport, txtpk to match your
field name, report name, textbox name)

The first assumes a primary index of type string
shown in textbox "txtpk" on form:

Private Sub cmdPreviewReport_Click()
On Error GoTo Err_cmdPreviewReport_Click

Dim stDocName As String
Dim strWhere As String
Const cQuote = """"

stDocName = "rptMyReport"
strWhere = "[pk] = " & cQuote & Me!txtpk & cQuote
DoCmd.OpenReport stDocName, acPreview, , strWhere


Exit_cmdPreviewReport_Click:
Exit Sub

Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click

End Sub

///////////////////////////////////////////////////////////////////////
If primary index is of type Long and shown
in textbox "txtpk" on form:

Private Sub cmdPreviewReport_Click()
On Error GoTo Err_cmdPreviewReport_Click

Dim stDocName As String
Dim strWhere As String

stDocName = "rptMyReport"
strWhere = "[pk] = " & CLng(Me!txtpk)
DoCmd.OpenReport stDocName, acPreview, , strWhere


Exit_cmdPreviewReport_Click:
Exit Sub

Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click

End Sub
 
B

BetaMike

Thanks for replying, I can't get your instructions to work but I'm nearly
there with these: - http://www.allenbrowne.com/casu-15.html

The 'pk' has been setup in the query as a required parameter. Opening the
query or the report and entering a Date produces the correct report so that
part works fine.

However, when clicking the 'Print' command button on the form - it opens up
the report but it is has no data. It is as though I have not typed in the
Date parameter!

Any suggestions?

Thanks.





Gary Walter said:
BetaMike said:
In an effort to reduce switching from 'Forms' to 'Reports' all the time, I
inserted a 'Print' command button on a form that points to the relevant
report.

Can I set it up so that it only prints the current record? It tried to
print
105 reports earlier!
Here are 2 simple possible examples.

(change pk, rptMyReport, txtpk to match your
field name, report name, textbox name)

The first assumes a primary index of type string
shown in textbox "txtpk" on form:

Private Sub cmdPreviewReport_Click()
On Error GoTo Err_cmdPreviewReport_Click

Dim stDocName As String
Dim strWhere As String
Const cQuote = """"

stDocName = "rptMyReport"
strWhere = "[pk] = " & cQuote & Me!txtpk & cQuote
DoCmd.OpenReport stDocName, acPreview, , strWhere


Exit_cmdPreviewReport_Click:
Exit Sub

Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click

End Sub

///////////////////////////////////////////////////////////////////////
If primary index is of type Long and shown
in textbox "txtpk" on form:

Private Sub cmdPreviewReport_Click()
On Error GoTo Err_cmdPreviewReport_Click

Dim stDocName As String
Dim strWhere As String

stDocName = "rptMyReport"
strWhere = "[pk] = " & CLng(Me!txtpk)
DoCmd.OpenReport stDocName, acPreview, , strWhere


Exit_cmdPreviewReport_Click:
Exit Sub

Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click

End Sub
 
G

Gary Walter

What is the recordsource of the form?

What is the name of the field in that recordsource that
says "I am a record distinct from all the other
records in the recordsource because of me"?

What is the name of the textbox on the form
that contains this field?

What is the recordsource of your report?
I assume it is the same as the form's recordsource?

What is the code you have so far for the command
button that is to print this one record?

If a date field is the 'pk' then you will need to delimit
with "#"

strWhere = "[pk] = #" & Me!txtpk & "#"

Truly, I've never seen data where a date field
is the primary key, but I don't know everything!
Thanks for replying, I can't get your instructions to work but I'm nearly
there with these: - http://www.allenbrowne.com/casu-15.html

The 'pk' has been setup in the query as a required parameter. Opening the
query or the report and entering a Date produces the correct report so
that
part works fine.

However, when clicking the 'Print' command button on the form - it opens
up
the report but it is has no data. It is as though I have not typed in the
Date parameter!


Gary Walter said:
BetaMike said:
In an effort to reduce switching from 'Forms' to 'Reports' all the
time, I
inserted a 'Print' command button on a form that points to the relevant
report.

Can I set it up so that it only prints the current record? It tried to
print
105 reports earlier!
Here are 2 simple possible examples.

(change pk, rptMyReport, txtpk to match your
field name, report name, textbox name)

The first assumes a primary index of type string
shown in textbox "txtpk" on form:

Private Sub cmdPreviewReport_Click()
On Error GoTo Err_cmdPreviewReport_Click

Dim stDocName As String
Dim strWhere As String
Const cQuote = """"

stDocName = "rptMyReport"
strWhere = "[pk] = " & cQuote & Me!txtpk & cQuote
DoCmd.OpenReport stDocName, acPreview, , strWhere


Exit_cmdPreviewReport_Click:
Exit Sub

Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click

End Sub

///////////////////////////////////////////////////////////////////////
If primary index is of type Long and shown
in textbox "txtpk" on form:

Private Sub cmdPreviewReport_Click()
On Error GoTo Err_cmdPreviewReport_Click

Dim stDocName As String
Dim strWhere As String

stDocName = "rptMyReport"
strWhere = "[pk] = " & CLng(Me!txtpk)
DoCmd.OpenReport stDocName, acPreview, , strWhere


Exit_cmdPreviewReport_Click:
Exit Sub

Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click

End Sub
 
B

BetaMike

Thanks Gary, your methodical questions have helped me to understand how it
should all fit together :)

The form recordsource is a table called Response
The report recordsource is a query called ResponseCalculations (Oops!)

I've fixed the problem by creating a 'Preview' button with the following
code: -
Private Sub Preview_Click()
On Error GoTo Err_Preview_Click
Dim stDocName As String
stDocName = "R - FAX - Alarm Response"
DoCmd.OpenReport stDocName, acPreview
Exit_Preview_Click:
Exit Sub
Err_Preview_Click:
MsgBox Err.Description
Resume Exit_Preview_Click
End Sub

This works fine now as the command simply opens the report, whereby the
query parameter kicks in and I enter the date of the 'incident'. I used the
date field, as we type up incident reports, print them off and fax them on a
daily basis. It doesn't matter if we have 1 or 10 reports.

Thanks again.

Gary Walter said:
What is the recordsource of the form?

What is the name of the field in that recordsource that
says "I am a record distinct from all the other
records in the recordsource because of me"?

What is the name of the textbox on the form
that contains this field?

What is the recordsource of your report?
I assume it is the same as the form's recordsource?

What is the code you have so far for the command
button that is to print this one record?

If a date field is the 'pk' then you will need to delimit
with "#"

strWhere = "[pk] = #" & Me!txtpk & "#"

Truly, I've never seen data where a date field
is the primary key, but I don't know everything!
Thanks for replying, I can't get your instructions to work but I'm nearly
there with these: - http://www.allenbrowne.com/casu-15.html

The 'pk' has been setup in the query as a required parameter. Opening the
query or the report and entering a Date produces the correct report so
that
part works fine.

However, when clicking the 'Print' command button on the form - it opens
up
the report but it is has no data. It is as though I have not typed in the
Date parameter!


Gary Walter said:
:
In an effort to reduce switching from 'Forms' to 'Reports' all the
time, I
inserted a 'Print' command button on a form that points to the relevant
report.

Can I set it up so that it only prints the current record? It tried to
print
105 reports earlier!

Here are 2 simple possible examples.

(change pk, rptMyReport, txtpk to match your
field name, report name, textbox name)

The first assumes a primary index of type string
shown in textbox "txtpk" on form:

Private Sub cmdPreviewReport_Click()
On Error GoTo Err_cmdPreviewReport_Click

Dim stDocName As String
Dim strWhere As String
Const cQuote = """"

stDocName = "rptMyReport"
strWhere = "[pk] = " & cQuote & Me!txtpk & cQuote
DoCmd.OpenReport stDocName, acPreview, , strWhere


Exit_cmdPreviewReport_Click:
Exit Sub

Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click

End Sub

///////////////////////////////////////////////////////////////////////
If primary index is of type Long and shown
in textbox "txtpk" on form:

Private Sub cmdPreviewReport_Click()
On Error GoTo Err_cmdPreviewReport_Click

Dim stDocName As String
Dim strWhere As String

stDocName = "rptMyReport"
strWhere = "[pk] = " & CLng(Me!txtpk)
DoCmd.OpenReport stDocName, acPreview, , strWhere


Exit_cmdPreviewReport_Click:
Exit Sub

Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top