Results 1 to 8 of 8
  1. #1
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    docmd.PrintOut (Access97)

    I am using docmd.printout to print pathology reports.The code is shown below.

    My problem is that sometimes it prints the Main Menu but mostly it does the right thing.

    The situation is that from the Main Menu a form with a list of reports waiting to be printed is displayed. Each report has a check box and the user ticks off the reports they want, then clicks the Print button.

    The reason for using docmd.printout is that if the report is going to run to more than a specified number of pages it won't be printed. Instead it will be printed later on large paper. (I have cut out this code for the moment.)

    The number of copies needed also has to be worked out, and this depends on a few things.

    As far as I can tell, the problem occurs with the first report in a batch. The report opens in preview mode, and the number of pages calculated. Then the Main Menu prints out.

    For the first report to be printed, there is a message box that is really telling the user to make sure that the right paper is in the printer. While I am writing this, it occurs to me that this might be the problem.
    Does clicking "OK" on a message box while in print preview mode move the focus to another screen invisibly?
    I have just removed the message box and done a couple more without problems, but the problem has always been intermittent.

    <pre>Private Sub cmdPrint_Click()
    Dim NoPages As Integer
    Dim stDocName As String
    Dim stcond As String
    Dim CopiestoPrint As Integer
    CopiestoPrint = DLookup("[NumCopies]", "tblPreferences")
    Dim extraCopies As Integer
    Dim PageLimit As Long
    PageLimit = DLookup("[ReportPageLimit]", "tblPreferences")
    Dim blFirst As Boolean
    blFirst = True
    Dim db As DAO.Database
    Set db = CurrentDb()
    Dim sql As String
    sql = "SELECT tblReports.* , tblCases.CaseStatus FROM tblReports INNER JOIN tblCases _
    ON tblCases.RefID = tblReports.RefID where ((Complete = True) _
    and isNull(ReportPrinted) and ([Select]=true))"
    DoCmd.RunCommand acCmdSaveRecord
    Dim rst As DAO.Recordset
    Set rst = db.OpenRecordset(sql, dbOpenDynaset)
    If Not rst.EOF Then
    rst.MoveFirst
    Do While Not rst.EOF
    extraCopies = Nz(DCount("ProviderNo", "tblcopiesto", "[ReferralNo]= " & rst![RefID]), 0)
    CopiestoPrint = CopiestoPrint + extraCopies
    stcond = "[ReportId] = " & rst!reportID
    If rst!Category = "H" Then
    stDocName = "rptHistotestreport"
    Else
    stDocName = "rptMycotestreport"
    End If
    DoCmd.OpenReport stDocName, acViewPreview, , stcond
    If Screen.ActiveReport.Label95.Visible = True Then
    CopiestoPrint = CopiestoPrint + 1
    End If
    If Screen.ActiveReport.Label98.Visible = True Then
    CopiestoPrint = CopiestoPrint + 1
    End If
    NoPages = Screen.ActiveReport.Pages
    If NoPages <= PageLimit Then
    If blFirst Then
    blFirst = False
    msgbox ("Small format reports will be printed first.")
    End If
    DoCmd.PrintOut acPrintAll, , , , CopiestoPrint
    DoCmd.Close acReport, stDocName
    End If
    rst.MoveNext
    Loop
    End If
    rst.Close
    DoCmd.OpenQuery "qryClearselected"
    Me.Requery

    Set rst = Nothing
    Set db = Nothing

    End Sub
    </pre>

    Regards
    John



  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: docmd.PrintOut (Access97)

    I have experienced similar problems. The name of the report the user has opened is available in the variable stDocName. You can use this to set focus to the report after showing the message box, immediately before printing the report:

    <font face="Georgia">If blFirst Then
    blFirst = False
    MsgBox ("Small format reports will be printed first.")
    End If
    DoCmd.SelectObject acReport, stDocName
    DoCmd.PrintOut acPrintAll, , , , CopiestoPrint</font face=georgia>

  3. #3
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: docmd.PrintOut (Access97)

    Thanks Hans

    That extra line seems to do the trick.

    I have since realised there may be another explanation of what is going on.

    I have an 'on timer' event on the Main Menu. (Most of the data for this db is imported; the event checks for data waiting to be imported.)
    I wonder if the ontimer event occuring is putting the focus back to the Menu.
    The SelectObject command should still fix this, although I suppose there is still a slight chance the timer event might occur between the SelctObject line and the PrintOut line.
    Regards
    John



  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: docmd.PrintOut (Access97)

    I would suggest you pause the timer while you're printing. Timers don't work all that well in Access forms and they will interfere with other operations in Access.
    Charlotte

  5. #5
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: docmd.PrintOut (Access97)

    I don't like the timer event much at all. I used to make them click a button to check for new records to import, and they complained about that and so I added the timer.

    It seems to me that setting the timerinterval to 0 in the Menu's OnLostFocus event I will pause the timer whenever the Main Menu is not active.
    I can set it back to its normal value in the OnGotFocus event.

    Does that seem a safe way to do it? Or should I use the Deactivate / Activate events?
    Regards
    John



  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: docmd.PrintOut (Access97)

    I would guess that the Activate/Deactivate events of the form are more dependable - when you switch to another window, the LostFocus event of the form only occurs if no control had the focus, otherwise the LostFocus event of the active control occurs, but not that of the form.

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: docmd.PrintOut (Access97)

    I second Hans' motion. Use the Activate/Deactivate events.
    Charlotte

  8. #8
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: docmd.PrintOut (Access97)

    Thanks again, I will put this in place.
    Regards
    John



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •