Solved

VBA "or"  Statement, parentheses   in correct place.

Posted on 2011-09-07
10
329 Views
Last Modified: 2012-08-13
VBA "or"  Statement, parentheses in correct place. I am using paranthesis in my "or" statement.
I just wanted to check and make sure that they are in the right place.  Mostly i am concerned with the beginning and ending parentheses in my statement.

Here is the code:
ElseIF (rstMasterView("TREE_SPECIES_CD") = 4010 Or rstMasterView("TREE_SPECIES_CD") = 4011 Or rstMasterView("TREE_SPECIES_CD") = 4012 Or rstMasterView("TREE_SPECIES_CD") = 4014 Or rstMasterView("TREE_SPECIES_CD") = 4015 Or rstMasterView("TREE_SPECIES_CD") = 4018) then
Thanks.
0
Comment
Question by:donnie91910
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36497265
It seems to be okay but I don't believe you need the beginning and ending parentheses.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 125 total points
ID: 36497267
They appear fine to me, however given the statement you posted you don't really need them. You really only need to consider using surrounding parentheses when you have a mix of AND and OR statements, since one binds tighter than the other (IIRC, AND binds tighter than OR, but I can't immediately recall). If you have a homogenous list of boolean operators, then you can simple avoid using the outer parens.
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 125 total points
ID: 36497293
Does VBA have Select Case

Select Case (rstMasterView("TREE_SPECIES_CD")
    Case 4010, 4011, 4012, 4014, 4015, 4018
        ' Do something
    Case <other values>
        ' Do something else
    Case Else
        ' ' blah
End Select

Open in new window

0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36497306
Sorry, first line s/b

Select Case rstMasterView("TREE_SPECIES_CD")
0
 
LVL 10

Assisted Solution

by:plummet
plummet earned 125 total points
ID: 36497339
Yup, all looks OK if a bit complicated. You might find it easier to assign the value to a variable and test that, like this:

dim iSpeciesCD as integer

iSpeciesCD = rstMasterView("TREE_SPECIES_CD")

if (your code here)...............
ElseIF iSpeciesCD = 4010 Or iSpeciesCD = 4011 Or iSpeciesCD = 4012 Or iSpeciesCD = 4014 Or iSpeciesCD = 4015 Or iSpeciesCD = 4018 then

Open in new window


Just makes it a bit easier to read!
0
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

 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 36497655
Yes, VBA has Select Case, and I would use it in this case.  Here is some typical Select Case code:
Select Case intChoice
   
      Case 0
         GoTo ErrorHandlerExit
   
      Case 1
         Call SendAcknowledgement(Me, lngID, strUserAID)
         
      Case 2
         Call CreateFirstReport(Me, lngID, strUserAID)
         
      Case 3
         Call CreateStatusReport(Me, lngID, strUserAID)
         
      Case 4
         strTemplate = "Dunn 1.dotx"
         Call PrintLetter(lngID:=lngID, _
            strUserAID:=strUserAID, _
            strWordTemplate:=strTemplate, _
            strInsured:=strInsured, _
            strClaimant:=strClaimant, _
            frm:=Me, _
            strAppraiser:=strAppraiser, _
            strEmailType:="Envelope")
      
      Case 5
         strTemplate = "Dunn 1.dotx"
         Call PrintLetter(lngID:=lngID, _
            strUserAID:=strUserAID, _
            strWordTemplate:=strTemplate, _
            strInsured:=strInsured, _
            strClaimant:=strClaimant, _
            frm:=Me, _
            strAppraiser:=strAppraiser, _
            strEmailType:="Attachment")
      
      Case 6
         strTemplate = "Dunn 2.dotx"
         Call PrintLetter(lngID:=lngID, _
            strUserAID:=strUserAID, _
            strWordTemplate:=strTemplate, _
            strInsured:=strInsured, _
            strClaimant:=strClaimant, _
            frm:=Me, _
            strAppraiser:=strAppraiser, _
            strEmailType:="Envelope")
      
      Case 7
         strTemplate = "Dunn 2.dotx"
         Call PrintLetter(lngID:=lngID, _
            strUserAID:=strUserAID, _
            strWordTemplate:=strTemplate, _
            strInsured:=strInsured, _
            strClaimant:=strClaimant, _
            frm:=Me, _
            strAppraiser:=strAppraiser, _
            strEmailType:="Attachment")
           
      Case 8
         strTemplate = "Dunn 3.dotx"
         Call PrintLetter(lngID:=lngID, _
            strUserAID:=strUserAID, _
            strWordTemplate:=strTemplate, _
            strInsured:=strInsured, _
            strClaimant:=strClaimant, _
            frm:=Me, _
            strAppraiser:=strAppraiser, _
            strEmailType:="Envelope")
      
      Case 9
         strTemplate = "Dunn 3.dotx"
         Call PrintLetter(lngID:=lngID, _
            strUserAID:=strUserAID, _
            strWordTemplate:=strTemplate, _
            strInsured:=strInsured, _
            strClaimant:=strClaimant, _
            frm:=Me, _
            strAppraiser:=strAppraiser, _
            strEmailType:="Attachment")
      
   End Select

Open in new window

0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 36497659
This is Access VBA, but Select Case works in Excel VBA too.
0
 
LVL 31

Assisted Solution

by:Helen_Feddema
Helen_Feddema earned 125 total points
ID: 36497672
I would set a String variable (strTreeSpecies) to the field value, and check that it is not empty or Null, then set up a Select Case statement for the possible values of strTreeSpecies.
0
 
LVL 10

Expert Comment

by:plummet
ID: 36497985
Select case would indeed be a neater way of doing this
0
 

Author Closing Comment

by:donnie91910
ID: 36498143
thank you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

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