Full Version: Keeping a user on a form until it is filled in
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
Wildster
Hi,
I've got a form and when I click the close button I created or the next record button, if all the text boxes have not been filled in it displays a message box stating so. The thing is that if this is the case then I do not want the user to be able to leave from the form until they fill in all the required fields, but instead the message appears and then the form is closed or the next record is displayed.
Is there a way of doing this?
Thanks
Doug Steele
Use the form's BeforeUpdate event to check that everything's okay, and set Cancel = True if it isn't.
!--c1-->
CODE
Private Sub Form_BeforeUpdate(Cancel As Integer)
  
Dim strMessage As String
  
[color="green"]' Put your logic here...[/color]
  If IsNull(Me!Text1) Then
    strMessage = strMessage & "Text1  must have a value in it." & vbCrLf
  End If
  
  If Nz(Me!Check1, False) And IsNull(Me!Text4) Then
    strMessage = strMessage & "Text4 must have a value if Check1 is selected." & vbCrLf
  End If
[color="green"]' etc., etc., etc.[/color]
  
[color="green"]' If there are problems, then strMessage will be non-zero.[/color]
  If Len(strMessage) > 0 Then
    MsgBox strMessage
    Cancel = True
  End If
  
End Sub
RAZMaddaz
One option is to go over to the table(s) and for these fields that the text boxes are based on, click in the Field Name and at the bottom change the Field Property - Required to "Yes"
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.