Making all fields required

tim1234

Registered User.
Local time
, 19:08
Joined
Nov 19, 2002
Messages
38
I have a form that I want to make all fields required so that the user cannot leave that form until all fields are filled out. I have the Required property in the table set to yes, and this works fine if I go to the form from the database window. However, If I go to the form from the main switchboard (In Add Mode), the form can be closed without all of the fields being completed. I know it's probably something pretty simple, but I haven't been able to figure it out. Any help would be greatly appreciated. Attached is a picture of the form if it would help at all.
 

Attachments

  • screen.jpg
    screen.jpg
    81.2 KB · Views: 194
I offer a couple of options.

You must use the forms BeforeUpdate event for any type of validation!

Key "Required" in the Tag property of the form object for each text box, etc.

Place this code in the forms BeforeUpdate event.

Code:
    Dim ctl As Control
    For Each ctl In Me
        If ctl.Tag = "Required" Then
            If IsNull(ctl) Or ctl = "" Then
                MsgBox "You must complete all required fields to continue", vbCritical, "Required Field"
                ctl.SetFocus
                Exit Sub
            End If
        End If
    Next
    Set ctl = Nothing

Also, here is a link [with a sample db] to another method I use to prevent a user from closing a form until they jump through my hoops but this might be overkill if you use the above but this link will show you another method... Enable/Disable The Control Box X Button
 
Thanks for the help

Thanks for the suggestions. I'll give them a try.
 
Still Having a Problem

I'm probably not getting this right, I pasted the code into the BeforeUpdate event (as shown in attachment) and I am still able to close the form with missing fields.

I also typed "Required" in the Tag property on the form fields. Still able to close with missing data.

Thanks for your help.
 

Attachments

  • code.jpg
    code.jpg
    56 KB · Views: 172
One step closer to working

With the word Required typed into all the tag property fields on my form and the code pasted into the beforeupdate property field, it is now showing the "You must complete all fields" message. The only problem is that when you click OK, it closes the form instead of focusing back on it. Thanks again.
 
it closes the form instead of focusing back on it.
You need to cancel the event.

Code:
    Dim ctl As Control
    For Each ctl In Me
        If ctl.Tag = "Required" Then
            If IsNull(ctl) Or ctl = "" Then
                MsgBox "You must complete all required fields to continue", vbCritical, "Required Field"
                ctl.SetFocus
                [COLOR=Blue]Cancel = True[/COLOR]
                Exit Sub
            End If
        End If
    Next
    Set ctl = Nothing
 
Finally got it. Thanks a lot!

Thanks for all your help. I finally got it to work.
 
Method Used

I typed required in the tag property box and added the code to the before update property field. Then I removed my close button that I had created and added a save (refresh) button. So to leave the form they must click the save (resfresh) button, and then click the close button. If they click save first, then the error pops up requiring them to fill in all fields. Probably a complicated way to do something simple, but the VB stuff is just a little too hard for me to figure out sometimes.
 

Users who are viewing this thread

Back
Top Bottom