Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

prevent field emptying when escape is pressed, access 2007

Status
Not open for further replies.

svdoerga

Technical User
Apr 28, 2011
26
0
0
US
I have a datasheet form (viewing only) that has a buttons to edit or add records. When you click the add record button, a new form will open and one of the fields (a field that needs consecutive numbering) is filled in from code using

DMax("[field]", "table") + 1

The textbox for this field is locked so it cannot be changed. However, when I press the escape button, the field gets emptied. Locking and/or disabling the textbox has no effect.

I found I can prevent the emptying of the field when escape is pressed by putting 'cancel = true' in the forms undo event, but that seems like overkill to me, because it disables every undo action.

My other thought was to monitor key presses and just refill the field after escape is pressed, but the keypress, keydown en keyup events do not seem to fire.

Is there a way to prevent the field from emptying when escape is pressed while allowing undo?
 
svdoerga . . .

So sorry. Scatch my post [blue]26 May 11 6:14[/blue]. If the textbox is locked you should'nt be ale to enter anything (including ESC). Try locking a textbox on another form and see if you get the same results.

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
vdoerga . . .

?

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Hey AceMan,

The locked textbox gets emptied when I press escape while any other control has focus.

If you're wondering why I am slow to reply, that's because I don't work every day. So sorry for that :p

 
New Postsvdoerga . . .

If during the time you hit [blue]ESC[/blue] the record is in [blue]edit mode[/blue] (depicted by the pencil icon on the record selector), then this is normal. Suppose you accidentally started an unintended new record ... [blue]ESC[/blue] is how you cancel changes.

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Yes, that is the case. I see the pencil on the record selector, which to me makes sense, because I'm adding records with the form.

So are you saying there is no way to prevent the calculated field from emptying while allowing undo (ctrl-z)?
 
svdoerga said:
[blue]So are you saying there is no way to prevent the calculated field from emptying while allowing undo (ctrl-z)?[/blue]
Not Quite! When you write to any control (wether thru the user interface or code), that record goes into [blue]edit mode[/blue] signaling Access that changes are being made. Your confusion arises from the [blue]locked[/blue] control. The [blue]Locked[/blue] property only prevents the user interface. [blue]You can still make changes thru code.[/blue]

Access allows undoing all changes to a record via [blue]ESC[/blue] and [blue]Ctrl+Z[/blue]. These are needed functions, espcially if you realized you were changing the wrong record or perhaps started adding a duplicate record. This is a way to get back to square one. Besides ... the locked control is updated automatically anyway!

To disable the keys, in the forms [blue]On Key Down[/blue] event, copy/paste the following:
Code:
[blue]   If KeyCode = vbKeyEscape Or (Shift = acCtrlMask And KeyCode = vbKeyZ) Then
      Shift = 0
      KeyCode = 0
   End If[/blue]
.
Dont forget to set the forms [blue]Key Preview[/blue] property to [blue]Yes[/blue].

Again ... how would you [blue]Undo[/blue] when needed?

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top