Solved

VBA - Multiple OR In a statement.

Posted on 2012-03-28
11
621 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
  • 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 33

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 47

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 33

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 47

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now