Function to test if TextBox entry is a 4-digit number.

J

John S. Ford, MD

I need to write a function to test whether a text box contains an entry of a
4-digit number? The text box in question is bound to a "text" field. In
other words, I want to know if my user has typed in a 4-digit number (TRUE)
or anything else including no entry at all (FALSE).

I want to avoid using an input mask.

Thanks in advance,
John
 
F

fredg

I need to write a function to test whether a text box contains an entry of a
4-digit number? The text box in question is bound to a "text" field. In
other words, I want to know if my user has typed in a 4-digit number (TRUE)
or anything else including no entry at all (FALSE).

I want to avoid using an input mask.

Thanks in advance,
John

I'm a bit unsure of exactly what you are asking here.
If the field is text you can enter letters and/or numbers.

Using VBA ...
If Len([TextField])=4 Then
' True it is 4 characters
Else
' False it's not 4 characters
End If

In an Access Control:
=IIf(Len([ControlName]) = 4,True,False)

The above checks the length of the text field entry, not whether it is
necessarily a number, nor if it is a number, whether it's over 1000.
As text, Access will accept an entry of 0012, which is 4 characters,
yet as a number it's value is just 12.

If you need to assure 4 Numbers (whole numbers .... no decimal part),
then

In VBA...
If Val([TextField])>=1000 and Val([TextField])<=9999 Then
' it's 4 numbers
Else
' It's not 4 numbers
End If

Using the IIf function:
=IIf(Val([TextField])>=1000 and Val([TextField])<=9999,True,False)

0012 will be false, 1200 will be true.
 
J

John S. Ford, MD

Fred, those functions fail if the number is 0000 for example (which I want
to have evaluated as TRUE). Also, letters are considered the same as
digits. 00a0 for example should evaluate as FALSE.

John

fredg said:
I need to write a function to test whether a text box contains an entry
of a
4-digit number? The text box in question is bound to a "text" field. In
other words, I want to know if my user has typed in a 4-digit number
(TRUE)
or anything else including no entry at all (FALSE).

I want to avoid using an input mask.

Thanks in advance,
John

I'm a bit unsure of exactly what you are asking here.
If the field is text you can enter letters and/or numbers.

Using VBA ...
If Len([TextField])=4 Then
' True it is 4 characters
Else
' False it's not 4 characters
End If

In an Access Control:
=IIf(Len([ControlName]) = 4,True,False)

The above checks the length of the text field entry, not whether it is
necessarily a number, nor if it is a number, whether it's over 1000.
As text, Access will accept an entry of 0012, which is 4 characters,
yet as a number it's value is just 12.

If you need to assure 4 Numbers (whole numbers .... no decimal part),
then

In VBA...
If Val([TextField])>=1000 and Val([TextField])<=9999 Then
' it's 4 numbers
Else
' It's not 4 numbers
End If

Using the IIf function:
=IIf(Val([TextField])>=1000 and Val([TextField])<=9999,True,False)

0012 will be false, 1200 will be true.
 
S

Stuart McCall

John S. Ford said:
I need to write a function to test whether a text box contains an entry of
a 4-digit number? The text box in question is bound to a "text" field. In
other words, I want to know if my user has typed in a 4-digit number (TRUE)
or anything else including no entry at all (FALSE).

I want to avoid using an input mask.

Thanks in advance,
John

Public Function LegalNumber(txt As Access.TextBox) As Boolean
LegalNumber = (Len(txt.Value) = 4 And IsNumeric(txt.Value))
End Function

Call it from the form's module, like this:

Debug.Print LegalNumber(Me.txtMyTextBox)

If you can ensure that txtMyTextBox has the focus before you make the call,
then you could use the .Text property instead of .Value.
This will give you the 'current state of play', ie the contents of the
textbox before the bound field is updated.
 
A

Albert D. Kallal

Consider using a input mask.

You likely don't have to have to write any code..and users will not even be
able to type in characters..

Just use

9999

The above would allow 1 to 4 digits, but using

0000

The above will force the text box to require 4 digits. Typing other
characters will make the keyboard go just beep and input will be ignored...

You realy don't need to write a whole bunch of valdation code here...save
your time and coding for other tasks...
 
D

Dirk Goldgar

John S. Ford said:
I need to write a function to test whether a text box contains an entry of
a 4-digit number? The text box in question is bound to a "text" field. In
other words, I want to know if my user has typed in a 4-digit number (TRUE)
or anything else including no entry at all (FALSE).

I want to avoid using an input mask.


If you really don't want to use an input mask (for some unknown reason) ...

If Me.txtMyTextbox & "" Like "####" Then
' It's four numeric digits.
Else
' It isn't.
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top