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

    Problem cropped up out of the blue

    Hello all,



    I have a form that has been working great for over 2 years. Now all of a sudden it has an issue. The intent is that the form will not allow the user to enter a Lot Number that has already been used, and will set the focus back to the "DHRNumber" field and allow the user to make the correction and move on to the next field.

    The problem is that focus is set back to the "DHRNumber" field but then you cannot move on to the next field. The only way to get around it is to close the form and reopen it, then you can proceed as normal.

    Like I said, this just all of a sudden started happening, what am I missing?

    The code is shown below for the On Lost Focus event.

    Thanks in advance.



    Code:
    Private Sub DHRNumber_LostFocus()
    
    On Error GoTo DuplicateLotNumber
    
    
    DoCmd.RunCommand acCmdSaveRecord
    
    
    Dim Num As Integer
    Num = Err.Number
    
    
    DuplicateLotNumber:
        If Err.Number = 3022 Then
    
    
            MsgBox "You have entered a Lot Number which already exists, Please verify you have the correct Lot Number", , "Duplicate Lot Number:"
        End If
    
    
        Me.Rev.SetFocus
        Me.DHRNumber.SetFocus
    End Sub

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    You would normally check values in the before update event, which would allow you to undo the entry.
    You are using an error to trap that in the lost focus event, which is not a recommended way of validating things.

    You can check in the before update event using (assuming your lot number is text) using something like

    Code:
    If Dcount("DHRNumber","YourtableName","[DHRNumber] = '" &  Me.DHRNumber & "'") > 0 Then 
        Me.Undo
        Cancel = True
    End IF
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376
    Thanks Minty that seemed to do the trick.

    I just wonder why the method I used worked for so long.

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    That is a very good question.
    I can think if a few suspects ( I mostly avoid on focus / lost focus events as they can have some interesting side effects), but tbh, I think it's better to just think you previously "got away with it"
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Was it ever triggered before? Maybe no one ever entered a duplicate number.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    I am missing the plot here?

    The last thing that code does is set focus to DHRNumber, regardless of any error or not?
    How on earth did it ever work?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Quote Originally Posted by Welshgasman View Post
    I am missing the plot here?

    The last thing that code does is set focus to DHRNumber, regardless of any error or not?
    How on earth did it ever work?
    Well spotted - I suspect an Exit Sub has been deleted somewhere.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Quote Originally Posted by Minty View Post
    Well spotted - I suspect an Exit Sub has been deleted somewhere.
    Or the End If got moved
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Blue to Red Puzzle
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 12-26-2019, 04:51 PM
  2. Blue Border
    By Kaloyanides in forum Forms
    Replies: 1
    Last Post: 09-21-2016, 11:24 AM
  3. Blue circle of death
    By Gina Maylone in forum Access
    Replies: 1
    Last Post: 08-28-2015, 10:29 AM
  4. Check Boxes turning blue
    By jhatcheqb in forum Database Design
    Replies: 3
    Last Post: 09-08-2014, 01:11 PM
  5. error 2950 out of the blue
    By kroenc17 in forum Access
    Replies: 1
    Last Post: 10-08-2010, 10:00 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