Parentheses are a great way (the only way, as far as I'm concerned) to force
your program to take certain steps when comparing expressions. They're a key
concept of not only vba programming, but most other languages as well (and
any mathematical operations), and AKIAK, work the same across the board.
Access and vba have their own order of operations, which is usually what we
would expect them to be, but given fact that we let the program try and
figure out what we meant if we don't use parenths, I make it a point to put
them in whenever there's more than one expression being evaluated. This is
especially true for logical operators (OR/AND).
I'm not sure how far along you are with programming, but here's a quick
example of how they can be used. It makes the code much easier to read as
well.
Consider a situation where you would want to do something, if X = Y, and X =
Z, but if not we can see if Y = Z and do the same thing
If (((X = Y) AND (X = Z)) OR (Y = Z)) Then...
This breaks down to:
If X=Y and X=Z
or if Y=Z, do something
if you had this all in one line (if X=Y AND X = Z OR Y = Z) you would find a
different result of the expression.
You can have as many sets of them in an expression as you would like, even
if they don't really do anything. You'll often see this looking at SQL views
of queries built in the designer:
WHERE (((x = y)))
As far as I know there is no limit on the amount you can use (I have a TI
calculator that takes at least 50 sets per expression, probably more). Many
programmers could simplify If/Then/Else and Select Case statements by proper
use of these.
One more very nice thing about using them is readability. It makes it very
easy to break the code into more readable blocks (used with the vba line
break character "_" :
If ( _
((X = Y) AND (X = Z)) _
OR (Y = Z) _
) Then
This probably looks a bit more confusing actually, but the deeper you go
with them the easier they are to read. This layout is especially useful when
wrapping lots of functions around a value:
MyVariable = Nz( _
Trim( _
Str( _
DLookup("myField", _
"myTable", _
"myWhereClause" _
) _
) _
) _
, "")
(I suppose this example is a bit on the weak side as well, but again, as
your code gets more advanced with complex functions inside other functions,
you'll find this pretty handy).
I should also point out that I'm *almost* positive that this is the fastest
and most efficient way to process multiple expressions.
Maybe you already know all this, but as it's such an important concept in
programming I thought I'd throw it out there just in case.
Anytime you have more than one expression, parenthesize it just to be on the
safe side.
Have fun!
--
Jack Leach
www.tristatemachine.com
"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
Blakey300 said:
Hi
I am useing access 2007 and I am a bit thick when it comes to coding. I
have a piece of code that allows me to send bulk emails to addresses in my db
which i have got working fine. However I now need to add additional "Where"
statements and not sure how to do it.
My current statement is as follows:
strSQL = "SELECT [E-MailAddress] FROM [Students] " _
& "WHERE [E-MailAddress] Is Not Null;"
and what i need is something like this:
strSQL = "SELECT [E-MailAddress] FROM [Students] " _
& "WHERE [E-MailAddress] Is Not Null And [Active] Is Not Null;"
but when I run this it ignores the And statement when i run the code.
Any ideas
Regards
Dave