Validate data

B

Bonnie

I am trying to validate data on a form (A continuous form). I have put this
code in the BeforeUpdate event of the field:

Private Sub QtyLogOut_BeforeUpdate(Cancel As Integer)
Select Case Me![QtyLogOut]
Case Me![QtyLogOut] > Me![QtyOnHand]
MsgBox "You are trying to log out more than we have!", vbOKOnly
Cancel = True
End Select
End Sub

It does not appear to work. Can you give me suggestions.
 
B

Brendan Reynolds

What this code would try to do would be to compare the value of QtyLogOut to
the result of the Boolean expression QtyLogOut > QtyOnHand, which will
return either True (-1) or False (0). This is obviously not what you want.

When testing against just one condition, it would be simpler to drop the
Select Case and use If ... Then ...

If QtyLogOut > QtyOnHand Then
'show message and cancel
End If

If you're using Select Case because you expect to add further tests, you
could do something like this ...

Select Case True
Case QtyLogOut > QtyOnHand
MsgBox "Trying to log out more than we have!"
Cancel = True
Case QtyLogOut < 0
MsgBox "Can't log out a negative amount!"
Cancel = True
End Select
 
B

Bonnie

Thanks Brendan, that's very helpful.

Brendan Reynolds said:
What this code would try to do would be to compare the value of QtyLogOut to
the result of the Boolean expression QtyLogOut > QtyOnHand, which will
return either True (-1) or False (0). This is obviously not what you want.

When testing against just one condition, it would be simpler to drop the
Select Case and use If ... Then ...

If QtyLogOut > QtyOnHand Then
'show message and cancel
End If

If you're using Select Case because you expect to add further tests, you
could do something like this ...

Select Case True
Case QtyLogOut > QtyOnHand
MsgBox "Trying to log out more than we have!"
Cancel = True
Case QtyLogOut < 0
MsgBox "Can't log out a negative amount!"
Cancel = True
End Select

--
Brendan Reynolds (MVP)


Bonnie said:
I am trying to validate data on a form (A continuous form). I have put
this
code in the BeforeUpdate event of the field:

Private Sub QtyLogOut_BeforeUpdate(Cancel As Integer)
Select Case Me![QtyLogOut]
Case Me![QtyLogOut] > Me![QtyOnHand]
MsgBox "You are trying to log out more than we have!", vbOKOnly
Cancel = True
End Select
End Sub

It does not appear to work. Can you give me suggestions.
 
Top