limited txt box

K

karim

Hi All,
How can I have a field "text box" take certain characters? like, if I
want the field to contain 2 letters and 2 numbers, but only certain letters.
AB11 or BC22 so if I type HG34 it would not accept the entry?

thanks for the help...
 
S

Steve Schapel

Karim,

I feel sure that what you are asking will be possible.

However, I think you will need to give more details about the "rules" before
anyone could advise specifically. Your examples of AB11, BC22, and HG34,
are helpful, but do not really define the boundary between what is
acceptable and what is not.
 
K

karim

Hello Steve and Steve,
Thanks for the fast replays.

well, I can't have a list box or option box because I will have many
combinations.
I would like to limit the text box to accept the letters AB or CD only for
the 1st 2 inputs, then these 2 letters are followed by any 2 number.
let me know if that explains it better or not.
 
S

Steve Schapel

Hi Karim,

Can you explain what you mean by "the 1st 2 inputs"? Maybe you can give
some specific examples of the data you are working with, to illustrate.

And then, what happens after the 1st 2 inputs? The 3rd input has to be EF?
 
K

karim

Hi Steve
when i said "the 1st 2 inputs", i ment the first 2 letters you will type.
like "AB", then what comes after the "AB" will be any 2 numbers. so the whole
thing would look like "AB25". what i'm trying to do is limit the letters. so
i want the txt box to Only accept AB##. when the user type anything different
that AB, i want the text box not to accept that. but the numbers that come
after the AB can be any numbers...

let me know if that makes it a little bit clearer, and thanks for trying to
help.
 
P

Peter Hibbs

Karim,

Try this :-

In the On Key Press event of your Text box enter this-


Private Sub TextField_KeyPress(KeyAscii As Integer)

KeyAscii = Asc(UCase(Chr(KeyAscii)))
If Chr(KeyAscii) Like "[!ABCD0-9]" And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If

End Sub

where TextField is the name of your Text box control. This will ignore
all characters except A, B, C, D and digits 0 to 9. If you need
different characters just include them after the exclamation mark like
this !ABCDE or whatever. If the characters are contiguous then you
could use !A-F to allow all letters from A to F.

The first line of code just converts all lower case letters to upper
case (which you may or may not need).

In the Input Mask property of the Text box enter LL00 (that is two
zeros) to limit the first two characters to letters and the last two
to digits. Note that you could just use the Input Mask facility to do
what you want but the users could still enter invalid characters, the
code stops them doing that.

It's not clear from your posts but if the character combinations are
specific, that is only AB is allowed and only CD is allowed (i.e. AC
not allowed and so on) then you would need a bit of code in the After
Update event to check for valid combinations and show an error message
if they are not valid.

HTH

Peter Hibbs.
 
S

Steve Schapel

Karim,

So, do I understand correctly? The rules are:
- the data must be 4 characters
- the first 2 characters must be either AB or CD
- the last 2 characters must be digits

Is that right?

Well, you could set a Validation Rule in the table design, like this:
Like "AB##" Or Like "CD##"
.... and then whatever you enter as the Validation Text will be shown, if the
data entered does not meet the rule, when the record is being updated.

Or, you could put code on the Before Update event of the textbox on the
form. One way this could go would be like this:

If Me.YourField Like "AB##" Or Me.YourField Like "CD##" Then
' ok
Else
Cancel = True
MsgBox "Invalid data"
End If

Am I on the right track?
 

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