Return to form if fields are null on close

Sketchin

Registered User.
Local time
Yesterday, 20:41
Joined
Dec 20, 2011
Messages
580
I have a bound form that is used to enter company info (address, name, category...etc). When the user closes the form, if Company name, province or category are left blank, I warn them with a message box asking if they want to exit and undo changes, or return to the form to fill in the missing info.

When they choose the option of returning to the form, I get 2 errors. You must enter a value in "tblCompanies.category", and "You can't save the record at this time, do you want to exit without saving?". If I click "No" on the second warning, focus is set to the missing data control and I can continue working.

How can I prevent the record from being saved when I choose to return to the form to fill in the blank records?

code:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   On Error GoTo Form_BeforeUpdate_Error

Dim Response As Integer

' Determine if required fields are populated.
If IsNull(Me.txtCompanyName) Then
Response = MsgBox("Company name is a required field. Do you wish to discard changes and exit?", vbYesNo, "Warning")
    If Response = vbYes Then
'undo changes
        Me.Undo
        DoCmd.Close
    Else
        Me.txtCompanyName.SetFocus
    End If

ElseIf IsNull(Me.cboProvince) Then

Response = MsgBox("Province is a required field. Do you wish to discard changes and exit?", vbYesNo, "Warning")
    If Response = vbYes Then
'undo changes
        Me.Undo
        DoCmd.Close
    Else
        Me.cboProvince.SetFocus
    End If

ElseIf IsNull(Me.cboCategory) Then

Response = MsgBox("Company Category is a required field. Do you wish to discard changes and exit?", vbYesNo, "Warning")
    If Response = vbYes Then
'undo changes
        Me.Undo
        DoCmd.Close
    Else
        Me.cboCategory.SetFocus
    End If
    
End If
   
   On Error GoTo 0
   Exit Sub

Form_BeforeUpdate_Error:

If Err.number = 2501 Then
    MsgBox "Record not saved", vbOKOnly
    Else
    
    MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure Form_BeforeUpdate of VBA Document Form_frmCompanyDetail"
End If

End Sub
 
I have gotten closer by adding Cancel = true if the user chooses to return to the form, but still get the warning message that "access cannot save the record at this time".
 

Users who are viewing this thread

Back
Top Bottom