Help with unpredictable code

T

Tony Williams

I have a control on a form that has code which creates a message if the user
does not input any data. If I put this code in the OnLostFocus property of
the control it works fine, but if I put it in the BeforeUpdate property of
the form it doesn't. Here is the code:
Private Sub txtMontha_LostFocus()
Dim MsgStr As String
Dim TitleStr As String
MsgStr = "You can not enter a record without " _
& "completing the Qtr End Date" & vbCrLf & vbCrLf _
& "Do You Wish To Go Back " _
& "And Enter The Qtr End Date?"
TitleStr = "Qtr End Date Must Be Entered"
If IsNull(Me!txtMontha) Or Me!txtMontha = "" Then
If MsgBox(MsgStr, vbYesNo, TitleStr) = vbYes Then
Cancel = True
Me.txtMonthlabela.SetFocus
Me.txtMontha.SetFocus

Else
Me.Undo

End If
End If
End Sub

Two questions:
1. I would prefer it to go in the BeforeUpdate in the event that the user
uses a mouse rather than tabs around the form. Can anyone see where I can
change the code to make it work?

2. I would also like the form to close if the user answers No to the
message. I've tried using Docmd.Close but can't get the sequence right. The
form either closes when I don't want it to or wont close if the user has
input data and just wants to close the form when they've finished.

Thanks
Tony
 
M

Michel Walsh

Hi,

Does the BeforeUpdate event of the FROM fires at all? If your form is not
bound to a RecordSource, the event won't fire. Same if no control bound to a
field is dirty at the moment you "change" of record, or expect somehow to
see the BeforeUpdate event firing.


Hoping it may help,
Vanderghast, Access MVP
 

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

Similar Threads


Top