Entry check

  • Thread starter Thread starter Ezekiël
  • Start date Start date
E

Ezekiël

Could someone help me with a code to check if an entry in a textbox is 8
numbers or not.

Something like if it is a number or not then messagebox. Then if it is a
number check if it has 8 digits, if not messagebox.

Greetings,

Ezekiel
 
To force the user to stay in the box until 8 digits are entered, use the
BeforeUpdate event of the text box.

To prevent the box accepting anything other than a digit, use the KeyPress
event of the text box.

These examples assume the text box is named "MyBox", and you have set the
two propeties to:
[Event Procedure]

Private Sub MyBox_BeforeUpdate(Cancel As Integer)
With Me.MyBox
If Not IsNull(.Value) then
If Len(.Text) <> 8 Then
Cancel = True
MsgBox "8 digits required."
Else
If Not IsNumeric(.Value) Then
Cancel = True
MsgBox "Numbers only."
End If
End If
End If
End With
End Sub

Private Sub MyBox_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End If
End Sub
 
the following input mask will demand 8 numbers:

00000000

(those are zeros, not letter o's)
access will generate an error msgbox when needed, but i'm
afraid it's not user friendly. when i use an input mask, i
usually explain the data entry requirements in the
control's Status Bar Text and/or the Control Tip, or
sometimes on the form itself - for instance, in a label
under the control.

hth
 
if IsNull(txtBox) then err.Raise 99999, "MyError","Nothing There!"
If not Is Numeric(txtBOx) then err.raise 999999, "MyError,"Not
Numeric"
if Len(cstr(txtBox) <> 8 then Err.Raise 99999, "MyError", "Wrong Size"

If you wrap the whole thing in an error handler, it will exit
gracefully, otherwise it will stop at the line that generates an
error.
Of course you could also use the more conventional
if IsNull(txtBox then
MsgBox "...."
elseif .... and so one, But I have found over the years that the
former leads to much cleaner code

Regards Greg Kraushaar
Wentworth Falls Australia
(Do not email - the reply address is a Spam spoofer)
(If you really must, remove all UCase and numbers)
 
Hi Allen,

Thx for the code, i'm going to try it out. By the way what does keyascii
mean?

Also, i've posted an other subject about comboboxes and i've been referred
to your website to get the answer. However i already know this, but i want
to go a litlle further.
How about when you go to the next record and the value in second combobox is
different than the record before. How can you 'synchronize' the values of
the first combobox.

e.g.

in combobox 1, i have some values that has different values in combobox 2.
Each time i select a value from combobox 1, the matching value appears in
combobox 2. When i choose another value in combobox 1 the values changes in
combobox 2. This works fine, but i go to the next record the value in
combobox 2 has changed where i should expect another value.

Can you solve this?

Allen Browne said:
To force the user to stay in the box until 8 digits are entered, use the
BeforeUpdate event of the text box.

To prevent the box accepting anything other than a digit, use the KeyPress
event of the text box.

These examples assume the text box is named "MyBox", and you have set the
two propeties to:
[Event Procedure]

Private Sub MyBox_BeforeUpdate(Cancel As Integer)
With Me.MyBox
If Not IsNull(.Value) then
If Len(.Text) <> 8 Then
Cancel = True
MsgBox "8 digits required."
Else
If Not IsNumeric(.Value) Then
Cancel = True
MsgBox "Numbers only."
End If
End If
End If
End With
End Sub

Private Sub MyBox_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.
Ezekiël said:
Could someone help me with a code to check if an entry in a textbox is 8
numbers or not.

Something like if it is a number or not then messagebox. Then if it is a
number check if it has 8 digits, if not messagebox.

Greetings,

Ezekiel
 
Right-click your text box and choose Properties.
On the Event tab of the Properties box, set the On Key Press event to:
[Event Procedure]
Click the build button (...) beside this.
Access opens the code window, and fills in Sub and End Sub lines for you.
On the Sub line, notice that it includes KeyAscii inside the brackets?

KeyAscii is a number supplied by Access in this event, and it notifies you
about what key was pressed. You can read the KeyAscii value to see it it was
a digit. You can also set the KeyAscii value to zero to prevent the
keystroke from ever reaching the text box.

Regarding synchronizing a 2nd combo so that its drop-down list of choices is
dependent on the first, see:
http://www.mvps.org/access/forms/frm0005.htm

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Ezekiël said:
Hi Allen,

Thx for the code, i'm going to try it out. By the way what does keyascii
mean?

Also, i've posted an other subject about comboboxes and i've been referred
to your website to get the answer. However i already know this, but i want
to go a litlle further.
How about when you go to the next record and the value in second combobox is
different than the record before. How can you 'synchronize' the values of
the first combobox.

e.g.

in combobox 1, i have some values that has different values in combobox 2.
Each time i select a value from combobox 1, the matching value appears in
combobox 2. When i choose another value in combobox 1 the values changes in
combobox 2. This works fine, but i go to the next record the value in
combobox 2 has changed where i should expect another value.

Can you solve this?

Allen Browne said:
To force the user to stay in the box until 8 digits are entered, use the
BeforeUpdate event of the text box.

To prevent the box accepting anything other than a digit, use the KeyPress
event of the text box.

These examples assume the text box is named "MyBox", and you have set the
two propeties to:
[Event Procedure]

Private Sub MyBox_BeforeUpdate(Cancel As Integer)
With Me.MyBox
If Not IsNull(.Value) then
If Len(.Text) <> 8 Then
Cancel = True
MsgBox "8 digits required."
Else
If Not IsNumeric(.Value) Then
Cancel = True
MsgBox "Numbers only."
End If
End If
End If
End With
End Sub

Private Sub MyBox_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.
Ezekikl said:
Could someone help me with a code to check if an entry in a textbox is 8
numbers or not.

Something like if it is a number or not then messagebox. Then if it is a
number check if it has 8 digits, if not messagebox.

Greetings,

Ezekiel
 
Back
Top