Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    kf1894 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    12

    Replace VB Run-time Error Message with Custom message

    Thanks for helping an Access newbie - I have a Command button which loads a subform. The code breaks on the 1st line (in bold below) because of a duplicate value in a indexed field. I want this to happen. What I need to know is --- A. Can I replace the VB runtime error message with a custom message? If yes, How do I accomplish this? B. When they clear the error message it allows them to select another name from the combo box and continues to load the subform.

    Private Sub cmdEnterResults_Click()
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    DoCmd.SetWarnings False
    DoCmd.OpenQuery "qappNewResponses"


    Me![sfrmResponses].Requery
    DoCmd.SetWarnings True

    End Sub

    Any and all help would be greatly appreciated
    Fez

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,820
    Try error handler code. http://allenbrowne.com/ser-23a.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    kf1894 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    12
    Thanks for the link - Unfortunately, the information is beyond my skill set. Is it possible to modify my code with the information?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,820
    What kind of query are you opening? Do you want the query to run regardless of error?

    Maybe like:

    Private Sub cmdEnterResults_Click()
    On Error GoTo ErrSome_Name
    DoCmd.RunCommand acCmdSaveRecord
    CurrentDb.Execute "qappNewResponses"
    Me![sfrmResponses].Requery
    Exit_SomeName:
    Exit Sub
    Err_SomeName:
    If Err.Number = x Then 'put the error number generated by your error in place of x
    Resume Next
    Else
    MsgBox "Error : " & Err.Number & " : " & Err.Description & " : Contact database administrator"
    Resume Exit_SomeName
    End If
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    kf1894 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    12
    Thanks for responding. The error is tied to a duplicate record based on the selection they made in a combobox. I need that to happen. I want to show a custom error message instead of the VB 3022, that cancels the code break and then allow them to go back and select a diiferent name from the combo that will not cause the error.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,820
    Want to actually answer the questions in my previous post? Did you try the suggested code?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    kf1894 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    12
    My apologies. The query is an append query & I do not want it to run if there is a duplicate in the table. I want to clear the break and allow them to go back and make another selection. so I did not try your code because it saved the record.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,820
    Then exit the Sub for any error:

    Err_SomeName:
    If Err.Number <> x Then 'put the error number generated by your error in place of x
    MsgBox "Error : " & Err.Number & " : " & Err.Description & " : Contact database administrator"
    End If
    Resume Exit_SomeName


    Correct my typo in the On Error GoTo:

    On Error GoTo Err_SomeName
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    kf1894 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    12
    Thanks - I put in the following code & now get a error 3061: too Few parameters. Expected 3. AND the record saves creating a duplicate. The error also apprears when I select a name that is not a duplicate.

    I appreciate your patience and support. As a FYI the same error happens if I keep in the DoCmd.RunCommand acCmdSaveRecord line.


    Private Sub cmdEnterResults_Click()
    On Error GoTo Err_cmdEnterResults_Click
    CurrentDb.Execute "qappNewResponses"
    Me![sfrmResponses].Requery
    Exit_cmdEnterResults_Click:
    Exit Sub
    Err_cmdEnterResults_Click:
    If Err.Number = 3022 Then 'put the error number generated by your error in place of x
    Resume Next
    Else
    MsgBox "Error : " & Err.Number & " : " & Err.Description & " : You have already completed a questionnaire for this Manager. Select another name or Exit this form and Select MPP Dimensions - Edit from the main menu"
    Resume Exit_cmdEnterResults_Click
    End If
    End Sub

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,820
    That's not the latest code I suggested.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    kf1894 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    12
    OK - Now I am really confused. I see 2 sets of code;

    The Orginal: fixing the On Error go to typo
    Private Sub cmdEnterResults_Click()
    On Error GoTo Err_SomeName
    DoCmd.RunCommand acCmdSaveRecord
    CurrentDb.Execute "qappNewResponses"
    Me![sfrmResponses].Requery
    Exit_SomeName:
    Exit Sub
    Err_SomeName:
    If Err.Number = x Then 'put the error number generated by your error in place of x
    Resume Next
    Else
    MsgBox "Error : " & Err.Number & " : " & Err.Description & " : Contact database administrator"
    Resume Exit_SomeName
    End If
    End Sub


    And the second one:
    Err_SomeName:
    If Err.Number <> x Then 'put the error number generated by your error in place of x
    MsgBox "Error : " & Err.Number & " : " & Err.Description & " : Contact database administrator"
    End If
    Resume Exit_SomeName

    My assumption was to incorporate the 2nd code into the 1st, changing the somename to by own verbiage. I tried that code with the line "DoCmd.RunCommand acCmdSaveRecord" and without (which is what is posted)
    Was that wrong? What should it be? Just the 2nd code?






    Correct my typo in the On Error GoTo:

    On Error GoTo Err_SomeName

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,820
    Post 8 shows only one structure for the error branch. It is to replace the first suggested code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    kf1894 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    12
    If you meant to replace the 1st suggested code by adding the error capture to my original code. It still does not work. It hangs up on Resume Exit_SomeName with the following error: Compile error: Label not defined

    This is what I did: Obviously it is incorrect. Can you re-wite this to work?

    Private Sub cmdEnterResults_Click()
    On Error GoTo Err_cmdEnterResults
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    DoCmd.SetWarnings False
    DoCmd.OpenQuery "qappNewResponses"
    Me![sfrmResponses].Requery
    DoCmd.SetWarnings True
    Err_cmdEnterResults:
    If Err.Number <> 3022 Then 'put the error number generated by your error in place of x
    MsgBox "Error : " & Err.Number & " : " & Err.Description & " : Contact database administrator"
    End If
    Resume Exit_cmdEnterResults
    End Sub

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,820
    My code was to replace the error branch, not the exit branch:

    Private Sub cmdEnterResults_Click()
    On Error GoTo Err_cmdEnterResults

    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    DoCmd.SetWarnings False
    DoCmd.OpenQuery "qappNewResponses"
    Me![sfrmResponses].Requery
    DoCmd.SetWarnings True

    Exit_cmdEnterResults_Click:
    Exit Sub

    Err_cmdEnterResults:
    If Err.Number <> 3022 Then 'put the error number generated by your error in place of x
    MsgBox "Error : " & Err.Number & " : " & Err.Description & " : Contact database administrator"
    End If
    Resume Exit_cmdEnterResults

    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    kf1894 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    12
    I really do appreciate your help & patience in all this & I'm probably driving you nuts!
    I entered the new code but am I'm still getting Compile Error: Label not defined at
    Resume Exit_cmdEnterResults

    However - I changed it to Resume Exit_cmdEnterResults_Click and it WORKED - Thanks again for your support and knowledge sharing.


Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. custom error message
    By msasan1367 in forum Access
    Replies: 1
    Last Post: 04-27-2013, 09:14 AM
  2. Replies: 14
    Last Post: 06-06-2012, 12:50 PM
  3. Replies: 1
    Last Post: 03-25-2012, 01:53 PM
  4. Custom validation error message
    By snorkyller in forum Access
    Replies: 2
    Last Post: 03-21-2011, 03:40 PM
  5. Custom error message problem
    By thekruser in forum Programming
    Replies: 10
    Last Post: 10-06-2010, 05:14 PM

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