PDA

View Full Version : Solved: Troubles With IF Not Expression



CreganTur
06-06-2008, 07:45 AM
Okay, I'm validating text entered into a textbox using the After update event. I want to check and see if the value entered is not a certain value, and throw a message if the entered value is not one of the prescribed values in the expression.

Problem is that it's not working correctly. It will throw the error message no matter what.

Here's some example code:
Private Sub txtField_AfterUpdate()
If Not Me.txtField = 1 Or 2 Or 3 Then
MsgBox "Invalid Entry"
End If
End Sub

As you can see, my intention is that if the user updates the textbox with anything other than 1, 2, or 3, then the MsgBox should show. But it's showing even if I enter 1, 2, or 3:banghead:

I've attached a sample database that puts all of this into practice. I'm hoping someone can tell me why it's not working!

Oorang
06-06-2008, 07:57 AM
"Not" only flips the appended value. So this expression
If Not Me.txtField = 1 Or 2 Or 3 Then Could also be read as:
If (Not Me.txtField = 1) Or 2 Or 3 Then "If" evaluates everything as boolean. Implicit Numeric to boolean conversion changes 0 to False and any other positive or negative number to True. So the above could also be read as:
If (Not Me.txtField = 1) Or True Or True Then Which means this expression will always evaluate to True. To do your expression the way I think you intended you would want to make it:
If Not (Me.txtField = 1 Or Me.txtField = 2 Or Me.txtField = 2) Then

CreganTur
06-06-2008, 08:26 AM
To do your expression the way I think you intended you would want to make it:
If Not (Me.txtField = 1 Or Me.txtField = 2 Or Me.txtField = 2) Then

:doh: I knew that, but I forgot about doing it because I just got done writing about 50 cases that evaluate multiple parts, which only require you to separate what you're evaluating with a comma, instead of providing the entire expression.

Thanks, as always:thumb