Adding Mutiple Where Statements to Code

B

Blakey300

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
 
K

KC-Mass

Put a left parenthesis "(" before the word "WHERE" and a right Parenthesis
")" before the semicolon ";"
Then it enforces the "AND"
 
B

Blakey300

Thanks

I have tried that:

strSQL = "SELECT [E-MailAddress] FROM [Students] " _
& "(WHERE [E-MailAddress] Is Not Null And [Active] Is Not Null);"

and I am getting a syntax error now

Any Ideas?

Regards

Dave

KC-Mass said:
Put a left parenthesis "(" before the word "WHERE" and a right Parenthesis
")" before the semicolon ";"
Then it enforces the "AND"


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
 
K

KC-Mass

Sorry missed the obvious syntax error:

Try
strSQL = "SELECT [E-MailAddress] FROM [Students] " _
& "WHERE ((Not [E-MailAddress] Is Null) And (Not [Active] Is
Null));"



Blakey300 said:
Thanks

I have tried that:

strSQL = "SELECT [E-MailAddress] FROM [Students] " _
& "(WHERE [E-MailAddress] Is Not Null And [Active] Is Not Null);"

and I am getting a syntax error now

Any Ideas?

Regards

Dave

KC-Mass said:
Put a left parenthesis "(" before the word "WHERE" and a right
Parenthesis
")" before the semicolon ";"
Then it enforces the "AND"


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
 
J

Jack Leach

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
 
B

Blakey300

Thanks for your help.

Had a few probs but got it working now, because the active field is a yes/no
box i had to tweak the code to the following:

strSQL = "SELECT [E-MailAddress] FROM [Students] " _
& "WHERE ((Not [E-MailAddress] Is Null) And ([Active]=Yes));"

Couldn't of done it without you!!!

Regards

Dave

KC-Mass said:
Sorry missed the obvious syntax error:

Try
strSQL = "SELECT [E-MailAddress] FROM [Students] " _
& "WHERE ((Not [E-MailAddress] Is Null) And (Not [Active] Is
Null));"



Blakey300 said:
Thanks

I have tried that:

strSQL = "SELECT [E-MailAddress] FROM [Students] " _
& "(WHERE [E-MailAddress] Is Not Null And [Active] Is Not Null);"

and I am getting a syntax error now

Any Ideas?

Regards

Dave

KC-Mass said:
Put a left parenthesis "(" before the word "WHERE" and a right
Parenthesis
")" before the semicolon ";"
Then it enforces the "AND"


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
 
B

Blakey300

Thanks for your tips Jack, they are very useful so i have saved them for
future reference, as I tend to avoid using code at the moment as I am not
that good at it purely because I haven't read enough books on it YET

Regards

Dave

Jack Leach said:
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
 
J

Jack Leach

Here's something you might want to take a look at. It's perfect for people
just tapping into the "true power" of access:

http://www.accessmvp.com/Strive4Peace/


--
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:
Thanks for your tips Jack, they are very useful so i have saved them for
future reference, as I tend to avoid using code at the moment as I am not
that good at it purely because I haven't read enough books on it YET

Regards

Dave

Jack Leach said:
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
 
J

John W. Vinson

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.

I think the problem here is that a Yes/No field (which you say Active is
downthread) is never NULL: it's either True (-1) or False (0). So your
criterion is never matched and it is as if you hadn't referred to Active at
all.

A criterion of

WHERE [E-MailAddress] IS NOT NULL And [Active]

will work (it isn't necessary to say [Active] = True because it's already
either True or False).
 
B

Blakey300

John

Already sussed that out, but thanks for taking the time to reply anyway

Regards

Dave

John W. Vinson 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.

I think the problem here is that a Yes/No field (which you say Active is
downthread) is never NULL: it's either True (-1) or False (0). So your
criterion is never matched and it is as if you hadn't referred to Active at
all.

A criterion of

WHERE [E-MailAddress] IS NOT NULL And [Active]

will work (it isn't necessary to say [Active] = True because it's already
either True or False).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top