TextBox with maximum number of character and textbox to count the remaining (1 Viewer)

omaggi

Registered User.
Local time
Today, 11:13
Joined
Oct 19, 2009
Messages
43
Hi Everyone.
I have a problem with my Project.
I have 2 textbox.
In the first one is possible to write a string of maximum 255 characters.
I want that when I arrive at the 255nd char, it's impossible to write anymore.
In the second I want that we have a "countdown" of possible writable character. The start it will be 255 till 0...

I tried with all the properties of textbox but I don't have a "dinamic countdown"...

Do you have any suggestions?

thx in advance, greetings.

ps: i post the code I use to "block" the first textbox...
Private Sub txtCommento_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo ErrHandle
Select Case KeyCode
Case 8, 46
Case Else
If Len(Me.txtCommento.Text) > txtCommento.Tag - 1 Then KeyCode = 0
End Select
ErrHandle:
If Err.Number = 0 Then Exit Sub
MsgBox "Errore numero: " & Err.Number & " Descrizione: " & Err.Description & " Sorgente: " & Err.Source & " Evento: CAP_KeyDown "
End Sub
 

DCrake

Remembered
Local time
Today, 19:13
Joined
Jun 8, 2005
Messages
8,632
For a start you can get a text box to acept more than 255 chars. Make the box more than one line deep and go into properties of the textbox and change the Keyboard behaiviour to New Line In Field. Then set the vertical scrollbar to true. Then you can type as much as you want.

David
 

omaggi

Registered User.
Local time
Today, 11:13
Joined
Oct 19, 2009
Messages
43
Sorry David but I think I had explain bad my problem.

I want only that when the string I write in the textbox, arrive of a length of 255, it will be impossible to write any other char. I want to do that cause the field is a varchar(255) and I don't want access cut the string...

do you understand?

thx
 

PeterF

Registered User.
Local time
Today, 20:13
Joined
Jun 6, 2006
Messages
295
Hi Everyone.
I have a problem with my Project.
I have 2 textbox.
In the first one is possible to write a string of maximum 255 characters.
I want that when I arrive at the 255nd char, it's impossible to write anymore.
In the second I want that we have a "countdown" of possible writable character. The start it will be 255 till 0...

I tried with all the properties of textbox but I don't have a "dinamic countdown"...

Your code seems to work if I put 255 in the part in red.
Your counter has to be set from here also, one of the first two blue lines will do.
Because no error is genereated when reaching 255 char I put a messagebox in there also.
Code:
Private Sub txtCommento_KeyDown(KeyCode As Integer, Shift As Integer) 
    On Error GoTo ErrHandle
    [COLOR="Blue"]Application.SysCmd acSysCmdSetStatus, "characters remaining " & 255 - Len(Me.txtCommento.Text) 'this prints in the status bar
    me.YourOtherFieldName = characters remaining " & 255 - Len(Me.txtCommento.Text)[/COLOR]
    Select Case KeyCode
       Case 8, 46
       Case Else
       If Len(Me.txtCommento.Text) > [COLOR="Red"]txtCommento.Tag - 1[/COLOR] Then 
          KeyCode = 0
          [COLOR="blue"]msgbox "Max number of chars reached"[/COLOR]
       end if
    End Select
ErrHandle:
    If Err.Number = 0 Then Exit Sub
    MsgBox "Errore numero: " & Err.Number & " Descrizione: " & Err.Description & " Sorgente: " & Err.Source & " Evento: CAP_KeyDown "
End Sub
 

DCrake

Remembered
Local time
Today, 19:13
Joined
Jun 8, 2005
Messages
8,632
So what you are saying is you want a maximum length of 255 characters in the text box?

Create a table underneath the text box called LblCharsLeft

Then on the KeyUp event of the textbox enter

Code:
Me.LblCharsLeft.Caption = 255 - Len(Trim(Me.TextBox)) & " characters left"

Then on the KeyDown event of the textbox

Code:
If 255 - Len(Trim(Me.TextBox)) < 1 then
   Keycode = 8
End If

David
 

omaggi

Registered User.
Local time
Today, 11:13
Joined
Oct 19, 2009
Messages
43
@PeterF

Thx a lot. Works greatly.

Greetings
 

omaggi

Registered User.
Local time
Today, 11:13
Joined
Oct 19, 2009
Messages
43
@PeterF

Only one question.

In the code the select case call 8,46. What does this numbers rappresent for?
Do you know a link where could I find all this "numbers"?

thx, greetings
 

DCrake

Remembered
Local time
Today, 19:13
Joined
Jun 8, 2005
Messages
8,632
8 represents the value when the backspace key is pressed.

If you create a text textbox and on the Keydown event

MsgBox Keycode

then everytime you press down a key it will give you the actual number of the keypress.

David
 

Users who are viewing this thread

Top Bottom