Update calculated field on form

  • Thread starter Thread starter Peter Gloor
  • Start date Start date
P

Peter Gloor

I have a form with some text fields on it. One of the fields is limited to
35 characters, but the user should enter as much relevant information as
possible.

In order to let the user see how many characters he/she already has entered
I wanted to place a field on the form with the actual size of the text that
is updated each time the user adds/removes a character.

How can this be achieved?

I tried to use TitleLen.Text = CStr( Len(Title.Text)) in the Title_KeyPress
event. But the I get an error at runtime indicating that this is not allowed
if TitleLen does not have the focus.
 
Peter said:
I have a form with some text fields on it. One of the fields is limited to
35 characters, but the user should enter as much relevant information as
possible.

In order to let the user see how many characters he/she already has entered
I wanted to place a field on the form with the actual size of the text that
is updated each time the user adds/removes a character.

How can this be achieved?

I tried to use TitleLen.Text = CStr( Len(Title.Text)) in the Title_KeyPress
event. But the I get an error at runtime indicating that this is not allowed
if TitleLen does not have the focus.

Your VB is showing ;-) In Access VBA you rarely use the
Text property, since it is only meaningful while data is
being entered into the control. In normal usage, you should
use the Value property, which is the default property so you
don't even need to specify it unless you feel it makes you
code easier to read. Putting all that together your line of
code could be:

TitleLen = Len(Title.Text)
or
TitleLen.Value = Len(Title.Text)
or (as I would write it):
Me.TitleLen = Len(Me.Title.Text)

You do want to use the Text property of the Title text box
because the value has not been finalized.

Generally, this kind of code is best placed in the Title
text box's Change event so you don't have to mess around
with funny keypresses such as backspace, etc.
 
Back
Top