count number of characters while they are being typed (1 Viewer)

naygl

Registered User.
Local time
Tomorrow, 02:25
Joined
Jan 7, 2002
Messages
58
I have a form which allows the user to enter info into various controls and then send all that data, as a text message, to a cell phone. I need to limit the number of characters to be sent.
I want to be able to change the colour of the text to red, in whatever control is being used at the time, while they are typing. So if they go over the limit of 160 they will be made aware of the fact.


I have some idea of what to do and have tried a few things but have had little luck. Any help would be appreciated.:)

Glen Naysmith:D
 

naygl

Registered User.
Local time
Tomorrow, 02:25
Joined
Jan 7, 2002
Messages
58
to break it down a bit:
I need to be able to know the lengths of the strings from various text boxes and comboboxes (which I can do) as well as the main 'Details' text box. The number of characters, across all controls, should not exceed 160 characters so I want to be able to count the number of characters that are being entered into 'Details' (while they are being entered) so I can warn the user if they are going to exceed the limit.

The main problem is that I can't just use Len(Details) because it won't give me a live count i.e. If I use the KeyPress event of the tbox to keep a count of the chars it gives me the length of the Details tbox before it got focus, not a running total. Hope that makes sense.

Glen Naysmith:D
 

David R

I know a few things...
Local time
Today, 09:25
Joined
Oct 23, 2001
Messages
2,633
You could probably use the OnChange event...

to update an unbound box that shows how many characters they're at, and even bolds or flashes or whatever when they hit 150.
 

Fornatian

Dim Person
Local time
Today, 15:25
Joined
Sep 1, 2000
Messages
1,396
or...

following on from David R's suggestion you could use a custom function suchas:

Public Function HitLimit() as Boolean
Dim strMsg as String

strMsg = Me.Box1 & " " & Me.CBO2 & " " & Me.Text1.Text & " " & etc...

if Len(strMsg) >150 then
HitLimit = True
Else
HitLimit = False
End if

End Function

then elsewhere you can call the function suchas:

If HitLimit Then
Msgbox "You talk too much"
End if

ian
 

naygl

Registered User.
Local time
Tomorrow, 02:25
Joined
Jan 7, 2002
Messages
58
Yeah, the thing is I need to be able to warn the user, when they reach the limit, while they are typing. When I use the Len function it won't tell me the length of the string that has just been typed while the text box still has focus, so I can't warn the user in a timely fashion i.e. before they tab to another control. I just need a way of finding out how many characters are being typed as they are typed.

I can't just use a simple function to count as each character is typed (OnKeyPress) because they might decide to highlight and delete whole blocks of text. And I can't seem to use a combination of a Len() function and a count function, I guess I'm doing something wrong but what?
 
Last edited:

Fornatian

Dim Person
Local time
Today, 15:25
Joined
Sep 1, 2000
Messages
1,396
As you say the problem lies in ascertaining the text in the activecontrol without knowing what it is...

I've just had a brainwave - why not add it all up suchas:

Me.Text1 & Me.Text2 & Me.Text 3 etc...including all controls

then dependent on whether:

Len(Me(ActiveControl.Name))>Len(Me(ActiveControl.Name).Text) will determine whether you need to add/take away the difference from the total.

Then you can do your comparisons.
 

rockman

Senior Member
Local time
Today, 07:25
Joined
May 29, 2002
Messages
190
You can only use the .Text property when a particular control has the focus. Therefore, try something like this:

Code:
Private Sub txtDetails_KeyDown()
   If Len(txtDetails.Text & txtBox1.value & txtBox2.value) > 150 then Call HitLimit
End Sub

Private Sub txtBox1_KeyDown()
   If Len(txtDetails.value & txtBox1.Text & textBox2.value) > 150 then Call HitLimit
End Sub

Private Sub txtBox2_KeyDown()
   If Len(txtDetails.value & txtBox1.value & textBox2.Text) > 150 then Call HitLimit
End Sub
HTH,
Jeff
 

naygl

Registered User.
Local time
Tomorrow, 02:25
Joined
Jan 7, 2002
Messages
58
Thanks Jeff and Ian. All I had to do was add .Text and it all works fine now. How bloody annoying. I had used your hitlimit function Ian, but I didn't see the .Text so I wasn't getting a 'live' count. Anyway, I appreciate your help.
 

Users who are viewing this thread

Top Bottom