Access Help - How to ensure a field is enter in the form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wongray
    New Member
    • Jun 2007
    • 17

    Access Help - How to ensure a field is enter in the form

    Hello Again,

    I need help and advise.

    I create a form in my database and I would like certain field in my form have to be entered, meaning, if I goto that field, it will automatically check and if it is empty, it will pop up with an error message "You Must enter this field"

    Can you please advise how to do?

    Thank You
    Raymond
  • BradHodge
    Recognized Expert New Member
    • Apr 2007
    • 166

    #2
    There are a few ways that you can do this. If you just want it filled out before the form is closed, you can set the Field in the table to Required YES.

    If you want it alert the user before going onto the next field on the form, you can use code similar to this...

    Code:
    Private Sub field1_AfterUpdate()
    If IsNull (Me.field1) Then
    MsgBox ("This field is required before proceeding.")
    Me.field1.SetFocus
    End If
    End Sub
    Hope this helps,
    Brad.

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      Brad, your first suggestion is the simplest, of course, but your code won't work. The AfterUpdate event doesn't occur until data has been entered, so if field1 is null, the code won't be run!

      The same code can be run in the Form_BeforeUpda te sub, and will be run after data is entered in any field if field1 is empty.

      You could place the code in the field1_LostFocu s event to check for null and pop the message when the user exits the field, but the code will only run if the user actually enters field1 to begin with. If you only had the single field you wanted to make required, you could set focus to field1 in the Form_Current() sub and then it would work.

      [CODE=vb]Private Sub Form_BeforeUpda te(Cancel As Integer)
      If IsNull(Me.field 1) Then
      MsgBox ("This field is required before proceeding.")
      Cancel = True
      Me.field1.SetFo cus
      End If
      End Sub
      [/CODE]
      [CODE=vb] Private Sub field1_LostFocu s()
      If IsNull(Me.field 1) Then
      MsgBox ("This field is required before proceeding.")
      Me.AnyOtherFiel d.SetFocus 'has to go elsewhere before returning to field1
      Me.field1.SetFo cus
      End If
      End Sub[/CODE]
      Linq ;0)>

      Comment

      • damonreid
        Recognized Expert New Member
        • Jul 2007
        • 114

        #4
        Why not just make them required fields when you set up the table? That way it will not allow you to move focus from that record without having all the information you want filled in?

        Comment

        • BradHodge
          Recognized Expert New Member
          • Apr 2007
          • 166

          #5
          Ahh yes... the old didn't update, so of course it can't check for Null. Must have been sleeping at my desk :)

          Brad.

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            That was Brad's first suggestion, Damon!

            Originally Posted by Brad:
            "There are a few ways that you can do this. If you just want it filled out before the form is closed, you can set the Field in the table to Required YES."

            And the fact of the matter is, a lot of people don't like the standard Access messages that pop up when a validation rule is violated!

            Linq ;0)>

            Comment

            • damonreid
              Recognized Expert New Member
              • Jul 2007
              • 114

              #7
              Sorry not sure how I missed that... brain not working.

              Another option is to have all the controls bar one disabled and when you Change one control set the next one .Enabled = True thus forcing the person to enter values for each field before moving on. Have the Close form button hidden as well until the last option is inputted.

              Comment

              • wongray
                New Member
                • Jun 2007
                • 17

                #8
                Thank you All,

                I still have some problem. Let me try to explain it better.

                I have a form with the following field

                - Project_Name
                - Status

                The Project_Name is a field that I can enter anything
                The Status is base on a Combo List

                When I enter the code into the BeforeUpdate under Event Procedure in the Project_name field. it works, but if I have to cancel it, it will continue to give me the error and I can't exit from the form

                for the Status field, the code does not work, not sure why, does it because it is a combo list?

                Please advise, your help is much appreciated.

                Many Thanks

                Comment

                • missinglinq
                  Recognized Expert Specialist
                  • Nov 2006
                  • 3533

                  #9
                  Code:
                  When I enter the code into the BeforeUpdate under Event Procedure in the Project_name field. it works, [b]but if I have to cancel it, it will continue to give me the error and I can't exit from the form [/b]
                  Not being able to exit from the form is the entire idea behind making a field required! If you could simply ignore the warning it wouldn' t do much good, would it? Are you now saying you want the user to be able to dump the new record if they want to?

                  Is Status a combobox or a textbox?

                  Linq

                  Comment

                  Working...