I need help with new record

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I Undo a new record when the user exits a text box leaving all fields
on this record empty. I want to do this automatically and not ask the user to
hit delete?
thanks
Al
 
Al said:
How can I Undo a new record when the user exits a text box leaving all fields
on this record empty. I want to do this automatically and not ask the user to
hit delete?


The Delete key generally only deletes the selected text in
the control that has the focus. Delete will only delete a
record if the record was previously saved and the entire
record is selected.

OTOH, the Escape key can be used to Undo the edits in a
control. Hitting Esc twice will Undo all changes to the
current record, even if it has not been saved. This can be
achieved using code. To undo a specific control:
Me.controlname.Undo
or to undo all the controls and return the record them to
their last saved values:
Me.Undo
 
Hi Marshall,
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?
any idea
thanks
Al
 
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))
 
Back
Top