Results 1 to 7 of 7
  1. #1
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376

    Error Handling issue

    Hello all,



    I am trying to create error handlers to better instruct users and to enhance my skills and I have run into yet another problem. In the attached database if you open frmPartNumbers and go to the third record it shows "Part #" as 12345a, if you change that to 12345 it will create an error because the first record has that PN.

    My issue is that when I try to handle the error I get an error number of "0" when it should be error number "3022". I also want the user to just see the message I have put in there and not the default Access error message.

    What am I doing wroog to capture the actual error number and how do I stop the default Access Error Message?


    Error Handling.zip


    Thanks in Advance

    Dave

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,906
    I get error 3022 ?
    Plus some annoying messages that image file is not where it is supposed to be.

    You need the OnError statement before any code executes?

    Plus you are using the Form Error event, so choose one or the other.? I have never used the Form Error event, so not sure how it behaves.
    Last edited by Welshgasman; 10-11-2020 at 04:44 AM. Reason: Added OnError sentence

  3. #3
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376
    Welshgasman,

    The message about the Image File not being where it is supposed to be is because of the Image Path Field has a path where it expects to find an image file which you don't have on your system, so that error is working correctly.
    I changed it so the OnError statement is before any code executes I believe.

    Code:
    Private Sub MfrPartNumber_AfterUpdate()
    On Error GoTo ErrHandler
    If Me.Dirty Then    'to check if any data has changed.
            DoCmd.RunCommand acCmdSaveRecord
    End If
    
    
    Dim ErrorNumber As Integer
    
    
    
    
    Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    Msg = "The PN Entered is a Duplicate PN and is not allowed, Please enter a different PN"""
    Style = vbYesNo + vbCritical + vbDefaultButton2
    Title = "Dave's Database"
    'Help = "DEMO.HLP"
    'Ctxt = 1000
    Response = MsgBox(Msg, Style, Title, , Ctxt)
    If Response = vbYes Then    ' User chose Yes.
        MyString = "Yes"    ' Perform some action.
    Else    ' User chose No.
        MyString = "No"    ' Perform some action.
    End If
    
    
    
    
    Exit_ErrorHandler:
        Exit Sub
    
    
    ErrHandler:
    Dim intErrNum As Integer
    intErrNum = Err
    If intErrNum = 3022 Then
     
            DoCmd.SetWarnings False
            ErrorNumber = Err.Number
            'MsgBox "The PN Entered is a Duplicate PN and is not allowed, Please enter a different PN", , "Daves MiniMRP"
            MsgBox "The PN Entered is a Duplicate PN and is not allowed, Please enter a different PN" & _
            vbCrLf & ErrorNumber, , "Daves MiniMRP"
            Me.PartImage.Picture = ""
            Me.txtImagePath = ""
            Me.MfrPartNumber.SetFocus
    End If
    
    
    End Sub
    It still isn't working as I would like though, now it goes back to the PN field but if you hit "Tab" it just goes to the next field rather that forcing the user to change the PN. That is probably because of the Me.Dirty Statement correct?
    Last edited by Dave14867; 10-11-2020 at 06:14 AM. Reason: additional info

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,906
    TBH I do not know why it allows you to move to the description.
    I've found this link http://www.databasedev.co.uk/duplicates.html so see how they do it.

    Seems it should also be the Before_Update even t as well?
    https://stackoverflow.com/questions/...ving-a-textbox

    HTH

    Quote Originally Posted by Dave14867 View Post
    Welshgasman,

    The message about the Image File not being where it is supposed to be is because of the Image Path Field has a path where it expects to find an image file which you don't have on your system, so that error is working correctly.
    I changed it so the OnError statement is before any code executes I believe.

    Code:
    Private Sub MfrPartNumber_AfterUpdate()
    On Error GoTo ErrHandler
    If Me.Dirty Then    'to check if any data has changed.
            DoCmd.RunCommand acCmdSaveRecord
    End If
    
    
    Dim ErrorNumber As Integer
    
    
    
    
    Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    Msg = "The PN Entered is a Duplicate PN and is not allowed, Please enter a different PN"""
    Style = vbYesNo + vbCritical + vbDefaultButton2
    Title = "Dave's Database"
    'Help = "DEMO.HLP"
    'Ctxt = 1000
    Response = MsgBox(Msg, Style, Title, , Ctxt)
    If Response = vbYes Then    ' User chose Yes.
        MyString = "Yes"    ' Perform some action.
    Else    ' User chose No.
        MyString = "No"    ' Perform some action.
    End If
    
    
    
    
    Exit_ErrorHandler:
        Exit Sub
    
    
    ErrHandler:
    Dim intErrNum As Integer
    intErrNum = Err
    If intErrNum = 3022 Then
     
            DoCmd.SetWarnings False
            ErrorNumber = Err.Number
            'MsgBox "The PN Entered is a Duplicate PN and is not allowed, Please enter a different PN", , "Daves MiniMRP"
            MsgBox "The PN Entered is a Duplicate PN and is not allowed, Please enter a different PN" & _
            vbCrLf & ErrorNumber, , "Daves MiniMRP"
            Me.PartImage.Picture = ""
            Me.txtImagePath = ""
            Me.MfrPartNumber.SetFocus
    End If
    
    
    End Sub
    It still isn't working as I would like though, now it goes back to the PN field but if you hit "Tab" it just goes to the next field rather that forcing the user to change the PN. That is probably because of the Me.Dirty Statement correct?

  5. #5
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    I've handled this problem before using this method.
    In the after update event of the textbox holding the PN, use DLookup (or DCount) to see if the PN already exists. If so, display your error message and UnDo the PN.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,778
    In the after update event of the textbox holding the PN, use DLookup (or DCount) to see if the PN already exists. If so, display your error message and UnDo the PN.
    Did you mean BeforeUpdate? AFAIK, AfterUpdate has no Cancel, thus no Undo either. Usually I validate data before attempting to write it, and often it is enough to simply trap the Access error number and present your own message.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    As micron said--- use the BeforeUpdate event to do validation. That's the last event before record is saved.

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

Similar Threads

  1. #Error in calculated control despite error handling
    By TrainingExcellence in forum Access
    Replies: 2
    Last Post: 08-07-2019, 07:45 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. Replies: 3
    Last Post: 09-05-2012, 10:23 AM
  5. Error 2501 displays with Error handling
    By agent- in forum Programming
    Replies: 13
    Last Post: 08-05-2011, 02:20 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