Results 1 to 3 of 3
  1. #1
    thebionicredneck2003 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2013
    Posts
    22

    Help with Validation code on exit event on a form

    Hi guys,
    I need some help with this code snippet. I am trying to perform a validation on exit of a specific tab of my form and I am calling this function in the form exit event
    Code:
    Public Function fnValidateForm(Form_2 As Form) As Boolean
    Dim ctl As Control
    ‘Dim Msg, Style, Title, Response, MyString
    fnValidateForm = True
    For Each ctl In Form_2.Controls
    'value in the control is required
    If InStr(1, ctl.Tag, "Required") > 0 Then
    ' no value entered or value is null
    ' or zero for numeric fields
    If (IsNull(ctl.Value)) Or (Len(ctl.Value) = 0) Then
    ctl.SetFocus
    MsgBox "Please complete the highlighted control", vbCritical + vbOKOnly
    Cancel = True
    fnValidateForm = False
     
    Exit For
    End If
    'Next
    End If
    Next
    End Function
    The problem I have is that I would like the user to remain on the tab until they fix the error, but this code block allows the user to go to the next tab without completing the errors which defeats the entire purpose of the validation.

    I have been on this for quite some time, so I just need a fresh pair of eyes to tell what I need to change to get this working.

    Thanks

  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,929
    Maybe set focus to a control on the tab?
    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
    DepricatedZero's Avatar
    DepricatedZero is offline Cthulhu Fhtagn!
    Windows 8 Access 2007
    Join Date
    Apr 2013
    Location
    Cincinnati
    Posts
    65
    My first try would be to do as June mentioned. Set focus back to the control that failed validation, and that should keep them on the tab as well.

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

Similar Threads

  1. If New Record Stop Code Exit Sub
    By burrina in forum Forms
    Replies: 1
    Last Post: 12-19-2012, 01:02 PM
  2. OnClose event and exit/quit buttons
    By atran in forum Access
    Replies: 1
    Last Post: 07-24-2012, 04:14 PM
  3. Replies: 3
    Last Post: 07-20-2012, 11:41 AM
  4. How do I capture Access DB exit event
    By jscriptor09 in forum Access
    Replies: 3
    Last Post: 09-21-2011, 09:45 AM
  5. How to put code in form event.
    By rogdawg in forum Forms
    Replies: 4
    Last Post: 08-06-2010, 03:38 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