Default Value and Form Validation

  • Thread starter Thread starter Francesca
  • Start date Start date
This is a weird glitch in the program. The validation rule is only meant
for when information is typed into the field. So if nothing is typed in, it
won't get triggered. Instead of using the built in validation rule, you
should do the check manually. In the Before Update of the form add

If not Asset_Num like "BC?????" then
Msgbox "The Asset number must be in the form BC12345!"
DoCmd.CancelEvent
Asset_Num.Setfocus
end if

Kelvin
 
Thanks again Kelvin...

I tried writing the If statement in the Before Update
field itself but it still doesn't seem to trigger it. I
then tried writing the expression in the Expresion builder
but it gives me a syntax error of "The expression you
entered contains invalid syntax..

Should I be using a macro or code builder instead..Or
would you know what is wrong with the syntax?

Thank you very much for any help...
 
After playing around with it a little more I added to the
If statement that you told me to try to the Code builder
and that seemed to not give me any syntax errors..

I am still having the same problem though, that it is
letting me move to the next field without entering any
numbers.. The Before Update code only works when you
start entering numbers and the Input Mask really does the
same thing at that point. Is there anyway to force the
user input the numbers without moving to the next field..

Thank you..
 
Francesca said:
After playing around with it a little more I added to the
If statement that you told me to try to the Code builder
and that seemed to not give me any syntax errors..

I am still having the same problem though, that it is
letting me move to the next field without entering any
numbers.. The Before Update code only works when you
start entering numbers and the Input Mask really does the
same thing at that point. Is there anyway to force the
user input the numbers without moving to the next field..

Thank you..

Francesca -

Did you put Kelvin's code in the BeforeUpdate event of the form, as he
instructed, or in the BeforeUpdate event of the text box itself? Your
description of what it's doing now sounds as though you have it in the
text box's event. Please post the complete event procedure, from its
"Private Sub" header to its "End Sub" footer.
 
If/Then is VBA code therefore it need to go into the code builder. For
future reference, when people mention code, it means to use the code
builder, sorry for not making that clear. The expression is used to help
write a math/logic equation. If you were using the IIF statement, you would
put it here.

Did you put the code in the event for the text box or for the form? Right
click on the box in top left corner of the form in design view and seelct
properties from there. The code from earlier should have gone in the
BeforeUpdate event from here.

I did not relaize you wanted to keep the focus from moving out of the box.
The info I provided earlier was to check the field before you moved to
another record or closed the form. To prevent it from moving out of the
box, put the following modified code in the LostFocus event of that box.

IF Not Asset_Num like "BC?????" then
Msgbox "The Asset number must be in the form BC12345!"
Asset_Num.Setfocus

It would probably help if you make this the first box on your form so that
it won't get skipped. Otherwise, if the user never goes into this box, the
validation can't be performed. You could leave the code from earlier which
will check the field incase that field gets skipped. Good Luck.

Kelvin
 
I realized later that you had specified that the code
should be for the form not for the individual field so I
changed that and it worked. Thanks again for the help..
 
Hello again..

I have another question? I have set up the Before Event
to validate my data when the record is saved but when I
enter a Input Mask of \B\C00000 for the Asset_Num field
and the user enters the 5 numbers which is correct the
Msgbox from the Before Event is being displayed when it
shouldn't be. When I changed the Input Mask to LL00000 I
don't get that message and it lets me save the record.
Also I have the Default Value set to "BC"

Any ideas why?
 
Back
Top