Results 1 to 11 of 11
  1. #1
    Lounger
    Join Date
    Sep 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Required field on a form (2002)

    I have a field [schoolyear] on a form that I want to make a required field that the person using the form must fill in before they can close the form. Ideally a message should popup if you try to close the form without filling in the field.

    My attempts to do this have not met with much success. Any suggestions?

    Thanks

    Paul

  2. #2
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Required field on a form (2002)

    All you need is some code in the form's Before Update event:

    <pre>If Isnull(schoolyear) = True
    msgbox "You must enter school year!",vbcritical
    Cancel = True
    End if</pre>

    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  3. #3
    Lounger
    Join Date
    Sep 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required field on a form (2002)

    Does not work yet. Added then to the first line. Does not work in the before update event. Put in close event and message pops up but does not like the cancel line. Any suggetions?

    If Isnull(schoolyear) = True Then
    msgbox "You must enter school year!",vbcritical
    Cancel = True
    End if

  4. #4
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Required field on a form (2002)

    Did you put that in the forms before update event as Mark suggested?

    I would put an Exit Sub after the Cancel = True command to stop it continuing in the code after the End If.

  5. #5
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Required field on a form (2002)

    Are you sure you put it in the form's BeforeUpdate event (not control's Before Update event).
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  6. #6
    Lounger
    Join Date
    Sep 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required field on a form (2002)

    I tried the following code in the form's Before Update event.

    If IsNull(Me.cboSchoolyear) = True Then

    MsgBox "You must enter school year!", vbCritical
    Cancel = True
    Exit Sub
    End if

    Nothing happens. If I put the code in the form's close event the message appears but the code stops at the cancel line.

  7. #7
    Lounger
    Join Date
    Sep 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required field on a form (2002)

    Still not working the way I want it to but I tried this.

    I made a command button on the form and put this code behind it. Works when you push the button.

    If IsNull(Me.cboSchoolyear) = True Then
    MsgBox "You must enter school year!", vbCritical
    Me.cboSchoolyear.SetFocus
    Exit Sub
    Else
    DoCmd.Close
    DoCmd.OpenForm "Switchboard"
    End If

    Only problem is that it does not work when I try to put the if part of the code in the close event of the form. When you push the x at the top right corner of the form or push the close button in the toolbar, message appears but the form closes when you click OK.

    Any ideas on this ?

  8. #8
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Required field on a form (2002)

    My experience with comboboxes is that you are better off check the listindex property, like this:

    If Me.cboSchoolYear.ListIndex = -1 then
    ... blah, blah
    end if
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  9. #9
    Lounger
    Join Date
    Sep 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required field on a form (2002)

    I switched to your code for the close button on the form and it still works. Does not resolve the issue with hitting the x to close the form.

    Also still can not make the message appear with your original idea of putting code in Before Update event.

    Any other suggestions? Thanks for all the help.

  10. #10
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Required field on a form (2002)

    Resolving the issue about using the "x" button to close the form is easy. Don't allow it!

    I don't know why you can't get the code to work in the form's BeforeUpdate event. I do it all the time.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  11. #11
    Lounger
    Join Date
    Sep 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required field on a form (2002)

    The code in the close button seems to do the job so I'm happy.

    Thanks for all the help.

    Paul

Posting Permissions

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