help with code

J

Jim VanGordon

Maybe you all can help me. I know a little VB but I'm no
expert. I've got a command button to print a report on
the form but it prints the whole report. How would I
alter the code so that it only prints that record? Here's
the code I've got so far

Private Sub Print_Report_Click()
On Error GoTo Err_Print_Report_Click

Dim stDocName As String

stDocName = "New Staking Forms Report"
DoCmd.OpenReport stDocName, acNormal

Exit_Print_Report_Click:
Exit Sub

Err_Print_Report_Click:
MsgBox Err.Description
Resume Exit_Print_Report_Click

End Sub
 
D

Dirk Goldgar

Jim VanGordon said:
Maybe you all can help me. I know a little VB but I'm no
expert. I've got a command button to print a report on
the form but it prints the whole report. How would I
alter the code so that it only prints that record? Here's
the code I've got so far

Private Sub Print_Report_Click()
On Error GoTo Err_Print_Report_Click

Dim stDocName As String

stDocName = "New Staking Forms Report"
DoCmd.OpenReport stDocName, acNormal

Exit_Print_Report_Click:
Exit Sub

Err_Print_Report_Click:
MsgBox Err.Description
Resume Exit_Print_Report_Click

End Sub

This sort of thing is very easy if the form is bound to a table that has
a primary key or other field(s) that uniquely identifies it. Then you
specify the current record's key value in a "WhereCondition" argument to
the OpenReport method. For example, suppose the current record is
uniquely identified by a (numeric) field named "ID". Then you could
change this line ...
DoCmd.OpenReport stDocName, acNormal

.... to this:

DoCmd.OpenReport stDocName, acNormal, , "ID=" & Me!ID

If the key field (which I'm calling ID for example) is a text field,
then you need this variation:

DoCmd.OpenReport stDocName, acNormal, , _
"ID=" & Chr(34) & Me!ID & Chr(34)

That puts quotes (") around the value.
 

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