Solved

VBA "or"  Statement, parentheses   in correct place.

Posted on 2011-09-07
10
335 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
[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
  • 3
  • 2
  • +2
10 Comments
 
LVL 47

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 75

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 47

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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 47

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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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