Results 1 to 8 of 8
  1. #1
    AlisonP is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Northern CA
    Posts
    13

    Control Undo in Exit vs. BeforeUpdate events

    I am a newbie here, so this may be a simple question. I am doing multiple data error checks on a data entry form (in a function). If the user enters something invalid, the function writes out an appropriate error message, and I would like to "erase" the bad data and keep the cursor in the control so the user can retry. There are two cases here: (a) initial data entry, nothing previously in the field, and (b) data entry already done, and the user goes back and changes the field. If I use the Exit event as in the code below, then focus stays in the control but the Undo does not work for case (b). If I use the same code in the BeforeUpdate event, then the Undo works for case (b). To get what I want (undo in both cases) do I have to use both the BeforeUpdate event AND the Exit event? Seems like there should be a better way. My main question is why doesn't Undo work in the Exit event coded below? Thanks for any help.


    Private Sub TxtLtot_Exit(Cancel As Integer)

    Dim CheckOto As Integer
    CheckOto = Me.cmbPreySpecies.Column(2)
    ' Check for errors in data entry
    If fnCheckTots(CheckOto, 0, Me.TxtLtot) = "No" Then


    Me.TxtLtot.Undo
    Cancel = True
    End If

    End Sub

  2. #2
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254

    Alison -

    Not sure this will help but, I have usually seen the Cancel = True before the undo. As an experiment, you may want to:

    try:
    Cancel = True
    Me.TxtLtot.Undo

    instead of:
    Me.TxtLtot.Undo
    Cancel = True

    In the exit code you posted and see what happens.

    All the best,

    Jim

  3. #3
    AlisonP is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Northern CA
    Posts
    13
    Thanks, Jim, for the suggestion. Unfortunately, making that change didn't work. Same problem as before. Any other suggestions?

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Is your fnCheckTots function returning a string or a boolean value? If boolean then try True/False instead of "No".

  5. #5
    AlisonP is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Northern CA
    Posts
    13
    Thank you for thinking about my little problem. The function returns a string, not a Boolean. Any other suggestions?

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    What does *not* work correctly when you use the BeforeUpdate event of the control?

  7. #7
    AlisonP is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Northern CA
    Posts
    13
    If it's a new record, so that there is no data in the field, the user is supposed to enter a valid number in the field. But if I use BeforeUpdate, and the user tabs past the field, then no error is caught. If I use Exit in that same situation, then the error is caught.

    Hmm, maybe I should be doing something different in the function? Here is one of the functions:


    Function fnCheckBeaks(CheckOto As Integer, varValue)

    ' Checks that a number >=0 has been entered for Ltot, Rtot, and Utot for fish species
    ' and those fields are blank for non-fish species.
    ' Also checks TotNoPrey field, that it is a positive number

    fnCheckBeaks = "No" 'assume bad value entered until all checks are passed

    'Check for positive number entered for TxtNoLower/TxtNoUpper where prey species is a cephalopod
    If CheckOto = 0 Then

    'Check to be sure a number is entered (already checked for text in FormError event)
    If IsNull(varValue) Then
    MsgBox "This is a cephalopod -- you must enter the number of otoliths (enter zero for none)", vbExclamation, "Try again"
    Exit Function
    End If

    'Check to be sure the number entered is not negative
    If varValue < 0 Then
    MsgBox "Error: You must enter a vaid number of beaks (enter zero for none)", vbExclamation, "Try Again"
    Exit Function
    End If

    End If

    ' If the species is not a fish, # beaks fields must be blank
    If CheckOto = -1 Then

    If Not IsNull(varValue) Then
    MsgBox "This species is not a cephalopod -- you must enter a blank in this field", vbExclamation, "Try Again"
    Exit Function
    End If

    End If

    'All validation checks passed
    fnCheckBeaks = "OK"

    End Function

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I appreciate that you are attempting to localize the validation into one function but you may need to simply use the events differently.

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

Similar Threads

  1. How do I undo the requirement to login every mdb
    By garymkrieg in forum Security
    Replies: 3
    Last Post: 07-11-2011, 03:53 PM
  2. Validation BeforeUpdate Errors
    By huv123 in forum Programming
    Replies: 3
    Last Post: 02-07-2011, 10:25 AM
  3. Undo Table Design Changes
    By MelindaP in forum Database Design
    Replies: 6
    Last Post: 07-27-2010, 09:07 AM
  4. Replies: 0
    Last Post: 12-28-2009, 12:14 PM
  5. BeforeUpdate and AfterUpdate Question
    By gsurfdude in forum Programming
    Replies: 0
    Last Post: 04-03-2008, 07:56 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