Results 1 to 8 of 8
  1. #1
    4 Star Lounger
    Join Date
    May 2002
    Location
    Roanoke, Virginia, USA
    Posts
    493
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Print Routines (A2K SP3)

    I currently have several macrs that are used in printing the same reports from several difference forms:

    I have tried the following private sub which seems to do what I need. However, I want to be able to print the same report from different forms. Rather than putting the code in the OnClick event of each form, can it be called similar to a macro which only appears once.

    Private Sub cmdPrintReceipt2_Click()
    On Error GoTo Err_cmdPrint_Receipt2_Click

    Dim strDocName As String
    Dim strWhere As String
    strDocName = "rptReceipt2"
    strWhere = "[ReceiptNumber]=[Forms]![frmCheckInEdit]![ReceiptNumber]"
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    DoCmd.OpenReport strDocName, acPreview, , strWhere

    Exit_cmdPrint_Receipt2_Click:
    Exit Sub

    Err_cmdPrint_Receipt2_Click:
    MsgBox Err.Description
    Resume Exit_cmdPrint_Receipt2_Click


    End Sub


    Tom

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,624
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Print Routines (A2K SP3)

    You can declare the sub as public, which will make it accessiable to all forms, or you can put it in a general-purpose module and call it from each form.
    Wendell

  3. #3
    4 Star Lounger
    Join Date
    May 2002
    Location
    Roanoke, Virginia, USA
    Posts
    493
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Print Routines (A2K SP3)

    Is one method better than the other?

    If you declare it as Public Sub do you just change the Private to Public? What is the syntax when calling the public sub from other On Click events on different forms..

    If the sub is in a seperate Module, would it be public or private and how is it referenced from the on click event on the form.

    As you can tell, I am just getting started with VBA and need a lot of help.

    Tom

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

    Re: Print Routines (A2K SP3)

    What exactly do you want to do? Making the sub public by changing Private to Public means that the rest of the application can trigger the event for *this* form when the form is open. It doesn't allow other forms to print the report otherwise, nor does it allow them to print other reports. True, you can create a public PrintReport event, but in Access 97 and later, you don't need the Access 95 code that the wizard still writes, and I'm not sure creating a public routine buys you very much unless you want a generic print routine into which you pass all the arguments it needs. But in that case, the public routine will probably be shorter than the routines that call it. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15> Here's a 2000 version of your Click event.


    Private Sub cmdPrintReceipt2_Click()
    On Error GoTo Err_cmdPrint_Receipt2_Click

    Dim strDocName As String
    Dim strWhere As String

    strDocName = "rptReceipt2"

    ' if the ReceiptNumber is a number rather than a numeric string, use this:
    ' strWhere = "[ReceiptNumber]=" & Me![ReceiptNumber]

    ' if Receipt number is a string rather than a true number, you need this instead:

    strWhere = "[ReceiptNumber]=" & Chr(34) & Me!ReceiptNumber & Chr(34)

    ' You don't actually need to do this since you aren't asking the report to evaluate the control any more
    ' DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    '
    ' but here's a 2000 equivalent
    ' DoCmd.RunCommand acCmdSaveRecord


    DoCmd.OpenReport strReportName, acPreview, , strWhere

    Exit_cmdPrint_Receipt2_Click:
    Exit Sub

    Err_cmdPrint_Receipt2_Click:
    MsgBox Err.Description
    Resume Exit_cmdPrint_Receipt2_Click

    End Sub
    Charlotte

  5. #5
    4 Star Lounger
    Join Date
    May 2002
    Location
    Roanoke, Virginia, USA
    Posts
    493
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Print Routines (A2K SP3)

    Currently, there are 2 command buttons on each of at least 3 forms whose On click event calls a macro to Print either a receipt or invoice. This is working fine and perhaps I should leave well enough alone.

    This forum seems to be adamant in using VBA instead of macros except under special circumstances.

    In trying to polish up my application, I was trying to determine if it's better to leave the 2 print macros in place or convert the print routine to VBA. I still want the two print options to be available on each form.

    Most all my command buttons in this application use macros which I thought I shoud be thinking of converting to VBA.

    Any guidance would be appreciated.

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

    Re: Print Routines (A2K SP3)

    Most of us find VBA code easier to read and to maintain than macros, but in itself, there is nothing against using macros. If you have macros that do what they are intended to do, that's fine - no reason to convert them unless you are planning a major overhaul.

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

    Re: Print Routines (A2K SP3)

    There isn't really much wrong with macros except their lack of error handling and their inflexibility. Besides, it takes as much time and effort to write a complicated macro as it does to write the equivalent code, and there are plenty of things that you simply can't do with macros. That makes it worthwhile to learn VBA but doesn't preclude using macros for simple stuff like printing. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    Charlotte

  8. #8
    4 Star Lounger
    Join Date
    May 2002
    Location
    Roanoke, Virginia, USA
    Posts
    493
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Print Routines (A2K SP3)

    Thanks to everyone who has responded. Based on both Hans and Charlotte's comments, I think the simple Print macros will take up less space and execute as fast of faster.

    Thanks

    Tom

Posting Permissions

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