Results 1 to 7 of 7
  1. #1
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727

    error handling

    Hi all,


    Taken over a acces 2003 database. Trying to error trap code for updating user info. Without error trap works. When I try to add error trap it errors saying "sub or function not defined". Please help


    Code:
    Private Sub Combo7_AfterUpdate()
    'On Error GoTo Err_Combo7_AfterUpdate    ' Find the record that matches the control.
        Dim rs As Object
        Set rs = Me.Recordset.Clone
        rs.FindFirst "[NewPT] = '" & Me![Combo7] & "'"
        If Not rs.EOF Then Me.Bookmark = rs.Bookmark
    'Exit_Combo7_AfterUpdate
    '    Exit Sub
    'Err_Combo7_AfterUpdate:
    '    MsgBox Err.Description
    '    Resume Exit_Combo7_AfterUpdate
        
    End Sub

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Try:
    Code:
    Private Sub Combo7_AfterUpdate()
     
       On Error GoTo Err_Combo7_AfterUpdate 
    
    ' Find the record that matches the control. 
        Dim rs As Object 
        Set rs = Me.Recordset.Clone 
        rs.FindFirst "[NewPT] = '" & Me![Combo7] & "'" 
        If Not rs.EOF Then Me.Bookmark = rs.Bookmark
    
    'Exit Combo7_AfterUpdate
        Exit Sub
    
    Err_Combo7_AfterUpdate:
        MsgBox Err.Description
    
    End Sub

  3. #3
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Ok Great! but why did it not like the "exit_Combo.. and Resume.." I always use this.
    thanks for your help.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    You missed the colon after it.


    Exit_Combo7_AfterUpdate:
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    A few things:

    The Message Box is the last step before the end of the sub, so why go back up to a statement that tells it to exit, when it will just exit naturally with the last line of code. It is unnecessary.

    But, as for the problems, first for "Exit_Combo7_AfterUpdate" to be a valid point you can jump to, it cannot be commented out and must have a colon at the end, i.e.
    Code:
    Exit_Combo7_AfterUpdate:
    Second, you don't use "Resume" to go to another section of code, you use "Resume Next" to pick up where you left off (I don't think you can tell it to go to some other part of your code like that).

    You might want to read the section on "Resume" here: http://www.cpearson.com/excel/ErrorHandling.htm. It is written from an Excel perspective, but VBA error handlers work similarly for Excel and Access.

  6. #6
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Great! thanks to you all.

  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,641
    Quote Originally Posted by JoeM View Post
    The Message Box is the last step before the end of the sub, so why go back up to a statement that tells it to exit, when it will just exit naturally with the last line of code.

    Second, you don't use "Resume" to go to another section of code, you use "Resume Next" to pick up where you left off (I don't think you can tell it to go to some other part of your code like that).
    The use of Resume to go the exit handler was fine (and was used the same way in the referenced link).

    While perhaps not actually necessary here, the line to resume at the exit handler is commonly included since the exit handler often has "clean-up" code in it. Some would argue that it is necessary here, as the rs variable would typically be set to Nothing in the exit handler (the rule of thumb being to clean any variable set with "Set").
    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. Error Handling Question - On Error Goto
    By redbull in forum Programming
    Replies: 7
    Last Post: 12-06-2012, 07:54 AM
  2. Replies: 3
    Last Post: 09-05-2012, 10:23 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 mws5872 in forum Access
    Replies: 4
    Last Post: 05-12-2010, 07:06 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