help with code

  • Thread starter Thread starter Jim VanGordon
  • Start date Start date
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
 
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.
 
Back
Top