Printing a report from a form

G

Guest

without opening to preview the report I would like the report to print from
the command button on the form. this is the last field before going to the
next record. the autonumber field is the unigue identifier. i keep getting
all the records printing out instead of just the one with focus. i tried
several docmds that have been suggested, but with no luck so far.
 
G

Guest

You need to use the Where argument to tell the report to print only the
current record. Since I don't know the names of your table fields or form
controls, I will use the following as examples. You will need to change the
name to yours.

DoCmd.OpenReport "MyReportName", acNormal, , "[AUTO_NUMBER_FIELD] = " &
Me.txtAutoNumber

MyReportName = The name of your report.
acNormal means go straight to the printer
[AUTO_NUMBER_FIELD] = the field in your table that is the primary key
txtAutoNumber = the control on your form that is the primary key of the
record you want to print.

Now, there is one other thing to consider. If this is a brand new record
you have just entered, the report will not find it because it is not yet in
the table. It is only in the form's recordset. To get it into the table,
you will need requery the form. The problem here is that whenever you do a
form requery, the recordset goes back to the first record in the recordset,
so you need to reposition your form to the record you were on. Here is some
"air" code to do that.

If Me.NewRecord then
lngKeyValue = Me.txtAutoNumber
Me.Requery
set rst = Me.RecordsetClone
rst.FindFirst "[AUTO_NUMBER_FIELD] = " & lngKeyValue
Me.BookMark = rst.Bookmark
set rst = Nothing
End If
 
S

Steve Schapel

Did the DoCmds you tried include these?...
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "YourReport", , , "AutonumberField=" & Me.AutonumberField
 

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