In
Vladimír Cvajniga said:
Public Function editMemo()
On Error Resume Next
With Screen.ActiveControl
.SelStart = 0
.SelLength = 0
End With
End Function
I'd like to use this function to prevent accidental deletion of memo
(text box) content when user sets focus to an appropriate control, eg.
txtMem

nSetFocus is set to =editMemo().
But I'm experiencing something that I don't understand: when a user
presses Enter on a preceding control Access doesn't clear the key
buffer and adds a CRLF (blank line) to the beginning of the txtMemo
control content (txtMemo.Text).
I must be doing something wrong...
I don't think you're doing anything wrong; I think it's just an
interesting bug in how Access handles keyboard input. Note that the
problem only shows up when the control's EnterKeyBehavior property is
set to New Line in Field. It's also a fact that, when a user's keyboard
action sends the focus from one control to another, it's the receiving
control that processes the last keystroke (Tab or Enter). You can
demonstrate this by putting code in the txtMemo's KeyPress event, and
displaying the value of its KeyAscii argument. Pressing the Enter key
in the previous control causes txtMemo's KeyPress event to fire,
receiving an ASCII value of 13.
It seems to me that there must be internal code attached to text boxes
that ignores the keypress that is pending when the focus enters the
control. But once the focus is in the control, the control must
correctly process the subsequent keystrokes. So there must be a flag
somwehere that tells the control that it has just been entered, and it
should ignore the pending keystroke. Somehow, when you use the
control's GotFocus or Enter event to manipulate its .SelStart,
..SelLength, or .Text property (and maybe some others), that flag must be
reset. So now the control processes the pending keystroke, instead of
discarding it, and enters a newline according to its setting.
As a test, I put this code in the control's KeyPress event, and the
undesirable behavior went away:
'----- start of code -----
Private Sub txtMemo_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
With Me.ActiveControl
If .EnterKeyBehavior = True Then
If .SelStart = 0 And .SelLength = 0 Then
KeyAscii = 0
End If
End If
End With
End If
End Sub
'----- end of code -----
Unfortunately, I don't see how to make this a public function, since you
need the KeyAscii argument.
By the way, I checked, and the same behavior occurs in Access 97.