Al said:
Thanks for replying, I have done this already with no luck. Let me explain.
I have a command button in a sub form to add new record. The button uses the
following code:
DoCmd.GoToRecord , , acNewRec
Me("ProgressNotesDate").SetFocus
I do not want the user to leave the "ProgressNotesDate" without entering a
date if all of the controls on the subform have no data, however, I want to
enable the user to exit and undo the record, if he clicked add new record by
mistak. Me.undo is not achieving this. I placed the following code in the
ProgressNotesDate_on exit:
************************
Dim ctl As Control, frm As Form, strDataEntered As String
For Each ctl In Me.Controls
With ctl
Select Case .ControlType
Case 109, 111
strDataEntered = Nz(Len(strDataEntered + ctl))
End Select
End With
Next ctl
If Len(strDataEntered) > 0 And IsNull(Me("ProgressNotesDate")) Then
msgbox "You Must Enter Progress Note Date"
Cancel = True
Else
Me.Undo
End If
Set ctl = Nothing
Set frm = Nothing
*****************************************
One other problem I got with placing this code in the onExit event is that I
noticed the it fires as soon as the user clicks the add new record command
button even though the user did not try to exit the progressnotesdate field
yet?
That code should go in the form's BeforeUpdate event.
The problem with clicking on the button is that it really
does signal that the user is done editing the text box. I
can think of maybe two ways to deal with this. The most
obvious and simplest is to use the same code to check the
form in the button's GotFocus event. The other, rather
obscure approach is to use a label instead of a button. You
can click on a label, but since it can not receive the
focus, you will not trigger the text box's Exit event.
I also think you may have a logic error in your code. The
expression Nz(Len(strDataEntered + ctl)) will only yeild a
ZLS if the last control on the form is Null. Don't you
really want to use Nz(Len(strDataEntered & ctl))