Dynamically update userform label based on textbox

M

macroapa

Hi, I have a userform with an unbound textbox where the contents will
ultimately be sent to a memo field in a table.

Now I want to limit the textbox length to 500 characters. I've
happily got code to check/validate this before it is sent to the
table, but this is a bit after the fact for the user.

What i want is a label underneath the text box which dynamically
updates as the user is typing into the textbox to show how many
characters they have left.

Now i thought this would be easy as i'd just use the on key press
event and test the length of the textbox and update the label
accordingly. However, it seems the value of the textbox in code
remains as null until you have exited the textbox.

I have used a bodged workaround whereby i have a public variable which
is initiated as 0 and is incremented by 1 in the on key press event.
Whilst this works well to a degree, it doesn't cater for situations
where the user deletes text. Now I know i could write some code to
test if the keypress was a backspace or delete key and if so decrement
my variable by one, but this still won't cater for situations where
the user highlights bulk text and presses delete.

Has anyone got any suggestions?

Thanks.
 
B

Bob Quintal

:
Hi, I have a userform with an unbound textbox where the contents
will ultimately be sent to a memo field in a table.

Now I want to limit the textbox length to 500 characters. I've
happily got code to check/validate this before it is sent to the
table, but this is a bit after the fact for the user.

What i want is a label underneath the text box which dynamically
updates as the user is typing into the textbox to show how many
characters they have left.

Now i thought this would be easy as i'd just use the on key press
event and test the length of the textbox and update the label
accordingly. However, it seems the value of the textbox in code
remains as null until you have exited the textbox.

I have used a bodged workaround whereby i have a public variable
which is initiated as 0 and is incremented by 1 in the on key
press event. Whilst this works well to a degree, it doesn't cater
for situations where the user deletes text. Now I know i could
write some code to test if the keypress was a backspace or delete
key and if so decrement my variable by one, but this still won't
cater for situations where the user highlights bulk text and
presses delete.

Has anyone got any suggestions?

Thanks.

Yes!!!

As you note, the .value property of a textbox only updates on exiting
the box.
However, the behavior of the textbox's .text property has exactly the
opposite behaviour, it is only valid when the user is in the textbox.
so use the keypress event to test the length of textbox.text and your
code will work. Note that the keypress event fires before the text is
updated so your count is 1 less than the real value.
 
M

macroapa

:












Yes!!!

As you note, the .value property of a textbox only updates on exiting
the box.
However, the behavior of the textbox's .text property has exactly the
opposite behaviour, it is only valid when the user is in the textbox.
so use the keypress event to test the length of textbox.text and your
code will work. Note that the keypress event fires before the text is
updated so your count is 1 less than the real value.

Brilliant, thank you. I'm making progress and it works for backspace,
but press the DEL key doesn't trigger the on key press event? Any
ideas why. I've done a simple Msgbox(keyascii) and pressing backspace
returns 8 in the message, but press the del key doesn't return
anything so the DEL key doesn't trigger the event???
 
M

macroapa

Brilliant, thank you. I'm making progress and it works for backspace,
but press the DEL key doesn't trigger the on key press event?  Any
ideas why.  I've done a simple Msgbox(keyascii) and pressing backspace
returns 8 in the message, but press the del key doesn't return
anything so the DEL key doesn't trigger the event???

Right, I think this code now covers all bases!:

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 46 Then
Label3.Caption = Len(Text0.Text) - Len(Text0.SelText)
End If
End Sub
Private Sub Text0_KeyUp(KeyCode As Integer, Shift As Integer)
Dim xL As Integer
If IsNull(Text0.Text) Then
xL = 0
Else
xL = Len(Text0.Text)
End If
Label3.Caption = xL
End Sub
 
B

Bob Quintal

m:
Right, I think this code now covers all bases!:

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 46 Then
Label3.Caption = Len(Text0.Text) - Len(Text0.SelText)
End If
End Sub
Private Sub Text0_KeyUp(KeyCode As Integer, Shift As Integer)
Dim xL As Integer
If IsNull(Text0.Text) Then
xL = 0
Else
xL = Len(Text0.Text)
End If
Label3.Caption = xL
End Sub

Glad you got it working.
 

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