Check length of entered value in text field (1 Viewer)

bruceblack

Registered User.
Local time
Today, 12:01
Joined
Jun 30, 2017
Messages
119
Good day to ya all!

I searched Google for like 2 hours and couldn't find what i was looking for.
Please help :(. Should be simple

I want have an if statement to check if the value of a text field on my form is a number, and the length is 5 digits, and then do something.


Something like:

IF value [TextboxOnForm] = numeric AND equal to 5 digits THEN

do something...

Thanks in advance guys!
 

Minty

AWF VIP
Local time
Today, 12:01
Joined
Jul 26, 2013
Messages
10,371
You can use
Code:
If IsNumeric(Me.YourTextBox) and Len(Me.Yourtextbox) = 5 then...
 

missinglinq

AWF VIP
Local time
Today, 07:01
Joined
Jun 20, 2003
Messages
6,423
You should know that the caveat, when using IsNumeric, is that in addition to digits, Decimal Points, Commas, Plus Signs and Negative Signs are considered to be 'numeric,' and will return True. The letters A-F, in combination with digits that could be interpreted as Hex Numbers, i.e. 3d22, are also considered 'numeric' by IsNumeric and will return True!

Linq ;0)>
 

isladogs

MVP / VIP
Local time
Today, 12:01
Joined
Jan 14, 2017
Messages
18,216
digits that could be interpreted as Hex Numbers, i.e. 3d22, are also considered 'numeric' by IsNumeric and will return True!

Thanks. I'd never thought about that ...but it does make sense!
 

missinglinq

AWF VIP
Local time
Today, 07:01
Joined
Jun 20, 2003
Messages
6,423
Here's a demo of a workaround that only allows 0-9 and the decimal point:

Code:
[B]Private Sub TestControl_AfterUpdate()

 Dim i As Integer
 Dim hits As Integer

 For i = 1 To Len(Me.TestControl)
      If Not Mid(Me.TestControl, i, 1) Like "[0-9.]" Then
       hits = hits + 1
      End If
    Next
   
    If hits > 0 Then
     MsgBox "False"
    Else
     MsgBox "True"
   End If
 End Sub
[/B]
Linq ;0)>
 
Last edited:

Mark_

Longboard on the internet
Local time
Today, 04:01
Joined
Sep 12, 2017
Messages
2,111
I want have an if statement to check if the value of a text field on my form is a number, and the length is 5 digits, and then do something.

Will this be used for other entries BESIDES 5 digit numeric? I am guessing you are looking for "00000" for your number, is this correct?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:01
Joined
May 7, 2009
Messages
19,234
you can also use:

If [TextOnForm] Like "[0-9][0-9][0-9][0-9][0-9]" Then
' do something

Else
' do another thing

End If
 

missinglinq

AWF VIP
Local time
Today, 07:01
Joined
Jun 20, 2003
Messages
6,423
Note that arnelgp's clever code not only checks that all characters entered are Numeric, it also checks that there are 5 digits entered!

Linq ;0)>
 

Users who are viewing this thread

Top Bottom