Results 1 to 8 of 8
  1. #1
    JennyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2017
    Posts
    51

    Error Handling

    Hi,



    Does anyone see any error with my Error Handling code below?

    Private Sub Form_Load()
    On Error GoTo Err_Form_Load

    Dim Security As Integer
    Me.txtuser = Environ("Username")
    If IsNull(DLookup("userSecurity", "QryUser", "[userLogin] = '" & Me.txtuser & "'")) Then
    MsgBox "No User Security is set up for this user. Please contact the Admin", vbOKOnly, "Login Info"

    'Disable Adminpage button if the user does not have the security level set up
    Me.NavbuttonAdm.Enabled = False
    Me.NavbuttonCM.Enabled = False
    Me.navbuttonCS.Enabled = False
    Me.NavbuttonRpt.Enabled = False

    Else
    'Assign a security level number to variable Security

    Security = DLookup("Usersecurity", "QryUser", "[UserLogin] = '" & Me.txtuser & "'")
    Select Case Security
    Case Is = 1
    Me.NavbuttonAdm.Enabled = True
    Case Is = 2
    Me.NavbuttonAdm.Enabled = False
    Case Is = 3
    Me.NavbuttonCM.Enabled = False
    Me.NavbuttonAdm.Enabled = False
    End Select

    End If

    Exit_Form_Load:
    Exit Sub

    Err_Form_Load:
    MsgBox "Error: (" & Err.Number & ") " & Err.Description, vbCritical
    Resume Exit_Form_Load
    End Sub



  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Look fine to me. Are you having problems with it?

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    Does anyone see any error with my Error Handling code below?
    Not that I can see. Why do you ask
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    JennyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jan 2017
    Posts
    51
    I was testing out this error handling code to make sure it gives me an error message instead of VBA screen. When I opened up the access, it brought me right to VBA page, which means somehow the error handling is not working.

  5. #5
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    you might have a 'ghost' break, assuming there are no breaks in the code that have been left behind (you'd see the colored dot to indicate this).
    Make one small change in the code so that the compile button is enabled. Remove the change you just made, compile and save and try again.
    Also, before this point you have not turned off warnings have you? That, in combination to your options on how to handle errors may result in an error not being announced but breaking execution on the error anyway. My suggestion is based on the fact that you did not say any code was highlighted in break mode.

    Also, I would assign the value of a DLookup to a variable if it's used more than once because
    1) any changes to it's structure must be repeated for every instance of its use
    2) each time you use it, you cause Access to evaluate the expression, which in some cases results in unnecessary network traffic.
    Last edited by Micron; 01-20-2017 at 04:06 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I could be that error handling has been disabled. While you are in a VBA code module, From the menu select Tools - Options, and click the General tab. For error handling to work, the Error Trapping must be set to "Break on Unhandled Errors".

    When you see the VBA screen on opening access, is VBA opening code shown above? And if it is, what line of code is highlighted? (there should be one).

    Are you seeing an error message just before you see the code?

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,388
    Hold down shift while starting Access. This will prevent the form from loading. Do you get the error? If so, it's not from the Form_Load event.
    Now load the form. Do you get the error?

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    ...I was testing out this error handling code to make sure it gives me an error message...
    In order to test the error handling there has to be an error for it to trap. At first glance, I don't see anything wrong with the code, so which line of code were you expecting would cause a runtime error that would invoke the error handling?

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

Similar Threads

  1. Error Handling: How to Skip an expected Error
    By Sa'El in forum Programming
    Replies: 2
    Last Post: 01-17-2016, 05:04 PM
  2. Replies: 5
    Last Post: 09-06-2015, 12:06 PM
  3. Error handling of table update error?
    By panoss in forum Forms
    Replies: 5
    Last Post: 10-31-2014, 02:06 PM
  4. Error Handling Question - On Error Goto
    By redbull in forum Programming
    Replies: 7
    Last Post: 12-06-2012, 07:54 AM
  5. Replies: 3
    Last Post: 09-05-2012, 10:23 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