There are a number of flaws. Your first line of code
would be undoing any data that you've entered, before it's even been tested, assuming that you’d used the name of the your actual control,
Branch_Code, rather that the “generic” name Allan used in his example! Once you correct the textbox name, you’ll never be able to add/edit a record., and
will place you in an endless loop, repeatedly sending you back to the textbox.
Next, what
Dataype is the
Branch_Code? Is it actually
Numeric or is it
Text. Identifying numbers such as this, or telephone numbers or Social Security Numbers should always be
Text. You should
only use a Numeric datatype if you’re
actually going to do math with the “numbers.” If you actually use a
Numeric datatype, Access will actually
strip off any leading zeros, which will give you problems if you have Branch_Code's like
01, 02, etc.
These lines actually check if the data consists of the characters 0-9, sending up a messagebox if it isn’t, but only undoes the data, with the
Else, if it actually is made up of numeric characters!
- If Not IsNumeric(Branch_Code) Then
-
MsgBox "Invalid Format"
-
Else
-
Branch_Code.Undo
-
End If
-
These lines need to be in the
Form’s Before update event, otherwise the user can simply enter nothing in the field. If nothing is entered in the field, the textbox BeforeUpdate event is never triggered
- If Branch_Code = "" Or IsNull(Branch_Code) Then
-
MsgBox " You must enter a value"
-
Cancel = True
-
ElseIf Len(Branch_Code) <> 2 Then ' This makes sure that there must be two characters in this field
-
MsgBox " This field must be 2 characters only"
-
Cancel = True
-
End If
These lines can go in the
Branch_Code_BeforeUpdate, but this
needs to be this
to throw up your message if it’s negative. The general rule is that code checking the
correctness of entered data goes in the
control’s BeforeUpadate event, but code checking
whether any data has been entered at all goes in the
Form’s BeforeUpdate event.
Welcome to
Bytes!
Linq
;0)>