Current Record Report? (1 Viewer)

Hydra427

Registered User.
Local time
Today, 04:24
Joined
Mar 9, 2012
Messages
40
I have a form that I use to enter diecast cars into my database. At the end of the form I have a text box "Label Printed" That shows if a label has been printed for that current record on the form. I am adding a print button to the form and want it to print the report "Box Label Report" for the current record on the form. Can anyone give me some help on the code needed to print the report based on the current record.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:24
Joined
Feb 19, 2002
Messages
43,223
Please don't ask the same question twice. See my answer in your other thread
 

Hydra427

Registered User.
Local time
Today, 04:24
Joined
Mar 9, 2012
Messages
40
OK, I think I figured out a way to make this work, it may be clunky but it does the job. I would prefer if the report never opened and just closed if someone knows a better solution let me know.

Code:
Private Sub Command14_Click()
DoCmd.OpenReport "Labels Table1", acViewPreview, acHidden, "[ID]=" & Me.ID
DoCmd.PrintOut , , , , 1
DoCmd.Close acReport, "Labels Table1", acSaveYes
Me.[Text Label] = "Yes"
End Sub
 

Hydra427

Registered User.
Local time
Today, 04:24
Joined
Mar 9, 2012
Messages
40
I tried all three suggestions but now it prints the record on the form and then prints the form with all records. So instead of only getting the one record I am getting a page with the record in question and then the form prints with all 10 records.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:24
Joined
Aug 30, 2003
Messages
36,124
You would delete the PrintOut and Close lines.
 

Hydra427

Registered User.
Local time
Today, 04:24
Joined
Mar 9, 2012
Messages
40
You would delete the PrintOut and Close lines.
Is that with the acNormal, or the way I have code written now. I want the button to print the report when I push it and not have it open the report so I can hit the print button on the report page when it opens.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:24
Joined
Aug 30, 2003
Messages
36,124
With the acNormal. That line prints the report with the selected record, making the next 2 lines unnecessary. In fact the PrintOut line is printing out the form, which is why you're getting all records.
 

Hydra427

Registered User.
Local time
Today, 04:24
Joined
Mar 9, 2012
Messages
40
With the acNormal. That line prints the report with the selected record, making the next 2 lines unnecessary. In fact the PrintOut line is printing out the form, which is why you're getting all records.
Thank you, can you explain the acHidden and what its function is as well. I tried reading online about it but to no avail.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:24
Joined
Aug 30, 2003
Messages
36,124
You don't need it with acNormal, the report won't open visibly anyway. You'd use acHidden if you were opening the report for printing or exporting but didn't want the user to see it open/close (it would probably be so fast it would just look like flicker). I sometimes use it when I want to export a report filtered with the wherecondition:

DoCmd.OpenReport...
DoCmd.OutputTo...
DoCmd.Close
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:24
Joined
Feb 19, 2002
Messages
43,223
DoCmd.OpenReport "Labels Table1", acViewPreview, acHidden, "[ID]=" & Me.ID
DoCmd.PrintOut , , , , 1
If you always want to go directly to print use the OpenReport method with the correct arguments. The PrintOut is not necessary.

I generally give my users an option as to what they want to do when they open something.


RptOptionsJPG.JPG


There's a bunch of validation code to make sure that required variables have been entered, then a case statement to do something with the selected report.
Code:
    If IsNull(strReport) Then
        MsgBox "Please select a report.", vbOKOnly
        Me.lstReports.SetFocus
        Exit Sub
    Else
        Select Case Me.fraOutputTo
            Case 1      'Preview
                DoCmd.OpenReport strReport, acViewPreview
            Case 2      'Print
                DoCmd.OpenReport strReport, acViewNormal
            Case 3      'Export to PDF
                If Me.txtPath & "" = "" Then
                    MsgBox "Please select a path.", vbOKOnly
                    Me.cmdBrowse.SetFocus
                    Exit Sub
                End If               
                strFileName = Me.txtPath & "\" & Me.cboJob.Column(1) & "_" & strReport & "_" & Format(Date, "yyyymmdd") & ".pdf"
                DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strFileName
            Case 4      'Export to Excel
                If Me.txtPath & "" = "" Then
                    MsgBox "Please select a path.", vbOKOnly
                    Me.cmdBrowse.SetFocus
                    Exit Sub
                End If
                strFileName = Me.txtPath & "\" & Me.cboJob.Column(4) & "_" & Me.txtExcelQueryName & "_" & Me.cboJob.Column(1) & "_" & Format(Date, "yyyymmdd") & ".xls"
                Kill strFileName
                If Me.lstReports.Column(3) = "P-01" Then        'Weekly Job Status
                    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, Me.txtExcelQueryName, strFileName, False
                    Call FormatWeeklyJobStatus(strFileName)
                Else
                    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, Me.txtExcelQueryName, strFileName, True
                End If
                MsgBox "Export Complete - File name = " & strFileName
        End Select
    End If
 

Users who are viewing this thread

Top Bottom