Can I have a count down of remaining chars of 255?

Steve C

Registered User.
Local time
Yesterday, 20:23
Joined
Jun 4, 2012
Messages
119
TextBox1 has, of course, a maximum of 255 Chars.

I imagine Unbound TextBox2 will count down from 255 with each keystroke entered to TextBox1 so I know when my limit is getting close.

Can anyone point me to the vba for this?
 
You could use the following in TextBox1 On Change event;
Code:
Me.TextBox2 = 255 - NZ(Len(Me.TextBox1.Text),0)
You would also need it in the form's On Current event
 
Sorry the Form's On Current would need;
Code:
Me.TextBox2 = 255 - NZ(Len(Me.TextBox1),0)
 
Here is a function that you can use with any Unbound text box to do what you want:
Code:
Function ChkCharCnt(ControlName As String, MaxLength As Long)
'declare a variable to use when calculating
Dim lngCharsUsed As Long

'calculate the number of characters used
lngCharsUsed = MaxLength - Nz(Len(Me.txtMyTextBox.Text), 0)
'check to see if all characters have been used
If lngCharsUsed >= 0 Then
    Me.lblTextRemain.Caption = lngCharsUsed & " remain."
Else
    Me.txtMyTextBox.Text = Left(Me.txtMyTextBox.Text, 255)
    Me.lblTextRemain.Caption = "The maximum of " & MaxLength & " characters have been used."
End If
End Function

Just provide the name of your unbound text box control and the maximum number of characters you want to allow to be typed into the text box.
 
Thank you JBB that’s perfect – Form OnCurrent Event sets TextBox2 to read 255 and TextBox OnChange Event clicks TextBox2 down one with each key stroke.

Thank you too Mr.B

A year ago I would have glazed over at your answer. Nowadays, I can understand (I think) your Function. It’s brilliant!

So I have a choice of two fantastic answers. Thank you very much.
 
I didn’t say that TextBox1 is on a Sub Form called frmFormSub in Continuous View, so that I can have more than one TextBox1 entry for each Record in the MainForm

How do I get TextBox2 to relate to the TextBox1 with focus?

Next problem is, I’d like is to hide TextBox2 on the Subform and copy it's contents to TextBox3 on the Mainform with something like.

Me.TextBox3=frmFormSub.Form!TextBox2

But, I’m not at that stage yet so I don’t know if that last bit works.
 
Mr. B.

In post #4, why pass ‘ControlName As String’ and not us it?

Chris.
 
Behind the sub-form:-
Code:
Private Sub Form_Current()

    ShowTextRemaining Me.TextBox1.Value, Me.Parent.TextBox3, 255

End Sub


Private Sub TextBox1_Change()

    ShowTextRemaining Me.TextBox1.Text, Me.Parent.TextBox3, 255
    
End Sub

In a standard module:-
Code:
Public Sub ShowTextRemaining(ByVal vntText As Variant, _
                             ByRef ctlTarget As Control, _
                             ByVal lngMaxLength As Long)

    ctlTarget = lngMaxLength - Nz(Len(vntText), 0)

End Sub

Chris.
 
Thank you ChrisO

Your Module looks great - can't wait to try it. But first, I'll take an opportunity to learn what Modules are (I have a vauge outline from the IsLoaded Module I pinched (and use) from Northwind) it's something I need to learn.

I will report back when it's all working for the benefit of anyone else following along behind.

Best wishes (from London UK)
 

Users who are viewing this thread

Back
Top Bottom