Results 1 to 14 of 14
  1. #1
    Rhubie is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    85

    Check for required when leave the field


    How can I check for a field being required when I leave the field rather than when I save the record?

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    With code in the On Exit event.
    Code:
      If IsNull(Me.ActiveControl) Then
        Cancel = True
      End If
    This would keep the focus on the control until the user made an entry. But IMHO it would be better to do any validation checks in the forms Before Update event.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Missinglinq's Avatar
    Missinglinq is online now VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    And if the user simply ignores the Control, i.e. never tabs into it? As Bob said, you really have to do this kind of validation in the Form_BeforeUpdate event, in order to insure that the Control is populated!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    Rhubie is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    85
    Thank you - if I do the validation in the Before Update event - then it doesn't check until I save the record - is that correct?

    Would I use the same code in the Before Update Event?

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    The code would be something like:
    Code:
    If IsNull(Me.NameOfControlBeingChecked) Then
        Cancel = True
      End If
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    Rhubie is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    85
    that would be in the before update event - would that check it before saving - wouldn't that work the same as having the required fields in the table?

  7. #7
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    I think the simple answer to that is yes, yes and yes.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  8. #8
    Missinglinq's Avatar
    Missinglinq is online now VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Yes it would, except that using Required at the Table-level will only catch cases where the Field is Null, I believe, while something like

    Code:
    If Nz(Me.NameOfControlBeingChecked,"") = "" Then
       Cancel = True
       NameOfControlBeingChecked.SetFocus
    End If


    will catch Nulls and Zero-Length Strings, cancel the update, then move back to the offending Control so that it can be populated.

    If you're doing more than one Validation, you need to add a line, to each of the Validations, so that it evaluates one Control, returns to the offending one, and gives the user time to correct it before evaluating the next one.

    Code:
    If Nz(Me.NameOfControlBeingChecked,"") = "" Then
       Cancel = True
       NameOfControlBeingChecked.SetFocus
       Exit Sub
    End If


    Most experienced developers prefer to do this kind of thing thru code, at the Form-level, rather than depending on doing things at Table-level, because they feel it gives them better control over things, especially things like the warning Messageboxes.

    Setting the Field named DME_Prov as Required, and leaving it blank, when going to save the Form, will pop a message that

    'the Field named DME_Prov is Required and as a result the Record cannot be saved'

    Chances are that the users will have no idea which Control holds DME_Prov. Even if labeled, the Control that holds the DME_Prov Field will probably have a Caption that says something like 'Durable Medical Equipment Provider;' something that makes sense to the user. The Access generated message will mean nothing to the end user, citing the DME_Prov Field as the problem, but a custom Messagebox can give the user a name that they will recognize and know where to go to correct their oversight.

    And as stated before, in order to insure that a Control is not left empty, the warning has to come just before the Record is Saved.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  9. #9
    Rhubie is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    85
    I am not an experience developer as you can see, but I am learning - how would I do this at the form level?

  10. #10
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Missinglinq has given you the required code (below). This would go in the Form's Before Update event. Change the red type to the name of the control that you want to check. The code can be repeated if you need to check other text boxes.
    Code:
    If Nz(Me.NameOfControlBeingChecked,"") = "" Then
       Cancel = True
       NameOfControlBeingChecked.SetFocus
       Exit Sub
    End If
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  11. #11
    Rhubie is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    85
    Thank you - if I want to add a message text to the field - how would I do that - what does Me. - would Me. be the table name?
    I have 3 fields - so I would put the code 3 times?

  12. #12
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Code:
    If Nz(Me.NameOfControlBeingChecked1,"") = "" Then
       Cancel = True
       Msgbox "Please enter data",vbOKOnly,"Data Required"
       NameOfControlBeingChecked1.SetFocus
       Exit Sub
    End If
    I have 3 fields - so I would put the code 3 times?
    Yes

    Me. Is used to refer to the current form, the one in which the code is written. You would not be able to use Me. in a general module.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  13. #13
    Rhubie is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    85
    Ok - the name of the form versus the name of the table - thank you - I have it working - I will need to take an online course for VB I think.

  14. #14
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    You can also use Me in a report module.
    The link below will give you more information.
    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. Conditional required field
    By jaffar2000 in forum Forms
    Replies: 10
    Last Post: 06-05-2012, 06:28 AM
  2. User Required Field
    By rlsublime in forum Access
    Replies: 4
    Last Post: 03-14-2012, 02:15 PM
  3. Cancel when there is a required field
    By dougie in forum Forms
    Replies: 3
    Last Post: 03-07-2012, 09:38 AM
  4. Save as Draft but mandatory field is not required
    By zuerin in forum Programming
    Replies: 5
    Last Post: 06-30-2011, 01:42 AM
  5. Required Field Help
    By brandonb in forum Access
    Replies: 4
    Last Post: 11-13-2009, 11:18 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