data validation

  • Thread starter Thread starter Grant Nicholson
  • Start date Start date
G

Grant Nicholson

i am trying to validate the data in a textbox using the following code:

ctl = txtRepairOrder

If Not ctl >= 128000 Or ctl <= 140000 Then
DisplayMessage "Repair Order number is NOT a valid repair order
number!" & (Chr(10)) & "Please enter a valid Repair Order Number."
End If

Response = acDataErrContinue
Me.txtRepairOrder.SetFocus

i get an error saying i must save the field before i can set focus or
gotocontrol.

using RunCommand acCmdUndo clears all the information from the form when i
just want to clear the text box.

any suggestions?
 
you don't mention what control and what event the code is running from. to
validate the data in a specific control immediately when it's entered, try
running this code on that control's BeforeUpdate event, as

ctl = txtRepairOrder

If Not ctl >= 128000 Or ctl <= 140000 Then
Cancel = True
DisplayMessage "Repair Order number is NOT a valid repair order
number!" & (Chr(10)) & "Please enter a valid Repair Order Number."
Me!txtRepairOrder.Undo
End If

do *not* include the "Response..." and "...SetFocus" lines of code.

hth
 
sorry, i did have it in the beforeupdate event.

seems to be working now, except that i get a second error message explaining
that the number did not follow the validation rule. (can live with that for
the time being.)

thanks for the help!
 
sounds like the control has a rule entered in the ValidationRule property,
in addition to the validation code you're running on the BeforeUpdate event.
you don't need both; if you're satisfied with the BeforeUpdate code, then
delete the rule in the control's ValidationRule property.

hth
 
that's what i thought was causing it, but double checked and can't find it.
it only happens the first time you try to put in a number that does not meet
the validation rule. i'll keep searching and greatly appreciate the help!
 
you're welcome :)
you might try putting a "break" on the validation code and stepping through
it, so you can (hopefully) see what's happening.
 
PMFBI but I have a related problem to the last one mentioned here.

I have two sub forms. Choosing a record on one subform displays a certain
subset of records on the other and also sets the value of a text box
(max_value ) on the main form. I have a validation rule in a control on the
second subform that compares what was entered with this text box.
(The rule is in the control, not as an event handler)
The validation works OK but I always get the validation message three times
if I break the rule instead of once. What might be happening?

(There is no other validation, it worked fine before (except that it allowed
anything) and I have only just added that validation. Delete the rule and
it works fine again.
Howard
 
Back
Top