Writing a query (probably using SQL)

  • Thread starter Thread starter Rebecca
  • Start date Start date
R

Rebecca

I think my problem is I don't understand SQL and I think that is what I need
in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or [masterFIRMinfo]![FirmPOBox] Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox]) And
IIf([Member File]![MailCode]="H",[Member File]![Home_Address1],[Member
File]![Home_city], [Member File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF statements like
that is my problem. What I need is both IIF statements combined and produce
the information that I need without running two separate queries (that is
how I handle it now) I run a lot of word merges off of this query.

Thanks in advance!!!!

Rebecca S.
 
If I am reading this right, [MailCode] has to be both "A" AND "H" in order
for the query to select it.
Assuming that there is only one MailCode per record, you might try changing
that to an OR and see if you get better results.

HTH,
 
Rebecca said:
I think my problem is I don't understand SQL and I think that is what
I need in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or
[masterFIRMinfo]![FirmPOBox] Is Null,[masterFIRMinfo]![FirmAddress1]
& " " & [masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox])
And IIf([Member File]![MailCode]="H",[Member
File]![Home_Address1],[Member File]![Home_city], [Member
File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF statements
like that is my problem. What I need is both IIF statements combined
and produce the information that I need without running two separate
queries (that is how I handle it now) I run a lot of word merges off
of this query.

Thanks in advance!!!!

Rebecca S.

Are "Member File" and "masterFIRMinfo" the names of tables (or stored
queries) that are joined in this query you're working on? You should be
using the dot (.) notation, not the bang (!), to qualify the field names
with the table names.

That's not the only problem, though. Although there's nothing wrong
with nesting IIf() functions, I can't make out what you want to have
happen here. It looks like maybe you're using the logical conjunction
"And" in a place where you ought to be using the concatenation operator
"&", but I'm not sure. Could you state in words how you want this
calculated field to be constructed?
 
Rebecca -
If you look at the Help topic for IIF statements, the
format is -
IIF(ConditionToCheck, ResultsIfTrue, ResultsIfFalse)

The comma after the ResultsIfTrue portion tells the IIF
function that what follows is what to return if the
ConditionToCheck evaluates to False.
In your statement, you have '[masterFIRMinfo]![FirmPOBox]'
following the first comma. I'm wondering if what you
really want is
FirmAddress1
FirmAddress2
PO Box

If that's the case, and there are only two values in the
MemberFile!MailCode field (A and H), your IIF statement
could be:

Address: IIf([Member File]![MailCode]="A" Or
[masterFIRMinfo]![FirmPOBox] Is Null,[masterFIRMinfo]!
[FirmAddress1] & Chr(10) & Chr(13) & [masterFIRMinfo]!
[FirmAddress2]& Chr(10) & Chr(13) & [masterFIRMinfo]!
[FirmPOBox],[Member File]![Home_Address1] & Chr(10) & Chr
(13) & [Member File]![Home_city] & Chr(10) & Chr(13) &
[Member File]![Home_State] & Chr(10) & Chr(13) & [Member
File]![Home_Zip])

The Chr(10) & Chr(13) character is for carriage
return/linefeed. If you just wanted a text listing like

FirmAddress1, FirmAddress2, POBox

replace the Chr(10) & Chr(13) code with ", " between the
ampersands (&).

-----Original Message-----
I think my problem is I don't understand SQL and I think that is what I need
in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or
[masterFIRMinfo]![FirmPOBox] Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]! [FirmPOBox]) And
IIf([Member File]![MailCode]="H",[Member File]! [Home_Address1],[Member
File]![Home_city], [Member File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF statements like
that is my problem. What I need is both IIF statements combined and produce
the information that I need without running two separate queries (that is
how I handle it now) I run a lot of word merges off of this query.

Thanks in advance!!!!

Rebecca S.


.
 
I have a Member File Table and a field in the table is MailCode. The
MailCode could have many different letters or even blank. What I need:
If the MailCode is an "A" I need it to pull information from my
masterFIRMinfo Table from the field FirmPOBox, but if the MailCode is blank
I need it to pull from the masterFIRMinfo table from the field
FirmAddress1(and so on). Then if the MailCode has an "H" then I need it to
read from the Member File Table from the field Home_Address (and so on).

This is not written in SQL. It is just written in the design view. I have
never used a dot between the table and field I have always used the ! to
point what field to pull information from. I am self taught starting with
Access 97 and just moving up with upgrades.

Did this make any sense? Right now I get the information I need but I run
two different queries to get it. One does the first IIF statement (a make
table query) and then I run another query that pulls from the new table and
runs the second IIF statement. It does work but since I run merges from the
query it would make my life easier if I could get it into one query.

Rebecca S.


Dirk Goldgar said:
Rebecca said:
I think my problem is I don't understand SQL and I think that is what
I need in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or
[masterFIRMinfo]![FirmPOBox] Is Null,[masterFIRMinfo]![FirmAddress1]
& " " & [masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox])
And IIf([Member File]![MailCode]="H",[Member
File]![Home_Address1],[Member File]![Home_city], [Member
File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF statements
like that is my problem. What I need is both IIF statements combined
and produce the information that I need without running two separate
queries (that is how I handle it now) I run a lot of word merges off
of this query.

Thanks in advance!!!!

Rebecca S.

Are "Member File" and "masterFIRMinfo" the names of tables (or stored
queries) that are joined in this query you're working on? You should be
using the dot (.) notation, not the bang (!), to qualify the field names
with the table names.

That's not the only problem, though. Although there's nothing wrong
with nesting IIf() functions, I can't make out what you want to have
happen here. It looks like maybe you're using the logical conjunction
"And" in a place where you ought to be using the concatenation operator
"&", but I'm not sure. Could you state in words how you want this
calculated field to be constructed?

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
I have tried the OR. The MailCode could be "A", "H" or blank.

Rebecca S.


George Nicholson said:
If I am reading this right, [MailCode] has to be both "A" AND "H" in order
for the query to select it.
Assuming that there is only one MailCode per record, you might try changing
that to an OR and see if you get better results.

HTH,
--
George Nicholson

Remove 'Junk' from return address.


Rebecca said:
I think my problem is I don't understand SQL and I think that is what I need
in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or
[masterFIRMinfo]![FirmPOBox]
Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox]) And
IIf([Member File]![MailCode]="H",[Member File]![Home_Address1],[Member
File]![Home_city], [Member File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF statements like
that is my problem. What I need is both IIF statements combined and produce
the information that I need without running two separate queries (that is
how I handle it now) I run a lot of word merges off of this query.

Thanks in advance!!!!

Rebecca S.
 
I now have this:


Address: IIf([Member File]![MailCode]="H",[Member file]![Home_address1]) &
IIf([Member File]![MailCode]="A" Or [masterFIRMinfo]![FirmPOBox] Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox])

And it finally is showing the "H" address but it also shows the Firm Address
right beside it. What I need is if there is an "H" (in the mailcode) I
need only [Home_address1] and if it is blank the address from
[FirmAddress1] and if there is an "A" or [FirmPOBox] is null, I need the
[FirmAddress1]

If I put an OR in replace of the & before the second IIF, I get a result
of -1 in the field.


I'm not sure if I am any closer to solving this or not.. Any thoughts would
be greatly appreciated!

Rebecca S.
 
whew, this thread was quite a bundle to get thru. please read the following
book <g> and hopefully it will help.

first, let's talk about the IIf() function for a minute. remember that the
IIf() function syntax (in English) is:
IIf(this expression is true, then return this, otherwise return this)
*the "otherwise return this" portion is not directly required, but it's
better not to leave it out of the IIf() function* because an unaddressed
situation will cause the function to return nothing.

a (simple) nested IIf() function syntax is either:

IIf(this expression is true, then IIf(this expression is true, return this,
otherwise return this), otherwise return this)
or
IIf(this expression is true, then return this, otherwise IIf(this expression
is true, return this, otherwise return this))

your code from the previous post says, in more-or-less English:

IIf(the MailCode = "H" is true,
then return the Home_address1,
*otherwise-return-this is left out*)
& concatenate the above return to the return of the following statement:
IIf(the MailCode = "A" is true Or the FirmPOBox Is Null is true,
then return FirmAddress1 & concatenate a space & concatenate
FirmAddress2,
otherwise return FirmPOBox.)

reading it as more-or-less English, you can see why you're getting the
results you stated below. your IIf statements and explanations in previous
posts seem somewhat contradictory and incomplete. let me see if i can
correctly state what you're actually trying to do:

if the mail code is "A":
return the firm's PO Box
but if the PO Box is blank, return the firm's Address1 and a space
and Address2
if the mail code is "H":
return the Home Address
but if the Home Address is blank, return the firm's Address1 and a
space and Address2
if the mail code is blank:
follow the same instructions as when the mail code is "A"

is that correct? if *not*, post corrections. if it is correct, try this:

IIf([Member File]![MailCode] = "A" Or [Member File]![MailCode] Is Null,
IIf([masterFIRMinfo]![FirmPOBox] Is Null, [masterFIRMinfo]![FirmAddress1] &
" " & [masterFIRMinfo]![FirmAddress2], [masterFIRMinfo]![FirmPOBox]),
IIf([Member File]![MailCode] = "B", IIf([Member File]![Home_Address1] Is
Null, [masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2], [Member File]![Home_Address1])))

in more-or-less English:

IIf(the MailCode = "A" is true Or the Mail Code Is Null is true,
then IIf( the FirmPOBox Is Null,
then return FirmAddress1 and " " and FirmAddress2,
otherwise return FirmPOBox)
otherwise IIf(the MailCode = "B",
then IIf(the Home_Address1 Is Null,
then return FirmAddress1 and " " and
FirmAddress2,
otherwise return Home_Address1),
*otherwise-do-this* is left out)))

make sure you include all three parens ))) at the end.

you don't seem to address instances where the mail code is neither A nor B
nor blank. (that's why the final "otherwise-do-this" is left out of the code
above.) are A, B and Null the only possible values in the mail code field?
if there are other possible values, you need to include the "otherwise, do
this" portion of the IIf() function to handle those values.

that's the nested-IIf-statement-from-hell, for sure, but i couldn't come up
with a more succinct expression. i would probably write a VBA function to
process it all, passing all the necessary field parameters to the function.
it would certainly be easier to write, debug and maintain in VBA.

hth


Rebecca said:
I now have this:


Address: IIf([Member File]![MailCode]="H",[Member file]![Home_address1]) &
IIf([Member File]![MailCode]="A" Or [masterFIRMinfo]![FirmPOBox] Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox])

And it finally is showing the "H" address but it also shows the Firm Address
right beside it. What I need is if there is an "H" (in the mailcode) I
need only [Home_address1] and if it is blank the address from
[FirmAddress1] and if there is an "A" or [FirmPOBox] is null, I need the
[FirmAddress1]

If I put an OR in replace of the & before the second IIF, I get a result
of -1 in the field.


I'm not sure if I am any closer to solving this or not.. Any thoughts would
be greatly appreciated!

Rebecca S.


Rebecca said:
I think my problem is I don't understand SQL and I think that is what I need
in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or
[masterFIRMinfo]![FirmPOBox]
Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox]) And
IIf([Member File]![MailCode]="H",[Member File]![Home_Address1],[Member
File]![Home_city], [Member File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF statements like
that is my problem. What I need is both IIF statements combined and produce
the information that I need without running two separate queries (that is
how I handle it now) I run a lot of word merges off of this query.

Thanks in advance!!!!

Rebecca S.
 
IIF does not recognize Is Null
you have to use the IsNull Function instead
ie
IsNull([masterFIRMinfo]![FirmPOBox])
instead of
[masterFIRMinfo]![FirmPOBox] Is Null

HTH

Pieter
 
IIf([Member File]![MailCode]="H",[Member file]![Home_address1]) ,
IIf([Member File]![MailCode]="A" And IsNull([masterFIRMinfo]![FirmPOBox]),
[masterFIRMinfo]![FirmAddress1] & (" " +
[masterFIRMinfo]![FirmAddress2]),
[masterFIRMinfo]![FirmPOBox])

but I wonder about the ! (if we're talking about table.field - use . )
Note that the space will dissapear after FrmAddress1 if no Address2 - due to
the use of + ...

HTH
Pieter

Rebecca said:
I now have this:


Address: IIf([Member File]![MailCode]="H",[Member file]![Home_address1]) &
IIf([Member File]![MailCode]="A" Or [masterFIRMinfo]![FirmPOBox] Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox])

And it finally is showing the "H" address but it also shows the Firm Address
right beside it. What I need is if there is an "H" (in the mailcode) I
need only [Home_address1] and if it is blank the address from
[FirmAddress1] and if there is an "A" or [FirmPOBox] is null, I need the
[FirmAddress1]

If I put an OR in replace of the & before the second IIF, I get a result
of -1 in the field.


I'm not sure if I am any closer to solving this or not.. Any thoughts would
be greatly appreciated!

Rebecca S.


Rebecca said:
I think my problem is I don't understand SQL and I think that is what I need
in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or
[masterFIRMinfo]![FirmPOBox]
Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox]) And
IIf([Member File]![MailCode]="H",[Member File]![Home_Address1],[Member
File]![Home_city], [Member File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF statements like
that is my problem. What I need is both IIF statements combined and produce
the information that I need without running two separate queries (that is
how I handle it now) I run a lot of word merges off of this query.

Thanks in advance!!!!

Rebecca S.
 
hmm, that's interesting. i tested an IIf() function in a query just now,
using Is Null, and it worked for me. tested in A2000 running on Win2kPro,
though i have to say i've used it previously in A2000, A97 and in A95 too
(if i recall correctly that far back). i'd never used the IsNull() function
until i learned VBA a few years ago. have you had trouble with Is Null in
queries, or expressions, or....?


Pieter Wijnen said:
IIF does not recognize Is Null
you have to use the IsNull Function instead
ie
IsNull([masterFIRMinfo]![FirmPOBox])
instead of
[masterFIRMinfo]![FirmPOBox] Is Null

HTH

Pieter

Rebecca said:
I think my problem is I don't understand SQL and I think that is what I need
in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or
[masterFIRMinfo]![FirmPOBox]
Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox]) And
IIf([Member File]![MailCode]="H",[Member File]![Home_Address1],[Member
File]![Home_city], [Member File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF statements like
that is my problem. What I need is both IIF statements combined and produce
the information that I need without running two separate queries (that is
how I handle it now) I run a lot of word merges off of this query.

Thanks in advance!!!!

Rebecca S.
 
additional info, rebecca.
i tested both bang and dot in a simple IIf() function in a query, and both
worked fine - even when i used bang for one reference and dot for another,
in the same IIf() function.
i also tested using the function without table references at all, only field
references. that also worked, because both tables were in the query and
there were no duplicate field names between the two.
so here's a "stripped-down" version of the nested IIf() function i
previously posted. if both table Member File and table masterFIRMinfo are in
your query, suggest you try this one first (checking the spelling of field
names, of course). if it doesn't work, try the "full" version (again,
checking spelling of table and field names).

IIf([MailCode] = "A" Or [MailCode] Is Null, IIf([FirmPOBox] Is Null,
[FirmAddress1] & " " & [FirmAddress2], [FirmPOBox]), IIf([MailCode] = "B",
IIf([Home_Address1] Is Null, [FirmAddress1] & " " & [FirmAddress2],
[Home_Address1])))

hth


tina said:
whew, this thread was quite a bundle to get thru. please read the following
book <g> and hopefully it will help.

first, let's talk about the IIf() function for a minute. remember that the
IIf() function syntax (in English) is:
IIf(this expression is true, then return this, otherwise return this)
*the "otherwise return this" portion is not directly required, but it's
better not to leave it out of the IIf() function* because an unaddressed
situation will cause the function to return nothing.

a (simple) nested IIf() function syntax is either:

IIf(this expression is true, then IIf(this expression is true, return this,
otherwise return this), otherwise return this)
or
IIf(this expression is true, then return this, otherwise IIf(this expression
is true, return this, otherwise return this))

your code from the previous post says, in more-or-less English:

IIf(the MailCode = "H" is true,
then return the Home_address1,
*otherwise-return-this is left out*)
& concatenate the above return to the return of the following statement:
IIf(the MailCode = "A" is true Or the FirmPOBox Is Null is true,
then return FirmAddress1 & concatenate a space & concatenate
FirmAddress2,
otherwise return FirmPOBox.)

reading it as more-or-less English, you can see why you're getting the
results you stated below. your IIf statements and explanations in previous
posts seem somewhat contradictory and incomplete. let me see if i can
correctly state what you're actually trying to do:

if the mail code is "A":
return the firm's PO Box
but if the PO Box is blank, return the firm's Address1 and a space
and Address2
if the mail code is "H":
return the Home Address
but if the Home Address is blank, return the firm's Address1 and a
space and Address2
if the mail code is blank:
follow the same instructions as when the mail code is "A"

is that correct? if *not*, post corrections. if it is correct, try this:

IIf([Member File]![MailCode] = "A" Or [Member File]![MailCode] Is Null,
IIf([masterFIRMinfo]![FirmPOBox] Is Null, [masterFIRMinfo]![FirmAddress1] &
" " & [masterFIRMinfo]![FirmAddress2], [masterFIRMinfo]![FirmPOBox]),
IIf([Member File]![MailCode] = "B", IIf([Member File]![Home_Address1] Is
Null, [masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2], [Member File]![Home_Address1])))

in more-or-less English:

IIf(the MailCode = "A" is true Or the Mail Code Is Null is true,
then IIf( the FirmPOBox Is Null,
then return FirmAddress1 and " " and FirmAddress2,
otherwise return FirmPOBox)
otherwise IIf(the MailCode = "B",
then IIf(the Home_Address1 Is Null,
then return FirmAddress1 and " " and
FirmAddress2,
otherwise return Home_Address1),
*otherwise-do-this* is left out)))

make sure you include all three parens ))) at the end.

you don't seem to address instances where the mail code is neither A nor B
nor blank. (that's why the final "otherwise-do-this" is left out of the code
above.) are A, B and Null the only possible values in the mail code field?
if there are other possible values, you need to include the "otherwise, do
this" portion of the IIf() function to handle those values.

that's the nested-IIf-statement-from-hell, for sure, but i couldn't come up
with a more succinct expression. i would probably write a VBA function to
process it all, passing all the necessary field parameters to the function.
it would certainly be easier to write, debug and maintain in VBA.

hth


Rebecca said:
I now have this:


Address: IIf([Member File]![MailCode]="H",[Member file]![Home_address1]) &
IIf([Member File]![MailCode]="A" Or [masterFIRMinfo]![FirmPOBox] Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox])

And it finally is showing the "H" address but it also shows the Firm Address
right beside it. What I need is if there is an "H" (in the mailcode) I
need only [Home_address1] and if it is blank the address from
[FirmAddress1] and if there is an "A" or [FirmPOBox] is null, I need the
[FirmAddress1]

If I put an OR in replace of the & before the second IIF, I get a result
of -1 in the field.


I'm not sure if I am any closer to solving this or not.. Any thoughts would
be greatly appreciated!

Rebecca S.


Rebecca said:
I think my problem is I don't understand SQL and I think that is what
I
need
in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or
[masterFIRMinfo]![FirmPOBox]
Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox]) And
IIf([Member File]![MailCode]="H",[Member File]![Home_Address1],[Member
File]![Home_city], [Member File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF statements like
that is my problem. What I need is both IIF statements combined and produce
the information that I need without running two separate queries (that is
how I handle it now) I run a lot of word merges off of this query.

Thanks in advance!!!!

Rebecca S.
 
tina-
First off I want to thank you for taking so much time and explaining
everything to me!!! and then to write the query and check to see if it
works.. wow!! way beyond what I expected..

I had to use the full version you initially wrote because I do have a
"mailcode" in both the member file and masterfirminfo. It seems to work
great! Now I have to write the rest of it, adding in the rest of the
address (city state zip) which I think I can figure out with all the
information you gave me.

I am having a new problem, I am going to post it as a separate question on
the newsgroup but thought I would mention it here since you have been so
helpful.. My database is closing on me.. and not saving the changes I make
to my query. For example, I go to the query, make the changes you gave me,
hit the ! to run the query, get the results I wanted, click the X to close
the query, and it closes my whole database!!!! So when I open the database
again, I go to the same query and the changes were not saved. I do it
again, but instead of hitting the X to close, I hit the little disk in the
toolbar to save, and it closes the database. I open again.. and it was not
saved.. any ideas what could have happend to my database? I did a back up
and then compacted it twice. re-opened the database and opened a few
different querys and forms and closed them with the X.. no problem. But when
I go and make the change to the query and hit the X,,,, database
closes....????

Again! THANKS SO MUCH for all the help you provided with my last question.
Like I said.. it is working.. but I can't get it to save.. :).. computers!..
(or me...) which ever.. something isn't working right.

Rebecca S.


tina said:
additional info, rebecca.
i tested both bang and dot in a simple IIf() function in a query, and both
worked fine - even when i used bang for one reference and dot for another,
in the same IIf() function.
i also tested using the function without table references at all, only field
references. that also worked, because both tables were in the query and
there were no duplicate field names between the two.
so here's a "stripped-down" version of the nested IIf() function i
previously posted. if both table Member File and table masterFIRMinfo are in
your query, suggest you try this one first (checking the spelling of field
names, of course). if it doesn't work, try the "full" version (again,
checking spelling of table and field names).

IIf([MailCode] = "A" Or [MailCode] Is Null, IIf([FirmPOBox] Is Null,
[FirmAddress1] & " " & [FirmAddress2], [FirmPOBox]), IIf([MailCode] = "B",
IIf([Home_Address1] Is Null, [FirmAddress1] & " " & [FirmAddress2],
[Home_Address1])))

hth


tina said:
whew, this thread was quite a bundle to get thru. please read the following
book <g> and hopefully it will help.

first, let's talk about the IIf() function for a minute. remember that the
IIf() function syntax (in English) is:
IIf(this expression is true, then return this, otherwise return this)
*the "otherwise return this" portion is not directly required, but it's
better not to leave it out of the IIf() function* because an unaddressed
situation will cause the function to return nothing.

a (simple) nested IIf() function syntax is either:

IIf(this expression is true, then IIf(this expression is true, return this,
otherwise return this), otherwise return this)
or
IIf(this expression is true, then return this, otherwise IIf(this expression
is true, return this, otherwise return this))

your code from the previous post says, in more-or-less English:

IIf(the MailCode = "H" is true,
then return the Home_address1,
*otherwise-return-this is left out*)
& concatenate the above return to the return of the following statement:
IIf(the MailCode = "A" is true Or the FirmPOBox Is Null is true,
then return FirmAddress1 & concatenate a space & concatenate
FirmAddress2,
otherwise return FirmPOBox.)

reading it as more-or-less English, you can see why you're getting the
results you stated below. your IIf statements and explanations in previous
posts seem somewhat contradictory and incomplete. let me see if i can
correctly state what you're actually trying to do:

if the mail code is "A":
return the firm's PO Box
but if the PO Box is blank, return the firm's Address1 and a space
and Address2
if the mail code is "H":
return the Home Address
but if the Home Address is blank, return the firm's Address1 and a
space and Address2
if the mail code is blank:
follow the same instructions as when the mail code is "A"

is that correct? if *not*, post corrections. if it is correct, try this:

IIf([Member File]![MailCode] = "A" Or [Member File]![MailCode] Is Null,
IIf([masterFIRMinfo]![FirmPOBox] Is Null,
[masterFIRMinfo]![FirmAddress1]
&
" " & [masterFIRMinfo]![FirmAddress2], [masterFIRMinfo]![FirmPOBox]),
IIf([Member File]![MailCode] = "B", IIf([Member File]![Home_Address1] Is
Null, [masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2], [Member File]![Home_Address1])))

in more-or-less English:

IIf(the MailCode = "A" is true Or the Mail Code Is Null is true,
then IIf( the FirmPOBox Is Null,
then return FirmAddress1 and " " and FirmAddress2,
otherwise return FirmPOBox)
otherwise IIf(the MailCode = "B",
then IIf(the Home_Address1 Is Null,
then return FirmAddress1 and " " and
FirmAddress2,
otherwise return Home_Address1),
*otherwise-do-this* is left out)))

make sure you include all three parens ))) at the end.

you don't seem to address instances where the mail code is neither A nor B
nor blank. (that's why the final "otherwise-do-this" is left out of the code
above.) are A, B and Null the only possible values in the mail code field?
if there are other possible values, you need to include the "otherwise, do
this" portion of the IIf() function to handle those values.

that's the nested-IIf-statement-from-hell, for sure, but i couldn't come up
with a more succinct expression. i would probably write a VBA function to
process it all, passing all the necessary field parameters to the function.
it would certainly be easier to write, debug and maintain in VBA.

hth


Rebecca said:
I now have this:


Address: IIf([Member File]![MailCode]="H",[Member
file]![Home_address1])
&
IIf([Member File]![MailCode]="A" Or [masterFIRMinfo]![FirmPOBox] Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox])

And it finally is showing the "H" address but it also shows the Firm Address
right beside it. What I need is if there is an "H" (in the mailcode) I
need only [Home_address1] and if it is blank the address from
[FirmAddress1] and if there is an "A" or [FirmPOBox] is null, I need the
[FirmAddress1]

If I put an OR in replace of the & before the second IIF, I get a result
of -1 in the field.


I'm not sure if I am any closer to solving this or not.. Any thoughts would
be greatly appreciated!

Rebecca S.


I think my problem is I don't understand SQL and I think that is
what
I
need
in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or [masterFIRMinfo]![FirmPOBox]
Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox]) And
IIf([Member File]![MailCode]="H",[Member File]![Home_Address1],[Member
File]![Home_city], [Member File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF
statements
like
that is my problem. What I need is both IIF statements combined and
produce
the information that I need without running two separate queries
(that
is
how I handle it now) I run a lot of word merges off of this query.

Thanks in advance!!!!

Rebecca S.
 
Pieter Wijnen said:
IIF does not recognize Is Null
you have to use the IsNull Function instead
ie
IsNull([masterFIRMinfo]![FirmPOBox])
instead of
[masterFIRMinfo]![FirmPOBox] Is Null

The SQL IIf() function does recognize "Is Null". The VBA IIF() function
doesn't. So you can use "IIf([SomeField] Is Null, ...)" in a query, but
not in VBA code.
 
glad it worked (running, that is), glad to help. btw, just as a side note, i
believe you could *not* include any table references *except* when you refer
to the mailcode field which is in both tables. also, i'd love to take the
credit, but...i didn't duplicate your table setup and test the nested IIf()
function i gave you. i only wrote two three-field tables in order to write a
query with a simple IIf() function, for the testing i mentioned in my last
post. so i'm not quite as wonderful as you think. <g>
re the "closing db, closing not saving query" problem. good idea to post a
new thread. suggest you first try writing in the nested IIf() function and
then saving the query *before you run it*. it's possible the function is too
complex and is causing problems, though that would usually cause some kind
of error *while it's running* not after. it's also possible that the query
object is corrupted, even the db object may be corrupted. next suggest you
try deleting the query and rewriting it. <cringe> or to be safe, open a new
blank db, import everything *except* that query, and write a new one -
that's probably what i'd do if the first suggestion didn'n work. but a new
thread will put much better minds (probably with better solutions) to work
on the problem.
good luck! :)


Rebecca said:
tina-
First off I want to thank you for taking so much time and explaining
everything to me!!! and then to write the query and check to see if it
works.. wow!! way beyond what I expected..

I had to use the full version you initially wrote because I do have a
"mailcode" in both the member file and masterfirminfo. It seems to work
great! Now I have to write the rest of it, adding in the rest of the
address (city state zip) which I think I can figure out with all the
information you gave me.

I am having a new problem, I am going to post it as a separate question on
the newsgroup but thought I would mention it here since you have been so
helpful.. My database is closing on me.. and not saving the changes I make
to my query. For example, I go to the query, make the changes you gave me,
hit the ! to run the query, get the results I wanted, click the X to close
the query, and it closes my whole database!!!! So when I open the database
again, I go to the same query and the changes were not saved. I do it
again, but instead of hitting the X to close, I hit the little disk in the
toolbar to save, and it closes the database. I open again.. and it was not
saved.. any ideas what could have happend to my database? I did a back up
and then compacted it twice. re-opened the database and opened a few
different querys and forms and closed them with the X.. no problem. But when
I go and make the change to the query and hit the X,,,, database
closes....????

Again! THANKS SO MUCH for all the help you provided with my last question.
Like I said.. it is working.. but I can't get it to save.. :).. computers!..
(or me...) which ever.. something isn't working right.

Rebecca S.


tina said:
additional info, rebecca.
i tested both bang and dot in a simple IIf() function in a query, and both
worked fine - even when i used bang for one reference and dot for another,
in the same IIf() function.
i also tested using the function without table references at all, only field
references. that also worked, because both tables were in the query and
there were no duplicate field names between the two.
so here's a "stripped-down" version of the nested IIf() function i
previously posted. if both table Member File and table masterFIRMinfo
are
in
your query, suggest you try this one first (checking the spelling of field
names, of course). if it doesn't work, try the "full" version (again,
checking spelling of table and field names).

IIf([MailCode] = "A" Or [MailCode] Is Null, IIf([FirmPOBox] Is Null,
[FirmAddress1] & " " & [FirmAddress2], [FirmPOBox]), IIf([MailCode] = "B",
IIf([Home_Address1] Is Null, [FirmAddress1] & " " & [FirmAddress2],
[Home_Address1])))

hth


tina said:
whew, this thread was quite a bundle to get thru. please read the following
book <g> and hopefully it will help.

first, let's talk about the IIf() function for a minute. remember that the
IIf() function syntax (in English) is:
IIf(this expression is true, then return this, otherwise return this)
*the "otherwise return this" portion is not directly required, but it's
better not to leave it out of the IIf() function* because an unaddressed
situation will cause the function to return nothing.

a (simple) nested IIf() function syntax is either:

IIf(this expression is true, then IIf(this expression is true, return this,
otherwise return this), otherwise return this)
or
IIf(this expression is true, then return this, otherwise IIf(this expression
is true, return this, otherwise return this))

your code from the previous post says, in more-or-less English:

IIf(the MailCode = "H" is true,
then return the Home_address1,
*otherwise-return-this is left out*)
& concatenate the above return to the return of the following statement:
IIf(the MailCode = "A" is true Or the FirmPOBox Is Null is true,
then return FirmAddress1 & concatenate a space & concatenate
FirmAddress2,
otherwise return FirmPOBox.)

reading it as more-or-less English, you can see why you're getting the
results you stated below. your IIf statements and explanations in previous
posts seem somewhat contradictory and incomplete. let me see if i can
correctly state what you're actually trying to do:

if the mail code is "A":
return the firm's PO Box
but if the PO Box is blank, return the firm's Address1 and a space
and Address2
if the mail code is "H":
return the Home Address
but if the Home Address is blank, return the firm's Address1
and
a
space and Address2
if the mail code is blank:
follow the same instructions as when the mail code is "A"

is that correct? if *not*, post corrections. if it is correct, try this:

IIf([Member File]![MailCode] = "A" Or [Member File]![MailCode] Is Null,
IIf([masterFIRMinfo]![FirmPOBox] Is Null,
[masterFIRMinfo]![FirmAddress1]
&
" " & [masterFIRMinfo]![FirmAddress2], [masterFIRMinfo]![FirmPOBox]),
IIf([Member File]![MailCode] = "B", IIf([Member File]![Home_Address1] Is
Null, [masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2], [Member File]![Home_Address1])))

in more-or-less English:

IIf(the MailCode = "A" is true Or the Mail Code Is Null is true,
then IIf( the FirmPOBox Is Null,
then return FirmAddress1 and " " and FirmAddress2,
otherwise return FirmPOBox)
otherwise IIf(the MailCode = "B",
then IIf(the Home_Address1 Is Null,
then return FirmAddress1 and " " and
FirmAddress2,
otherwise return Home_Address1),
*otherwise-do-this* is left out)))

make sure you include all three parens ))) at the end.

you don't seem to address instances where the mail code is neither A
nor
B the
code
"otherwise,
do
this" portion of the IIf() function to handle those values.

that's the nested-IIf-statement-from-hell, for sure, but i couldn't
come
up
with a more succinct expression. i would probably write a VBA function to
process it all, passing all the necessary field parameters to the function.
it would certainly be easier to write, debug and maintain in VBA.

hth


I now have this:


Address: IIf([Member File]![MailCode]="H",[Member
file]![Home_address1])
&
IIf([Member File]![MailCode]="A" Or [masterFIRMinfo]![FirmPOBox] Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox])

And it finally is showing the "H" address but it also shows the Firm
Address
right beside it. What I need is if there is an "H" (in the
mailcode)
I
need only [Home_address1] and if it is blank the address from
[FirmAddress1] and if there is an "A" or [FirmPOBox] is null, I need the
[FirmAddress1]

If I put an OR in replace of the & before the second IIF, I get a result
of -1 in the field.


I'm not sure if I am any closer to solving this or not.. Any thoughts
would
be greatly appreciated!

Rebecca S.


I think my problem is I don't understand SQL and I think that is
what
I
need
in order for me to get the results I need in my query.

This is what I have in the design view, field row: (running Access XP)

Address: IIf([Member File]![MailCode]="A" Or
[masterFIRMinfo]![FirmPOBox]
Is
Null,[masterFIRMinfo]![FirmAddress1] & " " &
[masterFIRMinfo]![FirmAddress2],[masterFIRMinfo]![FirmPOBox]) And
IIf([Member File]![MailCode]="H",[Member File]![Home_Address1],[Member
File]![Home_city], [Member File]![Home_State], [Member File]![Home_Zip])


Of course it doesn't work. My guess is having the two IIF statements
like
that is my problem. What I need is both IIF statements combined and
produce
the information that I need without running two separate queries (that
is
how I handle it now) I run a lot of word merges off of this query.

Thanks in advance!!!!

Rebecca S.
 
okay, that explains it. i wondered if i was hallucinating. btw, when you
mention the IIf() function in VBA, are you talking about the
If...Then...Else statement? or can you use the IIf() function directly in
VBA? in an Eval() function, maybe? (there's still lots, and lots more, i
don't know about VBA.)


Dirk Goldgar said:
Pieter Wijnen said:
IIF does not recognize Is Null
you have to use the IsNull Function instead
ie
IsNull([masterFIRMinfo]![FirmPOBox])
instead of
[masterFIRMinfo]![FirmPOBox] Is Null

The SQL IIf() function does recognize "Is Null". The VBA IIF() function
doesn't. So you can use "IIf([SomeField] Is Null, ...)" in a query, but
not in VBA code.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
tina said:
okay, that explains it. i wondered if i was hallucinating. btw, when
you mention the IIf() function in VBA, are you talking about the
If...Then...Else statement? or can you use the IIf() function
directly in VBA? in an Eval() function, maybe? (there's still lots,
and lots more, i don't know about VBA.)

No, I'm not talking about the "If ... Then ... Else" statements. There
is a VBA function named IIf(), and there is a builtin function in Jet
SQL named IIf(). They are almost, but not quite, identical. The main
differences are:

1. The SQL function understands "Is Null"; the VBA function doesn't.

2. The VBA function always evaluates both alternative result expressions
(which can raise an error if, for example, one of the expressions
divides by zero -- even if that result is not returned), while the SQL
function only evaluates the result expression that will be returned.

You can use the VBA IIf() function directly in VBA, though I always
prefer a regular If ... Then ... Else construct, as it's more efficient
and not subject to the problem noted in item 2, above. The difference
between the two functions is obscured because VBA functions can be used
in Access queries, but when you use IIf() in a Jet query it's the Jet
SQL function that is called.
 
that's all an eye-opener, thanks Dirk! :)


Dirk Goldgar said:
No, I'm not talking about the "If ... Then ... Else" statements. There
is a VBA function named IIf(), and there is a builtin function in Jet
SQL named IIf(). They are almost, but not quite, identical. The main
differences are:

1. The SQL function understands "Is Null"; the VBA function doesn't.

2. The VBA function always evaluates both alternative result expressions
(which can raise an error if, for example, one of the expressions
divides by zero -- even if that result is not returned), while the SQL
function only evaluates the result expression that will be returned.

You can use the VBA IIf() function directly in VBA, though I always
prefer a regular If ... Then ... Else construct, as it's more efficient
and not subject to the problem noted in item 2, above. The difference
between the two functions is obscured because VBA functions can be used
in Access queries, but when you use IIf() in a Jet query it's the Jet
SQL function that is called.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Back
Top