Record Validation

  • Thread starter Thread starter JamesJ
  • Start date Start date
J

JamesJ

I'm using the following code to validate (make sure a field has data) new
and existing records at the form level. My Contacts form has a listbox
populated with a select query and I'm only showing the Listing and Phone
data.
The form has all the fields for editing purposes.
In short, I need to check for a value in the ContactTypeID field 3 ways.
1) If the user clicks the Close button
2) If the user clicks a row in the Listbox(lstContacts)
3) If the user selects Close from the custom menu.

This code does work for the On Click of cmdClose and the BeforeUpdate of the
form.
Question. How might I create 1 routine for all 3 conditions? Is there an
easier way to do this?

Private Sub cmdClose_Click()

Dim Response As Integer

If Not IsNull(Me.Listing) And IsNull(Me.ContactTypeID) Then
'data in ContactTypeID is the required

Response = MsgBox("You failed to enter required data." & vbCrLf & _
"Do you want to continue editing the record?", vbExclamation
+ vbYesNo, _
"Missing Required Data")

If Response = vbNo Then

Me.Undo
Me.RecordsetClone.FindFirst ("ContactID = " &
Me!lstContacts) 'This is to sync the selected list box row with the form
Me.Bookmark = Me.RecordsetClone.Bookmark
Call GlobalClose

Else

Me!ContactTypeID.SetFocus

End If

Else

Call GlobalClose

End If

End Sub


Thanks much,
James
 
If your current record is "dirty" (has been changed and not saved), the
Form_BeforeUpdate() will fire in any situation which causes the record to
save - this includes any time the form moves to another record or closes for
any reason. I think this would cover all of your cases.
So I'd recommend putting your code there.
 
ok.

Thanks much.
James

MacDermott said:
If your current record is "dirty" (has been changed and not saved), the
Form_BeforeUpdate() will fire in any situation which causes the record to
save - this includes any time the form moves to another record or closes
for
any reason. I think this would cover all of your cases.
So I'd recommend putting your code there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top