Results 1 to 5 of 5

Thread: Required fields

  1. #1
    New Lounger
    Join Date
    Nov 2002
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Required fields

    [img]/w3timages/icons/frown.gif[/img]I'm using Access 97 and am having trouble in a form. I have a number of fields that I want to be required and have set them as required in the properties. However, I would like an error message to pop-up if the user exits the field without entering any data. Currently the only time the error message shows is when you leave the form. Does anyone know how to make the error occur on leaving the field?

  2. #2
    New Lounger
    Join Date
    Jul 2003
    Location
    Manhattan, Kansas, USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required fields

    In the OnExit event for the control, test to see if the value is Null. If so, popup your own error msgbox and set the focus back on that control.

  3. #3
    New Lounger
    Join Date
    Nov 2002
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required fields

    Thanks, I have tried to write some code to do this but I don't seem to be able to get it to work. Do you have any suggestions on some code that might work?

  4. #4
    Star Lounger
    Join Date
    Apr 2002
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Try This Function

    Copy the code below into a module and save it. Then, in the design of the form for any fields you want to require the user to enter a value in the OnExit property, type in:

    =FieldRequired()

    This will run the function to check if the user entered a value. If the field is null, blank, or has just a single space in it, the user will be stopped from proceeding until an entry is made in the field.

    Good luck!

    ===========CODE BELOW ===============

    Public Function FieldRequired()

    'Used to ensure data is entered in a field of a form
    'This will force the user to enter data in the field before they can exit
    'To run this function, in the OnExit property for the field
    'Type in: =FieldRequired()

    Dim ctlCurrentControl As Control
    Dim strControlName As String
    Set ctlCurrentControl = Screen.ActiveControl
    strControlName = ctlCurrentControl.Name

    If IsNull(ctlCurrentControl) Or ctlCurrentControl.Text = "" Or ctlCurrentControl.Text = " " Then
    MsgBox "You must enter a value for this field", vbCritical, "Must Enter a Value"
    DoCmd.CancelEvent
    End If

    End Function

  5. #5
    New Lounger
    Join Date
    Nov 2002
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Try This Function

    [img]/w3timages/icons/smile.gif[/img]Thanks that works great!

Posting Permissions

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