View Full Version : Button


Bluezman
11-15-2001, 06:42 AM
I'm trying to code a button on a form that will allow me to print a report for the specific record being displayed. I already have the report designed, but when I created the PRINT button, it starts printing the report from the first record, not the currently displayed record.

Am I missing something simple? Any help would be appreciated.

Bluezman

DJN
11-15-2001, 06:52 AM
You need to tell Access which record you want to print. The following sub will do just that. Just change the field and table names to suit.

Firstly, create a query using the table that has the record you want to print.
WHERE is the criteria required.

SELECT TableName.*, TableName.[PrimaryKeyFieldName]
FROM TableNaame
WHERE (((TableName.[PrimaryKeyFieldName])=[Forms]![FormName]![PrimaryKeyFieldName]));

Private Sub cmdPrintCurrentRecord_Click()

On Error GoTo Err_PrintCurrentRecord_Click

Dim strDocName As String

strDocName = "ReportName"
' Print Current record in a report, using YourQueryNamequery to print
' report for current record.
DoCmd.OpenReport strDocName, acViewNormal, "YourQueryName"

Exit_PrintCurrentRecord_Click:
Exit Sub

Err_PrintCurrentRecord_Click:
' If action was cancelled by the user, don't display an error message.
Const conErrDoCmdCancelled = 2501
If (Err = conErrDoCmdCancelled) Then
Resume Exit_PrintCurrentRecord_Click
Else
MsgBox Err.Description
Resume Exit_PrintCurrentRecord_Click
End If
End Sub

donbettis
11-15-2001, 06:59 AM
You could base your report on a query.

Make a query based on the table where the data is stored…

In the criteria for the field of say ContactID put some like this…

[Me]![ContactID]

This tells the query to only load the Record containing the current forms ContactID

EDIT BECAUSE:
The Above Reply was not here when I started this Reply and, becasue I fat fingered (aka TYPO) something.

[This message has been edited by donbettis (edited 11-15-2001).]

Bluezman
11-15-2001, 07:37 AM
Thanks for the help, it worked like a charm the first time out. I've found where this can come in very handy in other areas of this database as well, so you've saved me from one more day of hitting my head against the wall. As I heal, I'll be thanking you again and again.

Bluezman