Preventing incorrect data in a textbox

  • Thread starter Thread starter Randy
  • Start date Start date
R

Randy

I have 4 textboxes on a form as follows. [Edible], [Inedible] and [Foreign]
must equal [Sample]
[Sample]
[Edible]
[Inedible]
[Foreign] with the Control Source of =[Sample]-([Edible]+[Inedible])

The problem is if incorrect data (Too Much) is entered in [Edible] and
[Inedible] the outcome could be a negative number in [Foreign]. I tried
some code as follows, but it didn't work. Any Ideas..Thanks...Randy

If Me.Foreign < 0 Then
If MsgBox("Incorrect data Entered in Edible and/or Inedible. Please
Correct", vbYesNo + vbDefaultButton2) <> vbYes Then
Cancel = True
End If
End If
 
I have 4 textboxes on a form as follows. [Edible], [Inedible] and [Foreign]
must equal [Sample]
[Sample]
[Edible]
[Inedible]
[Foreign] with the Control Source of =[Sample]-([Edible]+[Inedible])

The problem is if incorrect data (Too Much) is entered in [Edible] and
[Inedible] the outcome could be a negative number in [Foreign]. I tried
some code as follows, but it didn't work. Any Ideas..Thanks...Randy

If Me.Foreign < 0 Then
If MsgBox("Incorrect data Entered in Edible and/or Inedible. Please
Correct", vbYesNo + vbDefaultButton2) <> vbYes Then
Cancel = True
End If
End If

What event did you use for this code? The [Foreign] textbox won't
actually have many events fire, in particular its AfterUpdate and
BeforeUpdate events will not. I think if you put this code in the
Form's BeforeUpdate event it should work correctly.

Actually, thinking about it, you should set Cancel to True whatever
the user answers (you should probably either make them correct the
entry or erase this record altogether)... but I'm not certain what you
want to happen!
 
Thanks John, your suggestions worked great...Randy
John Vinson said:
I have 4 textboxes on a form as follows. [Edible], [Inedible] and [Foreign]
must equal [Sample]
[Sample]
[Edible]
[Inedible]
[Foreign] with the Control Source of =[Sample]-([Edible]+[Inedible])

The problem is if incorrect data (Too Much) is entered in [Edible] and
[Inedible] the outcome could be a negative number in [Foreign]. I tried
some code as follows, but it didn't work. Any Ideas..Thanks...Randy

If Me.Foreign < 0 Then
If MsgBox("Incorrect data Entered in Edible and/or Inedible. Please
Correct", vbYesNo + vbDefaultButton2) <> vbYes Then
Cancel = True
End If
End If

What event did you use for this code? The [Foreign] textbox won't
actually have many events fire, in particular its AfterUpdate and
BeforeUpdate events will not. I think if you put this code in the
Form's BeforeUpdate event it should work correctly.

Actually, thinking about it, you should set Cancel to True whatever
the user answers (you should probably either make them correct the
entry or erase this record altogether)... but I'm not certain what you
want to happen!
 
Back
Top