Results 1 to 10 of 10
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can I log print events?

    I need to create a log file (or maybe append a table) to monitor report printouts. This is so I can record items (such as a contract number and name) to calculate print costs for particular contracts.

    The code below prints a report for the current record only. Is it possible to add some further code to this event to accomplish what I need to do?

    Any ideas gratefully accepted.

    Private Sub cmdPrintReport_Click()
    On Error GoTo Err_cmdPrintReport_Click

    Dim stDocName As String
    Dim strFilter As String

    stDocName = "rptSites"
    strFilter = "SiteID = Forms!frmSites!SiteID"
    DoCmd.OpenReport stDocName, acNormal, , strFilter

    Exit_cmdPrintReport_Click:
    Exit Sub

    Err_cmdPrintReport_Click:
    MsgBox Err.Description
    Resume Exit_cmdPrintReport_Click

    Dim strDocName As String


    End Sub

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

    Re: How can I log print events?

    There are several ways you could do this, but are you trying to determine if the report actually prints or could the user simply preview the report without printing? If you aren't doing any previews where the print could be cancelled, you could simply add some code here to execute the SQL for an append query. Something like:

    <pre>Dim strSQL as String
    strSQL = "Insert Into tblPrintLog ( PrintDate, SiteID ) " _
    & " Values (#" & Date() & "#, " & SiteID & " )"
    CurrentDb.Execute strSQL</pre>


    It's also possible to use the WithEvents keyword on the form that calls the reports and trap the events of the report so that you can respond appropriately if the report turns out to have NoData or (in a PrintPreview arrangement) when the report is closed. I've attached a sample Access 97 database that demonstrates this method. The same code and techniques will work in Access 2000 as well.
    Charlotte

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How can I log print events?

    Thanks, but where do I find the attachments you mention?

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

    Re: How can I log print events?

    Hmm, that's wierd. I attached the file but the attachment is gone. I'll try again.
    Attached Files Attached Files
    Charlotte

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How can I log print events?

    Thanks, I've got the attachment.
    Where it says 'enter code to log printing' do you mean the simple code from your first reply.
    Also, I'm unable to figure out how to add another field (say "ContractNo") to the append query. I'm pretty new to all this and a bit out of my depth.
    Lastly, and I think this is way, way out of my depth, would it be possible to extract the user id (either the database user - although no one is logging onto the database yet - or the windows user) to log who did the printing?

  6. #6
    Star Lounger
    Join Date
    Feb 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How can I log print events?

    I didn't look at Charlotte's attachment, so I won't get involved in the bit about altering the query.

    To get the current logged in user in Access 97, use the following:
    Environ("USERNAME")
    This returns the login name and it works in NT4 but from memory there was something slightly different in Win98. Maybe it was single quotes or none at all. I can't remember.

    Once you've added a field to your query, you can use the above function as the data you supply, either in a normal query or in VBA.

    ps. Charlotte, G'day mate 'ow ya goin' orright?

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

    Re: How can I log print events?

    I put the messagebox in so you could see when the event fired and that you could insert code within that event procedure to write your log entry. The sample wasn't intended to be anything but an example of how you could do it.

    As for the query, if you need to add a field, you could do it like this:

    <pre> Dim strSQL as String
    strSQL = "Insert Into tblPrintLog ( PrintDate, SiteID, " _
    & "ContractNo ) " _
    & " Values (#" & Date() & "#, " & SiteID _
    & "," & ContractNo & " )"
    CurrentDb.Execute strSQL</pre>


    See Marty's reply for one way to get the network login, although it can differ between versions of Windows and network operating system, so you'll have to do some experimenting.
    Charlotte

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

    Re: How can I log print events?

    Marty,

    How do I rate the Aussie version of "Hello"? I'm strictly a California girl, although maybe all the antipodeans in the Lounge are rubbing off on me! Anyway, g'die back at you!
    Charlotte

  9. #9
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How can I log print events?

    The problem I've got with the query is how to add a text field. It works fine when I add a additional field that is a number but I can't seem to get it to work for text. I get one of two errors depending on whether the field contains one (Too few parameters. Expected 1) or more than one word(Syntax error. Missing Operator in query expression ' firstword secondword')

  10. #10
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: How can I log print events?

    Hi,
    If you're inserting a text field you need to include quotes around the value you're inserting, so if, for example, ContractNo is a text field you would change Charlotte's code to read:
    Dim strSQL as String
    strSQL = "Insert Into tblPrintLog ( PrintDate, SiteID, " _
    & "ContractNo ) " _
    & " Values (#" & Date() & "#, " & SiteID _
    & ",<font color=blue>'</font color=blue>" & ContractNo & "<font color=blue>'</font color=blue> )"
    CurrentDb.Execute strSQL

    (I've highlighted the quotes in blue)
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

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