Timing issue?

M

Mark A. Sam

Hello,

This procedure is on the Keypress event and limits the text going into a
textbox to allow a custom message (I understand that I can limit the len in
the table).

The problem I am having is that the line

[FieldText] = Left([FieldText], 20)

results in the last character typed only in the textbox. If I break
anywhere in the code, however it reassigns correctly so that the last 20
charaters typed are in the textbox.

Example: I if type in "This is a test 12345" that is 20 charaters. If I
then type the number 6, the textbox ([FieldText]) will contain only the
number 6. If I put a break in the code then resume, the textbox will
contain "This is a test 12345".


Private Sub FieldText_KeyPress(KeyAscii As Integer)

If Len([FieldText].Text) >= 20 Then
MsgBox "You cannot enter any more characters!"
[FieldText] = Left([FieldText], 20)
End If

End Sub

Thanks for any help and God Bless,

Mark A. Sam
 
M

Mark A. Sam

Thank you Allen. I know about the .text property. I was playing around and
that is the version I posted. Adding a line KeySscii = 0 solved it:

Private Sub FieldText_KeyPress(KeyAscii As Integer)
Dim str As String

If Len([FieldText].Text) >= 20 And KeyAscii <> 8 Then
str = [FieldText].Text
KeyAscii = 0
MsgBox "You cannot enter any more characters!"
[FieldText].Text = str
End If

End Sub

Allen Browne said:
Mark, you need to work with the Text property in this event.

This might help:
Unbound text box: limiting entry length
at:
http://allenbrowne.com/ser-34.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Mark A. Sam said:
Hello,

This procedure is on the Keypress event and limits the text going into a
textbox to allow a custom message (I understand that I can limit the len
in the table).

The problem I am having is that the line

[FieldText] = Left([FieldText], 20)

results in the last character typed only in the textbox. If I break
anywhere in the code, however it reassigns correctly so that the last 20
charaters typed are in the textbox.

Example: I if type in "This is a test 12345" that is 20 charaters. If I
then type the number 6, the textbox ([FieldText]) will contain only the
number 6. If I put a break in the code then resume, the textbox will
contain "This is a test 12345".


Private Sub FieldText_KeyPress(KeyAscii As Integer)

If Len([FieldText].Text) >= 20 Then
MsgBox "You cannot enter any more characters!"
[FieldText] = Left([FieldText], 20)
End If

End Sub

Thanks for any help and God Bless,

Mark A. Sam
 

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

Top