Need help with If Statement

G

gigglin''me

I’ve been struggling with this code. It seems to work now except I don’t
understand why the message in grpConcur_AfterUpdate appears when the
condition is "if vendor <> 17 or <> 18" and yet the vendor does = 17 or 18.
Obvioulsy I have something wrong or in the wrong place. Can someone look at
the code and give me feedback.

Not sure how much info you need so will summarize what I’m trying to do here
and if you don’t need it feel free to skip it and go directly to the code
below. I have an input form with several tabs. On the first tab the vendor
and customer info is entered and there is an option group to choose one of 5
programs. The fourth tab has an option group ‘grpConcur’ with three buttons,
Yes, No, and N/A. There are nine option buttons (not in a group) that need to
be checked (all reasons that apply) based certain conditions. The option
group ‘grpConcur’ only applies to Vendors 17 and 18 and only if the program
selected on Tab 1 is 1 or 3. If the vendor is 17 or 18 and the program is 1
or 3 and Concur is Yes or N/A then the nine option buttons are disabled and
the value is false. If all those conditions are met and grpConcur is No, then
the nine option buttons are enabled. But if the vendor is 17 or 18 and the
program is 2, 4, or 5 then the 9 option buttons are disabled and the value is
false.

This is the code and to save space and your time, I’ve only noted one of the
nine option buttons. Also, if there is a better way to write this, feel free
to comment.

Private Sub Form_Current()
If (Me.cboVendor = 17 Or Me.cboVendor = 18) And (Me.grpRecourse.Value = 1 Or
Me.grpRecourse.Value = 3) And (Me.grpConcur.Value = 1 Or Me.grpConcur.Value =
3) Then
Me.btnPAAnaylsis.Value = False
Me.btnPAAnaylsis.Enabled = False

ElseIf (Me.cboVendor = 17 Or Me.cboVendor = 18) And (Me.grpRecourse.Value =
1 Or Me.grpRecourse.Value = 3) And Me.grpConcur.Value = 2 Then
Me.btnPAAnaylsis.Enabled = True

ElseIf (Me.cboVendor <> 17 Or Me.cboVendor <> 18) And Not
(IsNull(Me.grpConcur.Value)) Then
Me.grpConcur.Value = 0
Me.btnPAAnaylsis.Value = False
Me.btnPAAnaylsis.Enabled = False
End If

End Sub

Private Sub grpConcur_AfterUpdate()
If (Me.cboVendor = 17 Or Me.cboVendor = 18) And (Me.grpRecourse.Value = 1 Or
Me.grpRecourse.Value = 3) And (Me.grpConcur.Value = 1 Or Me.grpConcur.Value
= 3) Then
Me.btnPAAnaylsis.Value = False
Me.btnPAAnaylsis.Enabled = False

ElseIf (Me.cboVendor = 17 Or Me.cboVendor = 18) And (Me.grpRecourse.Value =
1 Or Me.grpRecourse.Value = 3) And Me.grpConcur.Value = 2 Then
Me.btnPAAnaylsis.Enabled = True

ElseIf (Me.cboVendor <> 17 Or Me.cboVendor <> 18) And Not
(IsNull(Me.grpConcur.Value)) Then
Me.grpConcur.Value = 0
Me.btnPAAnaylsis.Value = False
Me.btnPAAnaylsis.Enabled = False

MsgBox "The Pre-Approved Program is not available based" &
vbCrLf & _
"on the Vendor and/or Recourse Program selected.", vbOKOnly,
"Reminder"
End If
End Sub

Private Sub grpRecourse_BeforeUpdate(Cancel As Integer)
If (Me.cboVendor = 17 Or Me.cboVendor = 18) And (Me.grpRecourse.Value <> 1
Or Me.grpRecourse.Value <> 3) And Not (IsNull(Me.grpConcur.Value)) Then
Me.grpConcur.Value = 0
Me.btnPAAnaylsis.Value = False
Me.btnPAAnaylsis.Enabled = False
End If

End Sub

Thank you.
 
J

John W. Vinson

I’ve been struggling with this code. It seems to work now except I don’t
understand why the message in grpConcur_AfterUpdate appears when the
condition is "if vendor <> 17 or <> 18" and yet the vendor does = 17 or 18.
Obvioulsy I have something wrong or in the wrong place. Can someone look at
the code and give me feedback.

It's an error in your interpretation of Boolean algebra. The Boolean operator
OR *looks like* the English language conjunction or, but it has a more
specific meaning.

The OR operator takes two arguments, on its left and on its right. If either
or both arguments are TRUE, it returns TRUE; if both arguments are FALSE, it
returns false.

The statement

Me.cboVendor <> 17 Or Me.cboVendor <> 18

will always be TRUE, regardless ov the value of cboVendor! Why?

Suppose cboVendor is 16. Both arguments to OR are True; the result is
therefore True.

Suppose it's 17. Then the first argument to OR is False (17 is NOT unequal to
17), but the second is True (17 *is* unequal to 18).

If you change the OR to an AND you'll get the desired result - AND will return
TRUE if both its arguments are TRUE, and false otherwise.


John W. Vinson [MVP]
 
M

Marshall Barton

gigglin''me said:
I’ve been struggling with this code. It seems to work now except I don’t
understand why the message in grpConcur_AfterUpdate appears when the
condition is "if vendor <> 17 or <> 18" and yet the vendor does = 17 or 18.
Obvioulsy I have something wrong or in the wrong place. Can someone look at
the code and give me feedback.

ElseIf (Me.cboVendor <> 17 Or Me.cboVendor <> 18)



That needs to be AND instead or OR

but a sequence of ElseIf with nearly opposite conditions
would probably be eaier to follow wuth some nested If Else
blocks.

OTOH, including data values such as vendor numbers 17 and 18
in your code is a poor programming practice. (What will you
do if you add another vendor to the special handling list?).

It would be better if could add a field to the vendor table
that indicated the type of special handling. Then you could
use simpler Select Case logic without caring which vendor
number is being processed. You could then modify the list
of special vendors just by editing the table field instead
of having to change your code, retesting to make sure you
didn't mess up something else and redistributing your
database to all of your users.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top