Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jul 2002
    Location
    North Dakota, USA
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamically count down number of characters left (2000)

    I want to display on a form the number of characters I have left to type in a text box. The text box has a field size limit of 24 characters. I want a label or text box to display the number of characters left (or # that have been entered, either or) in the field. I want this number to decrease as I type the characters in. Can this be done?

    I've tried putting a text box with a control source of =len([fieldname]), but how do I get it to change as I type? I tried adding a .Requery command to the OnKeyPress event of the text box that I'm trying to count, but that didnt' work.

    Sarah

  2. #2
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Dynamically count down number of characters le

    I could get this to work by putting in an unbound text box, then putting this code into the onchange event for the textbox you type int

    Private Sub textfield_Change()
    DoCmd.RunCommand acCmdSaveRecord
    Me!txtRemaining = 24 - Len(Me![textfield])
    End Sub

    And also this in the oncurrent event for the form:

    Private Sub Form_Current()
    Me!txtRemaining = 24 - Len(Me![textfield])
    End Sub

    It was only by putting in the save command that I could get it to update dynamically. I imagine there is a better way but this seems to work.
    Regards
    John



  3. #3
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Dynamically count down number of characters left (2000)

    I would use a couple of labels to display length of text & length remaining. Sub used to update captions:

    Private Sub GetTextLen(ByRef bFocus As Boolean)

    Dim MaxLen As Long
    MaxLen = 24

    With Me
    ' Text property only available when control has focus:
    If bFocus Then
    .LabelTextLen1.Caption = Len(Nz(.Field3_txt.Text))
    .LabelTextLen2.Caption = MaxLen - Len(Nz(.Field3_txt.Text))
    Else
    .LabelTextLen1.Caption = Len(Nz(.Field3_txt))
    .LabelTextLen2.Caption = MaxLen - Len(Nz(.Field3_txt))
    End If
    End With

    End Sub

    This sub called from form OnCurrent event (control may or may not have focus) and on textbox Change event (when control has focus) (if you attempt to get textbox's Text property when control does not have focus, a run-time error will occur):

    Private Sub Form_Current()
    GetTextLen False
    End Sub

    Private Sub Field3_txt_Change()
    GetTextLen True
    End Sub

    In example Field3 is text field whose max length is 24 characters. Nz function used in case field is Null (like when on new record). Sub could be generalized by using parameter variables for MaxLen, textbox name, etc. If interested see attached demo database (A2K format). Form frmTextLen demonstrates use of technique.

    HTH
    Attached Files Attached Files

  4. #4
    2 Star Lounger
    Join Date
    Jul 2002
    Location
    North Dakota, USA
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Dynamically count down number of characters left (2000)

    Thank you, Mark. Your solution worked great.

    Sarah

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •