So I have created a basic form for users to enter data when creating a new item. I want to make every field required so I have given each field a tag and then created a If statement to run through those fields and check whether or not each field is null. (code below)
Private Function CheckAllFields() As Boolean
Dim Ctrl As Control
CheckAllFields = False
For Each Ctrl In Me.Controls
If Ctrl.Tag = "*" And IsNull(Ctrl) Then
MsgBox "A required Field has not been filled."
CheckAllFields = True
Exit For
End If
Next Ctrl
End Function
So that code works and will pop up a box if a user tries to close the form without entering something into each field. The code I am using to check after a user goes to close the form is below:
Private Sub Form_Unload(Cancel As Integer)
If CheckAllFields = True Then Cancel = True: Exit Sub
End Sub
Now this all works as it should until I get someone who enters this form by accident and does not need to enter new information. The way it is setup currently the user is unable to close the form because of the Unload statement seeing that fields are null. I have been trying to work around this by messing with an If statement that checks to see if the form is dirty or not because I figured that if someone opens the form and realizes that it is the wrong form they just go to close it and the form should still be Dirty = False. Everything I have tried seems to not work with this dirty property. If anyone could lend some advice as how I can get this to work it would be much appreciated!! Thanks in advance!