Results 1 to 4 of 4

Thread: Error Trapping

  1. #1
    Join Date
    Feb 2005
    Posts
    46

    Unanswered: Error Trapping

    Hi

    Not the best at access but im doing my best

    I have created a form which opens up in a datasheet mode, on the first field I enter an IP address, I have created the following code:

    Private Sub ip_AfterUpdate()

    Me.device.SetFocus
    DoCmd.GoToRecord , , acNewRec
    DoCmd.GoToRecord , , acPrevious
    Me.siteCode.Value = "2"
    Me.Combo14.Value = "10"
    Me.device.SetFocus

    End Sub

    Currently with the above code if the record exists it crashes telling me it wont create duplicate records, which is good cause I dont want to record two IP address.

    What I basically want to happen, is for access to check if the IP Address allready exists and if it does then then to stop and tell me that the record exists.

  2. #2
    Join Date
    Jan 2004
    Location
    Jersey, UK
    Posts
    108

    How to...

    Hi, I'm sorry I don't have more time right now to provide you with a complete solution, but here's the logic at least.

    Each error returns an error code and an error description. You should be able to determine what error code is returned when the system encounters a duplicate record. You do this by typing code similar to this:

    your sub name

    dim strErrorCode as string

    strErrorCode = err.number & " " & err.description

    'both these lines show you the result of the string in the variable
    msgbox strErrorCode
    debug.print strErrorCode

    end your sub name

    After you've determined the error number it's just a case of setting up an error trap like this:

    your sub name
    on error goto errTrap

    'your code here

    errTrap:
    select case err.number
    case 0 'this means there was no error
    'do nothing
    case 1234 'example only: let's say this is your error number
    exit sub ' or something else appropriate
    end select
    end your sub name


    Regards - Andy

  3. #3
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Another way would be to use DCount or DLookup to see if the entered value
    already resides in the table and if so, throw up a message box accordingly.

    Code:
    If DCount(expr, domain[, criteria]) > 0 then  'IP Already exists
        'Code for message box here
    End If

    BTW, I think 3022 is the error code for duplicates in indexed fields.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    Join Date
    Feb 2005
    Posts
    46
    Awsome andy that was spot on



    This is the final code

    Private Sub ip_AfterUpdate()
    On Error GoTo errTrap
    Me.device.SetFocus
    DoCmd.GoToRecord , , acNewRec
    DoCmd.GoToRecord , , acPrevious
    Me.siteCode.Value = "2"
    Me.Combo14.Value = "10"
    Me.device.SetFocus
    errTrap:
    Select Case Err.Number
    Case 0
    'do nothing
    Case 2105
    MsgBox ("This IP allready exists")
    End Select
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •