Mike Krailo
Well-known member
- Local time
- Today, 02:29
- Joined
- Mar 28, 2020
- Messages
- 1,559
I'm trying to do a simple textbox validation to test if the description field is filled out in a very simple test form. This works when using simple code logic, but if using my close and save button, the form closes before there is a chance of correcting the problem on the form. So then I put in logic that first tests if the form is dirty and simply saves the data without closing the form (see below code). This got me farther as now the form doesn't close, but now I have to click the close and save button once to do the save, and once more to close the form.
What is the proper logic to get the desired results of validating many possible form fields and also allowing the user to make corrections before clicking the close and save button once again? Current sample file is attached if you want to play with it. There are two different forms that each show the two different problem scenarios. In each case, leaving the description field blank and entering a current value and attempting to use close and save will demonstrate the issue.
What is the proper logic to get the desired results of validating many possible form fields and also allowing the user to make corrections before clicking the close and save button once again? Current sample file is attached if you want to play with it. There are two different forms that each show the two different problem scenarios. In each case, leaving the description field blank and entering a current value and attempting to use close and save will demonstrate the issue.
Code:
Private Sub CloseBtn_Click()
On Error GoTo ErrorHandler
If Me.Dirty = True Then
' force save and trigger before update without closing form yet.
Me.Dirty = False
Else
DoCmd.Close acForm, Me.Name
End If
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2101 'The setting you entered isn't valid for this property
Resume Next
Case Else
MsgBox "Error " _
& Err.Number & ": " _
& Err.Description, vbExclamation, "There is an ERROR in CloseBtn_Click"
Resume Next
End Select
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.InvDescription) Then
MsgBox "Must provide description"
Cancel = True
Me.InvDescription.SetFocus
End If
' Many more validation code blocks to follow here
End Sub