Results 1 to 12 of 12
  1. #1
    brownk is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    May 2012
    Location
    Evansville, IN
    Posts
    56

    Error Handling on Form

    This is a pretty basic question i just dont know where to begin looking. I have a form with buttons which open reports. I have each report set up to 'Between [startDate] And [endDate]' so when opening the report the user is prompted to enter dates. If you choose cancel I get a Run-time error 2501: The OpenReport action was canceled. How can i go about leaving this error out of the picture? Or even better, if i user decides to cancel it just pulls ALL data, not just from a specified date.


    Thanks in advance

    **EDIT- I dont know if there are other ways of doing this, but i would like to do this in VBA******
    Last edited by brownk; 09-06-2012 at 09:41 AM. Reason: more info

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Here are the basics of error handling:

    http://www.baldyweb.com/ErrorTrap.htm

    Most of us use forms to gather user input. One advantage is that you can validate that they've entered valid dates before continuing. That would also enable you to react accordingly if they leave the input empty. You also might be able to use this type of thing:

    Between Nz([startDate], #1/1/1900#) And Nz([endDate], #12/31/2039#)

    though that is completely untested. I tend to use code-based solutions.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Here's some old code I have archived for trapping Error 2501 and suppressing the error message. Sadly, I do not remember who/where I got it from:

    Code:
    Sub PrintInvoice_Click()
    ' This code created by Command Button Wizard.
    On Error GoTo Err_PrintInvoice_Click
    
        Dim strDocName As String
        
        strDocName = "Invoice"
        ' Print Invoice report, using Invoices Filter query to print
        ' invoice for current order.
        DoCmd.OpenReport strDocName, acViewNormal, "Invoices Filter"
    
    Exit_PrintInvoice_Click:
        Exit Sub
    
    Err_PrintInvoice_Click:
        ' If action was cancelled by the user, don't display an error message.
        Const conErrDoCmdCancelled = 2501
        If (Err = conErrDoCmdCancelled) Then
            Resume Exit_PrintInvoice_Click
        Else
            MsgBox Err.Description
            Resume Exit_PrintInvoice_Click
        End If
    
    End Sub


    You could, I suppose, also insert your own error message, in the If... section, if you want to.

    Linq ;0)>

  4. #4
    brownk is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    May 2012
    Location
    Evansville, IN
    Posts
    56
    Thanks for the link pbaldy, it will help out a lot. Thanks for the code missinglinq, i will try this out and post my results.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    No problemo.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    brownk is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    May 2012
    Location
    Evansville, IN
    Posts
    56
    Alrighty, i messed around with it a bit and i still cannot get it to work. Sorry I'm a real noob with VBA, im sort of teaching myself as i go along. Heres the code before i attempted any error handling.

    Option Compare Database
    Option Explicit

    Private Sub AppRegister_Click()
    DoCmd.OpenReport "Commercial Application Register", acViewPreview
    End Sub

    Private Sub LoanRegister_Click()
    DoCmd.OpenReport "Commercial Loan Register", acViewPreview
    End Sub

    Private Sub PipeReportAp_Click()
    DoCmd.OpenReport "Commercial Pipeline Report - Approved", acViewPreview
    End Sub

    Private Sub PipeReportPen_Click()
    DoCmd.OpenReport "Commercial Pipeline Report - Pending", acViewPreview
    End Sub

    Private Sub OutLoanCom_Click()
    DoCmd.OpenReport "Outstanding Commercial Loan Commitments", acViewPreview
    End Sub

    Private Sub Done_Click()
    On Error GoTo Err_Done_Click


    DoCmd.Quit

    Exit_Done_Click:
    Exit Sub

    Err_Done_Click:
    MsgBox Err.Description
    Resume Exit_Done_Click

    End Sub

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Why don't you post one of the procedures with your attempt at error handling and we'll fix it. You realize each sub will have separate error handling?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    brownk is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    May 2012
    Location
    Evansville, IN
    Posts
    56
    Here is my attempt. I get an error Label not defined.

    Option Compare Database
    Option Explicit

    Private Sub AppRegister_Click() 'debugger stops here

    On Error GoTo err_trap

    DoCmd.OpenReport "Commercial Application Register", acViewPreview
    End Sub
    Private Sub LoanRegister_Click()

    On Error GoTo err_trap

    DoCmd.OpenReport "Commercial Loan Register", acViewPreview
    End Sub

    Private Sub PipeReportAp_Click()

    On Error GoTo err_trap

    DoCmd.OpenReport "Commercial Pipeline Report - Approved", acViewPreview
    End Sub

    Private Sub PipeReportPen_Click()

    On Error GoTo err_trap

    DoCmd.OpenReport "Commercial Pipeline Report - Pending", acViewPreview
    End Sub

    Private Sub OutLoanCom_Click()

    On Error GoTo err_trap

    DoCmd.OpenReport "Outstanding Commercial Loan Commitments", acViewPreview
    End Sub

    err_trap:
    If Err.Number = 2501 Then Exit Sub

    End Sub

  9. #9
    brownk is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    May 2012
    Location
    Evansville, IN
    Posts
    56
    DUH. I got it guys. lol.... noob moment. Thanks for your help! For reference the code needed to look like this :


    Option Compare Database
    Option Explicit

    Private Sub AppRegister_Click()

    On Error GoTo err_trap

    DoCmd.OpenReport "Commercial Application Register", acViewPreview

    err_trap:
    If Err.Number = 2501 Then Exit Sub

    End Sub
    Private Sub LoanRegister_Click()

    On Error GoTo err_trap

    DoCmd.OpenReport "Commercial Loan Register", acViewPreview

    err_trap:
    If Err.Number = 2501 Then Exit Sub

    End Sub

    Private Sub PipeReportAp_Click()

    On Error GoTo err_trap

    err_trap:
    If Err.Number = 2501 Then Exit Sub

    DoCmd.OpenReport "Commercial Pipeline Report - Approved", acViewPreview
    End Sub

    Private Sub PipeReportPen_Click()

    On Error GoTo err_trap

    err_trap:
    If Err.Number = 2501 Then Exit Sub

    DoCmd.OpenReport "Commercial Pipeline Report - Pending", acViewPreview
    End Sub

    Private Sub OutLoanCom_Click()

    On Error GoTo err_trap

    DoCmd.OpenReport "Outstanding Commercial Loan Commitments", acViewPreview
    End Sub

    err_trap:
    If Err.Number = 2501 Then Exit Sub

    End Sub

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I wouldn't call it proper error handling, and some you have out of order, but it'll probably work. I assume some cut/paste errors too, as the bit at the end is out of place.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    brownk is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    May 2012
    Location
    Evansville, IN
    Posts
    56
    What isn't proper about it?

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Proper is what Linq and I posted, which includes an exit handler. Yours runs through the error handler whether there's been an error or not.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 09-05-2012, 10:23 AM
  2. On Error handling
    By rivereridanus in forum Access
    Replies: 2
    Last Post: 06-18-2012, 07:46 AM
  3. Error 2501 displays with Error handling
    By agent- in forum Programming
    Replies: 13
    Last Post: 08-05-2011, 02:20 PM
  4. Error Handling
    By jgelpi16 in forum Programming
    Replies: 4
    Last Post: 09-14-2010, 12:17 PM
  5. Error Handling
    By trb5016 in forum Access
    Replies: 2
    Last Post: 08-10-2010, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums