Solved

VBA - Multiple OR In a statement.

Posted on 2012-03-28
11
633 Views
Last Modified: 2012-03-31
This may seem like a very basic programming question in VBA but for some reason I can not get this statement to work.  I'm fairly new to VBA.

Here is the statement.

If producttype <> "ABC" Or producttype <> "DEF" Or producttype <> "GHI" Or producttype <> "JKL" Then
     MsgBox ExportRequest, vbInformation, "No Excel Form for Product Type"
    Exit Sub

If I limit it to one <> then it works for that one product type.  

How should this be written in VBA?
0
Comment
Question by:CompTech810
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 334 total points
ID: 37776819
I think you need ANDS:

If producttype <> "ABC" AND producttype <> "DEF" AND producttype <> "GHI" AND producttype <> "JKL" Then
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 334 total points
ID: 37776826
Alternatively Using OR:

If  NOT (producttype = "ABC" Or producttype = "DEF" OR producttype = "GHI" Or producttype = "JKL") Then
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 166 total points
ID: 37776835
You need to use ANDs instead

If producttype <> "ABC" And producttype <> "DEF" And producttype <> "GHI" And producttype <> "JKL" Then
     MsgBox ExportRequest, vbInformation, "No Excel Form for Product Type"
    Exit Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:CompTech810
ID: 37776840
I feel like such an airhead!!  That worked.  Between all the different programming languages I've used I have again confused myself.  Thank You!
0
 
LVL 34

Expert Comment

by:Norie
ID: 37776845
You could use a Select Case statement.

This will only show the message box if producttype is not equal to ABC, DEF etc

If producttype is ABC or DEF or GHI then it will do nothing but that can be changed if required.
Select Case producttype

      Case  "ABC",  "DEF", "GHI","JKL"
              ' do nothing
      Case Else
                MsgBox ExportRequest, vbInformation, "No Excel Form for Product Type"
                Exit Sub
End Select

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 37776846
Glad to help out :)

... jacko's solution is exactly what I posted in the first comment
0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 37776865
I use a function I wrote a several years ago for these types of checks.  It allows me to test to see whether a value is listed among an array of other values passed to the function.  The function looks like:

Public Function IsIn(TestValue As Variant, ParamArray ArrayOfValues() As Variant) As Boolean

    'Tests to see whether a value is in a list of other values.
    'Similar to both the SQL IN ( ) clause and SELECT Case statements
    'Accepts a value of Variant data type, followed by an array of values.
    'Checks to see if the first argument is in the array
    'Ignors NULL values passed in the array

    Dim intLoop As Integer
   
    IsIn = False
   
    If IsNull(TestValue) Then Exit Function
   
    For intLoop = LBound(ArrayOfValues) To UBound(ArrayOfValues)
        isnull(ArrayOfValues(intLoop)) Then
            'ignore that value
        ElseIf TestValue = ArrayOfValues(intLoop) Then
            IsIn = True
            Exit For
        End If
    Next
   
End Function


To use this function in your case, you would simply use the syntax:

If IsIn(ProductType, "ABC", "DEF", "GHI", "JKL") = False Then
    'do something here
Else
    'do somthing else
endif
0
 
LVL 34

Expert Comment

by:Norie
ID: 37776907
fyed

Does it work if you pass an actual array?

For example like this.
    arr = Array(1, 2, 3, 4, 5)

    If IsIn(1, arr)  Then
   ....

Open in new window

0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 37777042
No, I so rarely use arrays that I didn't think of that initially.

However, over time, I have made several slightly modified versions to handle

1.  arrays
2.  comma separated strings (like you might get from an inputbox)

but don't have either of those versions handy.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question