Keep form from closing

MysticElaine

Registered User.
Local time
Today, 17:25
Joined
May 26, 2014
Messages
24
I have a form that has some fields with validation rules, which are Not Is Null. I have a private function to validate records (here is a snipet)
Code:
Private Function ValidateRecord() As Boolean
ValidateRecord = False
Select Case Outcome
     Case "Interpretation"
          If IsNull(Me.[Date Provided]) Then
          MsgBox [Date Provided].ValidationText
          Exit Function
          End If
End Select
ValidateRecord = True
End Function

and a form_before update sub
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If ValidateRecord = False Then
Cancel = True
End If
End Sub

The form has a save and close button. I was wondering if there was a way to keep the form from closing if those fields are null. Right now, you click the button, the validation text pops up (it pops up twice too), and then the form closes. Can we keep the form open so they can fix the problem?

I thought about maybe having the (If...Then, Inputbox)code in the Form_BeforeUpdate instead. If I do that, is there a way to do that for date textboxes and comboboxes where you choose the value instead of typing it?

Thanks,
 
You can Cancel the BeforeUpdate event if you don't like the results and the form will remain open.
 
You can Cancel the BeforeUpdate event if you don't like the results and the form will remain open.

Isn't that what I have already...with the cancel=true? The form still closes.

I have now tried this:
Code:
Private Sub SaveAndClose_Click()
On Error GoTo SaveErr
DoCmd.Save
DoCmd.Close acForm, "Case", acSaveNo

Select Case Outcome
    Case "Interpretation"
        If IsNull([Date Provided]) Or IsNull(Providers) Or IsNull(Time) Or ([Providers] = "Outsider" And IsNull(Cost)) Then
        MsgBox "Please make sure the fields Date Provided, Provider, Time and Cost (if applicable) are entered!"
        Else
            If [Service Requested] = "Translation" Then
            MsgBox "Outcome should not be Interpretation when Translation was the requested service!"
            Else
                DoCmd.Save
                DoCmd.Close acForm, "Case", acSaveNo
            End If
        End If
    Case "Translation"
        If IsNull([Date Provided]) Or IsNull(Providers) Or IsNull(Time) Or IsNull([Number of Pages]) Or ([Providers] = "Outsider" And IsNull(Cost)) Then
        MsgBox "Please make sure the fields Date Provided, Provider, Number of Pages, Time and Cost (if applicable) are entered!"
        Else
            If [Service Requested] = "Interpretation" Then
            MsgBox "Outcome should not be Translation when Interpretation was the requested service!"
            Else
                DoCmd.Save
                DoCmd.Close acForm, "Case", acSaveNo
            End If
        End If
    Case "No Service"
        If IsNull(Comments) Then
        MsgBox "Enter a reason for why service was not provided!"
        Else
        DoCmd.Save
        DoCmd.Close acForm, "Case", acSaveNo
        End If
End Select
SaveErr:
If Err.Number <> 0 Then
DoCmd.CancelEvent
MsgBox "All INTAKE fields must be entered!", vbOKOnly, "Error"
End If
End Sub

This took care of my original problem, but now the cancel event doesn't work.

This form basically has two parts. The first part had some fields set to required in the table, so when the form tried to save and close, it would error and the cancel event would keep it from closing. The second part had some fields that would only be enabled once the outcome changed and I wanted to make sure they were entered before the form closed, which I have now corrected with the Select Case.
 
Last edited:
You can also Cancel the Unload event and keep the form from closing. Set a variable in the BeforeUpdate event that tells the Unload event to let it happen.
 

Users who are viewing this thread

Back
Top Bottom