Dont Save OnClose if..

B

Bob V

This form has about 12 Text Boxes But when I click cmdClose I dont want the
record to save if [tbName] Control Source [OwnerLastName] Text Field
This dose not seem to work..........Thanks for any Help.........Bob

Private Sub cmdClose_Click()
If IsNull(tbName) Then
If Me.Dirty Then
Me.Undo
End If
End If

DoCmd.Close acForm, Me.Name
End Sub
 
A

Allen Browne

This could be a timing issue, Bob, i.e. the record might already be saved
before this code runs.

The only way you can be sure to catch this is to use the BeforeUpdate event
procedure of the *form*. Access fires this event just before the record gets
saved. You cancel the event if you don't want the record to be saved.

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If IsNull(Me.txName) Then
Cancel = True
strMsg = "Cannot save while the last name is blank." & vbCrLf & _
"Correct the entry, or press Esc twice to undo."
MsgBox strMsg, vbExclamation, "Invalid data"
End If
End Sub
 
B

Bob V

Thanks Allen BRILLIANT worked perfectly...Bob :)

Allen Browne said:
This could be a timing issue, Bob, i.e. the record might already be saved
before this code runs.

The only way you can be sure to catch this is to use the BeforeUpdate
event procedure of the *form*. Access fires this event just before the
record gets saved. You cancel the event if you don't want the record to be
saved.

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If IsNull(Me.txName) Then
Cancel = True
strMsg = "Cannot save while the last name is blank." & vbCrLf & _
"Correct the entry, or press Esc twice to undo."
MsgBox strMsg, vbExclamation, "Invalid data"
End If
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Bob V said:
This form has about 12 Text Boxes But when I click cmdClose I dont want
the record to save if [tbName] Control Source [OwnerLastName] Text Field
This dose not seem to work..........Thanks for any Help.........Bob

Private Sub cmdClose_Click()
If IsNull(tbName) Then
If Me.Dirty Then
Me.Undo
End If
End If

DoCmd.Close acForm, Me.Name
End Sub
 

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

Top