Counting Characters

  • Thread starter Thread starter plusnet news
  • Start date Start date
P

plusnet news

I am building a form for sending text messges from access but there is a
limit of 160 characters in a sms message.

How can i count the characters in a text box and display the result in a
unbound text box as i type?
 
plusnet news wrote in message
I am building a form for sending text messges from access but there is a
limit of 160 characters in a sms message.

How can i count the characters in a text box and display the result in a
unbound text box as i type?

Use the on change event of the text control, and evaluate the .Text
property of it

me!txtCount = 160 - len(me!txtMsg.Text)
 
Roy
I dont quite get the idea of this!

can you give me the correct format for my form.

Formname= frmsms
textboxname = Mesage
unboundboxname = count

Thanks Stuart
 
Me!count = 160 - Len(Me!Mesage)

Since this code is in the form you can use Me to refer to
the form rather than having to spell out the whole
reference.

160 - Len(Me!Mesage) will give you the number of chars left
and
Len(Me!Mesage) will give you the total length so far.


--
Nick Coe (UK)
http://www.alphacos.co.uk/ AccHelp + pAnimal
http://www.mrcomputersltd.com/ Repairs Upgrades

In plusnet news typed:
 
The Clue here is that plusnet news wishes the unbound txtbox to count as he
types, what you all suggest works but only if the focus is moved to the
textbox when a recalc/repaint is forced. The txtbox is therefore not dynamic
as he required?

My only interest is in your solution for this gentlemen
 
plusnet news wrote in message
Roy
I dont quite get the idea of this!

can you give me the correct format for my form.

Formname= frmsms
textboxname = Mesage
unboundboxname = count

Thanks Stuart

I would suggest renaming at least one of the controls - as count is a
reserved word (property of most collections) you should not use it as
name of objects. Then take a look at my suggestion and see if you can
get it to work;-)
 
MikeJohnB wrote in message
The Clue here is that plusnet news wishes the unbound txtbox to count as he
types, what you all suggest works but only if the focus is moved to the
textbox when a recalc/repaint is forced. The txtbox is therefore not dynamic
as he required?

My only interest is in your solution for this gentlemen

I'm a little uncertain what you're saying, MikeJohnB.

It does seem like you are critisizing the given suggestion, but, from
what you posted, I'm not entirely confident that you know what you're
talking about. Could you please clearify?
 
Roy
You mistake my intension entirely, I don't know enough about access to make
rude comments so I will try again to explain what "I" feel is required.

As the guy types, he wants the Txtbox to indicate the number of charaters
that he has entered to date dynamically. Type the letter t, the txtbox
displays 1, th = 2 this =3 this is a trial =14.

As you suggest, Txtbox = Len(Field) works fine but does not count as you
type. This is true if you put the equation in on change, key down, key up
etc? Perhaps it is because of the reserved word "Count" Once you have
finished typing and move the focus, the text box then returns the total
number of charaters used. I will try again without using the txtbox name
count.

As I said, my only interest is in how you solve this one. I have a use for
this as well.

Thank you and best wishes
 
MikeJohnB wrote in message
<[email protected]> :

I have perhaps a tendency of being a bit direct, but I feel that this
exchange of view wouldn't necessary if paing attention to the details
in
my initial reply. Please correct, if I've misunderstood. Some comments
inline.
Roy
You mistake my intension entirely, I don't know enough about access to make
rude comments so I will try again to explain what "I" feel is required.

As the guy types, he wants the Txtbox to indicate the number of charaters
that he has entered to date dynamically. Type the letter t, the txtbox
displays 1, th = 2 this =3 this is a trial =14.

This is, as far as I know, what I suggested a solution for. If you do
not get it to work, tell which event you're using and which property
you
are measuring - i e - paste your code with event handler.
As you suggest, Txtbox = Len(Field) works fine but does not count as you
type.

I did NOT suggest that. Again - I did NOT suggest that. Please reread
my
suggestion. It is very short, but also quite specific with regards to

- which property of the text control to measure (.Text)
- which event to use for this on the same control (on change)

My suggestion is, I think, close to being a standard/basic approach to
something like this.
This is true if you put the equation in on change, key down, key up
etc? Perhaps it is because of the reserved word "Count" Once you have
finished typing and move the focus, the text box then returns the total
number of charaters used. I will try again without using the txtbox name
count.

I think I was clear that you musts check the .Text property of the
control. This property give the current content of the text control,
and
is the only property of text controls that will contain what is typed
into a control when testing it in the on change event. The .Value
property, which is the default property of the control, meaning you
don't need to specify it, is what it seems you are using - only
contains
what is saved into the control (i e, before starting to type, or after
leaving the control - but not contents while typing)
As I said, my only interest is in how you solve this one. I have a use for
this as well.

I think I did.
Thank you and best wishes

Create a new form. In design view drop two text controls;

txtMsg - the control in which we type
txtCount - the control which is to receive the count

in the on change event of the txtMsg control enter

me!txtCount = 160 - len(me!txtMsg.Text)
 
Create an unbound form with two unbound text controls and a
button on.

txtEntered, txtVarCount and cmdClear

Enter the code below into the forms class module. It
probably could do with tidying up a bit but the basics are
there.

Option Compare Database
Option Explicit
Dim lngChars As Long

Private Sub cmdClear_Click()
lngChars = 0
Me.txtVarCount = Null
Me.txtEntered = Null
End Sub

Private Sub txtEntered_KeyPress(KeyAscii As Integer)

'Choose which keys to inc or dec
Select Case KeyAscii
Case vbKeyBack
'Decrement on backspace
lngChars = lngChars - 1
Case Else
lngChars = lngChars + 1
End Select

Me.txtVarCount = lngChars
Me.Repaint

End Sub

--
Nick Coe (UK)
http://www.alphacos.co.uk/ AccHelp + pAnimal
http://www.mrcomputersltd.com/ Repairs Upgrades

In MikeJohnB typed:
 
Nick Coe (UK) wrote in message said:
Create an unbound form with two unbound text controls and a button on.

txtEntered, txtVarCount and cmdClear

Enter the code below into the forms class module. It probably could do with
tidying up a bit but the basics are there.

Option Compare Database
Option Explicit
Dim lngChars As Long

Private Sub cmdClear_Click()
lngChars = 0
Me.txtVarCount = Null
Me.txtEntered = Null
End Sub

Private Sub txtEntered_KeyPress(KeyAscii As Integer)

'Choose which keys to inc or dec
Select Case KeyAscii
Case vbKeyBack
'Decrement on backspace
lngChars = lngChars - 1
Case Else
lngChars = lngChars + 1
End Select

Me.txtVarCount = lngChars
Me.Repaint

End Sub

Couple of questions, if I may
- how does this deal with copy/paste of text?
- how does this deal with for instance the user pressing ESC, Delete,
Shift+Enter?
- how does this deal if the user presses backspace when there are no
characters in the box?
 
Roy,
It was only a suggestion, my idea being for the OP or
whoever wants it to change it to suit.

The Select Case could be expanded to trap other key
constants.

The counter variable could be tested to check for < 0, that
might take care of backspace on empty.

I didn't intend it to be a complete solution, just a working
model to point to a possible solution.
--
Nick Coe (UK)
http://www.alphacos.co.uk/ AccHelp + pAnimal
http://www.mrcomputersltd.com/ Repairs Upgrades

In RoyVidar typed:
 
Right, pay attention to the detail is the answer and as you say Roy, what you
suggested works, it was the .Text bit that I was missing. Thanks for the
assistance
 

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

Back
Top