So I've look all over, but can't seem to grasp the concept of validating my form code wise.
I have a contact form that I need to validate prior to saving to verify the following:
1 - If a company or (first name & last name) have not been entered then cancel the save/close event.
2 - I have 3 cascading comboboxes that if the fields are null, then cancel the save/close event.
(If the above field(s) has been validated then I want to run a message box verifying that they want to save the contact.)
I'm including the validation code I have now, but it doesn't work. With the code below, it just saves everything, or if I input a company name it asks if I want to save. It's like i'm only getting to the first If only. Also, i have not included the comboboxes as I figure I'll start small and add in until I get what I need.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error Resume Next
If Me.Dirty Then
If Not IsNull(Me.Company) Then
If MsgBox("The record has changed - do you want to save it?", _
vbYesNo + vbQuestion, "Save Changes") = vbNo Then
Me.Undo
ElseIf IsNull(Me.First_Name) Then
MsgBox "You need to fill out a 'Company' or 'First & Last Name'!"
Cancel = True
ElseIf IsNull(Me.Last_Name) Then
MsgBox "You need to fill out a 'Company' or 'First & Last Name'!"
Cancel = True
Else
If MsgBox("The record has changed - do you want to save it?", _
vbYesNo + vbQuestion, "Save Changes") = vbNo Then
Me.Undo
End If
End If
End If
End If
End Sub
I've done ElseIf and Else If, that doesn't seem to make a difference. I believe I'm going about the validation process all wrong, but I just can't seem to grasp the idea yet. Anything is a help, Thanks!