Printing Problems. (1 Viewer)

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 00:37
Joined
Apr 1, 2019
Messages
731
Hi, I have the following code on my form to preview a filtered report in front of a popup form. This works fine. However, what I'd like to do is upon closing the form, is to prompt the user as to whether they wish to print the report or export it. I like the idea of using the DoCmd.RunCommand acCmdPrint to pop up the "windows" print options. I have the latter code on the 'On Close' event of the report. I get the error Message' 2585'. When I try this. If anyone can assist I'd be grateful. Also if there is a better technique then I'm 'all ears'. PS - I copied and pasted in the latter routine but could not format it as code. I'd appreciate someone explaining how this is done again. Cheers
Code:
Private Sub BttnPrint_Click()

Dim strWhere As String
On Error GoTo BttnOpenReport_Click_Err
If Me.Dirty Then
    Me.Dirty = False
End If
   
If Me.NewRecord Then
    MsgBox "Select a Record to Print"
Else

strWhere = "TaskID=" & taskID

    Forms![frmtask].Visible = False
    DoCmd.OpenReport "rptHazard", acViewPreview, , strWhere
    'DoCmd.RunCommand acCmdPrint
End If

BttnOpenReport_Click_Exit:
    Exit Sub

BttnOpenReport_Click_Err:
    MsgBox Error$
    Resume BttnOpenReport_Click_Exit

End Sub
Code:
Private Sub Report_Close()
DoCmd.OpenReport "rptHazard", acViewPreview, , strWhere
DoCmd.RunCommand acCmdPrint
Forms![frmtask].Visible = True
End Sub
 
Last edited:

Micron

AWF VIP
Local time
Today, 07:37
Joined
Oct 20, 2018
Messages
3,478
There are about 3000 error numbers. You could help us to help you by providing the message as well. I could look it up, but I'm trying to convert every newcomer, one at a time. Or I'm too lazy - take your pick. ;)

Oh, and please use code tags (</> on menu bar) for code. You can go back to your original post and add them if you care to.
 
Last edited:

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 00:37
Joined
Apr 1, 2019
Messages
731
Micron, 'Run-time error 2585:This action can't be carried out while processing a form or report event'. Occurs upon close and after the 'windows' print menu pops up. Did the code thing too.
 
Last edited:

Micron

AWF VIP
Local time
Today, 07:37
Joined
Oct 20, 2018
Messages
3,478
I would not expect that you could print a report that you just invoked the Close method on, and since you're not specifying which report, I take it that Access decides it is the one that you're closing (it may still be Active). AFAIK, you'll need to use the SelectObject method to specify the report, or pass an open arg to the next report and print it in the open event if the args property isn't null. In the latter case, I believe you'll need to move the print command there also.

Perhaps someone will correct me as I haven't used your approach as far as I can recall.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:37
Joined
May 7, 2009
Messages
19,233
add Timer Event to your Form, but set it's TimerInterval to 0 on the property sheet.
we will trigger the timer on the Report's Unload event:
Code:
Private Sub Form_Timer()
    Me.TimerInterval = 0
    'check if report is still open
    If SysCmd(acSysCmdGetObjectState, acReport, "rptHazard") <> 0 Then
        'close if it is
        DoCmd.Close acReport, "rptHazard"
    End If
End Sub

remove the Close event of the Report.
instead add code to it's Unload event:
Code:
Option Explicit

Private bolClose As Boolean

Private Sub Report_Unload(Cancel As Integer)
    If Not bolClose Then
        If MsgBox("Print the report before closing?", vbQuestion + vbYesNo) = vbYes Then
            Cancel = True
            Me.Visible = False
            bolClose = True
            DoCmd.PrintOut
        End If
    End If
    Forms!frmTask.TimerInterval = 300
    Forms!frmTask.Visible = True
End Sub
 

vhung

Member
Local time
Today, 04:37
Joined
Jul 8, 2020
Messages
235
Runtime Error 2585 happens when Microsoft Access fails or crashes whilst it’s running, hence its name. It doesn’t necessarily mean that the code was corrupt in some way, but just that it did not work during its run-time.

Report - A report contains summarized information from a data source, usually in an end-user-friendly format, such as PDF or Excel, although proprietary reporting tools usually with built-in design tools also exist.

>either you have to add code during OnCloseForm; start with Dim answer = msgbox(), vbinformation/question + vbOkCancel
> if answer =vbOk then
DoCmd.RunCommand acCmdPrint ;"YourReport"
else
answer =vbCancel then: exit sub
end if
 

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 00:37
Joined
Apr 1, 2019
Messages
731
Gents, thanks. Will give this a go. Appreciate it.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 00:37
Joined
Apr 1, 2019
Messages
731
arnelgp, I owe you an update. I tried your code but it didn't work. I didn't have time to examine why but intend to revisit it. I really like the idea of enlisting the 'familiar' windows print menu, because it offers multiple print options. Would love to see an example. Appreciate your help.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:37
Joined
May 7, 2009
Messages
19,233
this is a sample.
 

Attachments

  • sampleFormReport.zip
    36.4 KB · Views: 288

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 00:37
Joined
Apr 1, 2019
Messages
731
Arnelgp, champion. I'll have a go. Cheers
 

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 00:37
Joined
Apr 1, 2019
Messages
731
arnelgp, works great. I substituted 'DoCmd.RunCommand acCmdPrint' for 'DoCmd.Printout. Now I see the print dialog box that allows me to print to file, change file format or select a different printer. Exactly what I want. This is a very short piece of code to do so much. Seems too good to be true. Are there any limitations I need to be aware of? Really appreciate your efforts.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:37
Joined
May 7, 2009
Messages
19,233
you have to find out if there are limitations.
 

Users who are viewing this thread

Top Bottom