How do I print a report off a command button, current record only

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to print a report from a form. I only want to print the current
record. I can get the button to print all the records, but I only want it to
print the current record.
 
on the print command line assign a where condition

Dim MyWhereCondition as string
' if value number use that
MyWhereCondition = "MyFieldNameInTable= " & me.FieldNameInTheForm
' if value text use that
MyWhereCondition = "MyFieldNameInTable= '" & me.FieldNameInTheForm & "'"
docmd.OpenReport "ReportName",,,MyWhereCondition
 
Add a button to the form, on the OnClick event of the button write the code
to print the report
--
If I answered your question, please mark it as an answer. That way, it will
stay saved for a longer time, so other can benifit from it.

Good luck
 
Dim MyWhereCondition as string
' if value number use that
MyWhereCondition = "MyFieldNameInTable= " & me.FieldNameInTheForm
' if value text use that
MyWhereCondition = "MyFieldNameInTable= '" & me.FieldNameInTheForm & "'"
docmd.OpenReport "ReportName",,,MyWhereCondition
================================================
MyFieldNameInTable - Every report is bound (record source) to a table, when
you want to open the report on a specific record you need to filter the
report on a crtain value that you want to pass from the form.
So the field value in the form should correspond to a field in the report.

So basically the MyFieldNameInTable should be the name of the field in the
table that the report is bound to
=================================================
FieldNameInTheForm - The name of the field in the form that you want to get
the value from.
--
If I answered your question, please mark it as an answer. That way, it will
stay saved for a longer time, so other can benifit from it.

Good luck
 
The field I'll use is the invoiceID field. The code text below is code I
enter in Visual Basic, correct? Exactly, where do put this information in.
I put it in and I got run-time error 3464
 
Here is the code:
Private Sub Command176_Click()
Dim MyWhereCondition As String
' if value number use that
MyWhereCondition = "InvoiceID= " & Me.InvoiceID
' if value text use that
MyWhereCondition = "InvoiceID= '" & Me.InvoiceID & "'"
DoCmd.OpenReport " Invoices ", , , MyWhereCondition
On Error GoTo Err_Command176_Click

Dim stDocName As String

stDocName = "Invoices"
DoCmd.OpenReport stDocName, acPreview

Exit_Command176_Click:
Exit Sub

Err_Command176_Click:
MsgBox Err.Description
Resume Exit_Command176_Click

End Sub

This is my first time using visual basic, so I need all the help I can get.
 
Try this

Private Sub Command176_Click()
On Error GoTo Err_Command176_Click

Dim MyWhereCondition As String

MyWhereCondition = "InvoiceID= " & Me.InvoiceID
DoCmd.OpenReport " Invoices ",acPreview , , MyWhereCondition

Exit_Command176_Click:
Exit Sub

Err_Command176_Click:
MsgBox Err.Description
Resume Exit_Command176_Click

End Sub

--
If I answered your question, please mark it as an answer. That way, it will
stay saved for a longer time, so other can benifit from it.

Good luck
 
Back
Top