Print Report from a Form

A

Alex

I have a report with a datasource Goals table. I also have a form with a
datasource Goals table. I'd like to create a macro that I can assign to a
toolbar button that prints the current record that is open in the form in the
report. I'm not sure if it matters, but the Goals table has a multiple
primary key of 3 fields; rcdl, desk & goaldate.

Thanks for your help.
 
K

Klatuu

Create a command button on the form where the click event has the OpenReport
method to open the report. Use the values for the current record on the form
to build the Where condition for the OpenReport method. See OpenReport in
VBA editor help for more details.
 
A

Allen Browne

Forms are the interface in Access.
Tables are really just buckets to hold the database.
You will find it easier to create a form with a command button than to try
to create a generic toolbar button that determines what the key is.

On your form, you will have a command button for printing the report. For
the code to put in the Click event procedure of this form, see:
Print the record in the form
at:
http://allenbrowne.com/casu-15.html

In your case, you have a complex key, so the strWhere will need to read all
3 fields, e.g.:

strWhere = "(rcdl = " & Me.rcdl & ") AND (desk = """ & Me.desk & _
""") AND goalDate = " & Format(Me.goaldate, "\#mm\/dd\/yyyy\#") & ")"

That example assumes that:
- rcdl is a number field (so has no delimters);
- desk is a text field (so has " as delimiter);
- goaldate is a number field (so as # as delimiter).
Modify the delimters to match your actual field types.
 
R

Rick B

If you add a button to your form, the wizard will walk you right through
printing a report. It will even let you tell it to only print for the
current record.
 
A

Alex

Thanks - I gave this code a try, but the report opens with the first record,
regardless of which one is currently open in the form. Maybe I have
incorrect field names. Example: in "(rcdloc = "" & Me.Rldc & ""), are these
table, form or report field names? Thanks

Here's my code:
Private Sub PrintGoal_Click()
Dim strWhere As String
If Me.Dirty Then 'Saves any edits
Me.Dirty = False
End If
If Me.NewRecord Then 'Check to see if there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "(rcdloc = "" & Me.Rldc & "") AND (deskcode = "" & Me.desk & "")
AND (GoalDate = " & Format(Me.GoalDate, "\#mm\/dd\/yyyy\#") & ")"
DoCmd.OpenReport "rptPrintGoal", acViewPreview, strWhere
End If
End Sub
 
A

Allen Browne

The first name in the string is the field name in the table.
The second is the matching text box in your form.

It's like:
strWhere = "(Field1] = " & Me.Text0 & ...

If you created your form using a wizard, the text box will have the same
name as the field it is bound to.
 
Top