ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Limiting number of characters in textbox (https://www.excelbanter.com/excel-programming/416928-limiting-number-characters-textbox.html)

ViViC via OfficeKB.com

Limiting number of characters in textbox
 
I received help previously on this problem which was brilliant but I missed a
problem that occurs. When entering the numbers from right side of text box
you can continue to enter numbers as you like with no stop. When the person
enters the 5th number it must stop the entry and show message cannot
procedure further and then move to next textbox. The code to date is below.
Any assistance would be greatly appreciated.

Private Sub txtInvClaim_Change()
Dim i As Long
Dim s As String
Dim ba() As Byte ' byte-array
Static bExit As Boolean

If Not bExit Then
s = UCase(Me.txtInvClaim.Text)
ba = s
s = ""
For i = 0 To UBound(ba) Step 2
If ba(i + 1) = 0 Then
Select Case ba(i)
Case 48 To 57
' digits 0-9
s = s & Chr(ba(i))
Case 65 To 90
'letters only in 1st 2 characters
If i <= 3 Then
s = s & Chr(ba(i))
End If
End Select
End If
Next
If Len(s) 1 Then
s = Left$(s, 2) & "/" & Right$("00000" & Mid$(s, 3, Len(s) - 2), 5)
End If
If txtInvClaim.Text < s Then
bExit = True
txtInvClaim.Text = s
End If
End If
bExit = False
End Sub

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200809/1


[email protected]

Limiting number of characters in textbox
 
Hi there!
Try something like this.
You will need to set the Textbox1.MaxLength property either through
the Textbox1 properties menu in the VBE or programmatically (somthing)
like this:
Private Sub UserForm_Initialize()
TextBox1.MaxLength = 5
End Sub

Then you can use the following code to monitor the length of the text
that has been entered with the following code, and do what ever you
want if the condition is true.
Private Sub TextBox1_Change()
If Len(TextBox1.Value) = TextBox1.MaxLength Then
MsgBox "Max length reached: Cannot proceed any further."
TextBox2.SetFocus
End If

End Sub

Hope this helps.

Kind regards

Bernie Russell

ViViC via OfficeKB.com

Limiting number of characters in textbox
 
Thanks Bernie for your reply. I reply the same day but when I look again it
had not be displayed, lost into space. The second part of textbox fills from
the right and if I set the max to 5 it won't allow me to enter or change the
numbers. I need it to fill from right and stop after five numbers with error
message saying it cannot be more than 5 numbers.

I'll keep trying for a solution
ViViC



wrote:
Hi there!
Try something like this.
You will need to set the Textbox1.MaxLength property either through
the Textbox1 properties menu in the VBE or programmatically (somthing)
like this:
Private Sub UserForm_Initialize()
TextBox1.MaxLength = 5
End Sub

Then you can use the following code to monitor the length of the text
that has been entered with the following code, and do what ever you
want if the condition is true.
Private Sub TextBox1_Change()
If Len(TextBox1.Value) = TextBox1.MaxLength Then
MsgBox "Max length reached: Cannot proceed any further."
TextBox2.SetFocus
End If

End Sub

Hope this helps.

Kind regards

Bernie Russell


--
Message posted via
http://www.officekb.com



All times are GMT +1. The time now is 10:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com