Help with IIf statement

T

Tony Williams

I thought I had cracked if statements but I'm getting a syntax error with
this
= IIf ( [tblInstitution1]![txtInstitution] isNull,"
[tblInstitution1]![txtInitial]" & " " & "[tblInstitution1]![txtlastname]" ,
"[tblInstitution1]![txtInstitution]" )
I'm using it as a control on a report. can anyone tell me what's wrong with
this?
Thanks
Tony
 
K

Keith Wilby

Tony Williams said:
I thought I had cracked if statements but I'm getting a syntax error with
this
= IIf ( [tblInstitution1]![txtInstitution] isNull,"
[tblInstitution1]![txtInitial]" & " " & "[tblInstitution1]![txtlastname]"
, "[tblInstitution1]![txtInstitution]" )
I'm using it as a control on a report. can anyone tell me what's wrong
with this?
Thanks
Tony
The "txt" prefixes suggest you're referring to text boxes - try:

= IIf (IsNull(Me.txtInstitution),Me.txtInitial & " " &
Me.txtlastname,Me.txtInstitution)

Keith.
www.keithwilby.com
 
B

BruceM

Is tblInstitution1 the report's record source? If so, there is no need to
reference it. If txtInstitution comes from a form or some other object, you
need to name the object type (table, form, etc.):
[Forms]![frmYourForm]![YourField]. You don't put quotes around the fields;
doing so causes the literal text to appear. Also, IsNull is placed
elsewhere, and needs its own parentheses:

IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])
 
T

Tony Williams

Thanks Bruce the reports source is a query but I've added the extra fields
to the query. I understand now the relationship between the IIf and IsNull
and having done all that it works fine. Thanks for pointing me in the right
direction.
Tony
BruceM said:
Is tblInstitution1 the report's record source? If so, there is no need to
reference it. If txtInstitution comes from a form or some other object,
you need to name the object type (table, form, etc.):
[Forms]![frmYourForm]![YourField]. You don't put quotes around the
fields; doing so causes the literal text to appear. Also, IsNull is
placed elsewhere, and needs its own parentheses:

IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])

Tony Williams said:
I thought I had cracked if statements but I'm getting a syntax error with
this
= IIf ( [tblInstitution1]![txtInstitution] isNull,"
[tblInstitution1]![txtInitial]" & " " & "[tblInstitution1]![txtlastname]"
, "[tblInstitution1]![txtInstitution]" )
I'm using it as a control on a report. can anyone tell me what's wrong
with this?
Thanks
Tony
 
T

Tony Williams

Bruce it worked on one report and not the others? They are all based on
queries and I've added the extra fields initial and lastname to the queries
but I'm getting#Error. Any ideas? Real odd!
Thanks
Tony
BruceM said:
Is tblInstitution1 the report's record source? If so, there is no need to
reference it. If txtInstitution comes from a form or some other object,
you need to name the object type (table, form, etc.):
[Forms]![frmYourForm]![YourField]. You don't put quotes around the
fields; doing so causes the literal text to appear. Also, IsNull is
placed elsewhere, and needs its own parentheses:

IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])

Tony Williams said:
I thought I had cracked if statements but I'm getting a syntax error with
this
= IIf ( [tblInstitution1]![txtInstitution] isNull,"
[tblInstitution1]![txtInitial]" & " " & "[tblInstitution1]![txtlastname]"
, "[tblInstitution1]![txtInstitution]" )
I'm using it as a control on a report. can anyone tell me what's wrong
with this?
Thanks
Tony
 
B

BruceM

What is the expression that generates the #Error indication?

Tony Williams said:
Bruce it worked on one report and not the others? They are all based on
queries and I've added the extra fields initial and lastname to the
queries but I'm getting#Error. Any ideas? Real odd!
Thanks
Tony
BruceM said:
Is tblInstitution1 the report's record source? If so, there is no need
to reference it. If txtInstitution comes from a form or some other
object, you need to name the object type (table, form, etc.):
[Forms]![frmYourForm]![YourField]. You don't put quotes around the
fields; doing so causes the literal text to appear. Also, IsNull is
placed elsewhere, and needs its own parentheses:

IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])

Tony Williams said:
I thought I had cracked if statements but I'm getting a syntax error with
this
= IIf ( [tblInstitution1]![txtInstitution] isNull,"
[tblInstitution1]![txtInitial]" & " " &
"[tblInstitution1]![txtlastname]" ,
"[tblInstitution1]![txtInstitution]" )
I'm using it as a control on a report. can anyone tell me what's wrong
with this?
Thanks
Tony
 
T

Tony Williams

This is what I have as the source of the control
=IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])
and this is the sql of the query
SELECT tblInstitution1.txtRefNbr, tblInstitution1.txtInstitution,
tblInstitution1.txtInitial, tblInstitution1.txtlastname,
tblInstitution1.txtCountry
FROM tblInstitution1
WHERE (((tblInstitution1.txtCountry)<>"UK"));

This is the same sorce statment that I have in my other report but it wont
work in my three others the above being a typical example??????
Thanks Bruce
Tony
BruceM said:
What is the expression that generates the #Error indication?

Tony Williams said:
Bruce it worked on one report and not the others? They are all based on
queries and I've added the extra fields initial and lastname to the
queries but I'm getting#Error. Any ideas? Real odd!
Thanks
Tony
BruceM said:
Is tblInstitution1 the report's record source? If so, there is no need
to reference it. If txtInstitution comes from a form or some other
object, you need to name the object type (table, form, etc.):
[Forms]![frmYourForm]![YourField]. You don't put quotes around the
fields; doing so causes the literal text to appear. Also, IsNull is
placed elsewhere, and needs its own parentheses:

IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])

I thought I had cracked if statements but I'm getting a syntax error
with this
= IIf ( [tblInstitution1]![txtInstitution] isNull,"
[tblInstitution1]![txtInitial]" & " " &
"[tblInstitution1]![txtlastname]" ,
"[tblInstitution1]![txtInstitution]" )
I'm using it as a control on a report. can anyone tell me what's wrong
with this?
Thanks
Tony
 
B

BruceM

Assuming the query works on its own (i.e. it returns the expected results in
datasheet view), do you have any text boxes that have the same names as the
fields? That can cause the #Error problem.
Since you have a query you can use the IIf expression in a new query field.
In query design view, at the top of an empty column, do something like:
InitialName: IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])
Then you can set the control source of the text box to InitialName (or
whatever you decide to call the query field).
In any case, you should check for name duplication. When you drag a field
from the field list onto a report it gives the control the same name as the
field. This was not one of Microsoft's better choices with Access. Even if
you use a query as I have described, duplicate names for fields and controls
can cause problems in other areas.

Tony Williams said:
This is what I have as the source of the control
=IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])
and this is the sql of the query
SELECT tblInstitution1.txtRefNbr, tblInstitution1.txtInstitution,
tblInstitution1.txtInitial, tblInstitution1.txtlastname,
tblInstitution1.txtCountry
FROM tblInstitution1
WHERE (((tblInstitution1.txtCountry)<>"UK"));

This is the same sorce statment that I have in my other report but it wont
work in my three others the above being a typical example??????
Thanks Bruce
Tony
BruceM said:
What is the expression that generates the #Error indication?

Tony Williams said:
Bruce it worked on one report and not the others? They are all based on
queries and I've added the extra fields initial and lastname to the
queries but I'm getting#Error. Any ideas? Real odd!
Thanks
Tony
Is tblInstitution1 the report's record source? If so, there is no need
to reference it. If txtInstitution comes from a form or some other
object, you need to name the object type (table, form, etc.):
[Forms]![frmYourForm]![YourField]. You don't put quotes around the
fields; doing so causes the literal text to appear. Also, IsNull is
placed elsewhere, and needs its own parentheses:

IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])

I thought I had cracked if statements but I'm getting a syntax error
with this
= IIf ( [tblInstitution1]![txtInstitution] isNull,"
[tblInstitution1]![txtInitial]" & " " &
"[tblInstitution1]![txtlastname]" ,
"[tblInstitution1]![txtInstitution]" )
I'm using it as a control on a report. can anyone tell me what's wrong
with this?
Thanks
Tony
 
T

Tony Williams

Thanks Bruce I used your second option and it worked just fine. Thanks for
sticking with me, much appreciated.
Tony
BruceM said:
Assuming the query works on its own (i.e. it returns the expected results
in datasheet view), do you have any text boxes that have the same names as
the fields? That can cause the #Error problem.
Since you have a query you can use the IIf expression in a new query
field. In query design view, at the top of an empty column, do something
like:
InitialName: IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])
Then you can set the control source of the text box to InitialName (or
whatever you decide to call the query field).
In any case, you should check for name duplication. When you drag a field
from the field list onto a report it gives the control the same name as
the field. This was not one of Microsoft's better choices with Access.
Even if you use a query as I have described, duplicate names for fields
and controls can cause problems in other areas.

Tony Williams said:
This is what I have as the source of the control
=IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])
and this is the sql of the query
SELECT tblInstitution1.txtRefNbr, tblInstitution1.txtInstitution,
tblInstitution1.txtInitial, tblInstitution1.txtlastname,
tblInstitution1.txtCountry
FROM tblInstitution1
WHERE (((tblInstitution1.txtCountry)<>"UK"));

This is the same sorce statment that I have in my other report but it
wont work in my three others the above being a typical example??????
Thanks Bruce
Tony
BruceM said:
What is the expression that generates the #Error indication?

Bruce it worked on one report and not the others? They are all based on
queries and I've added the extra fields initial and lastname to the
queries but I'm getting#Error. Any ideas? Real odd!
Thanks
Tony
Is tblInstitution1 the report's record source? If so, there is no
need to reference it. If txtInstitution comes from a form or some
other object, you need to name the object type (table, form, etc.):
[Forms]![frmYourForm]![YourField]. You don't put quotes around the
fields; doing so causes the literal text to appear. Also, IsNull is
placed elsewhere, and needs its own parentheses:

IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])

I thought I had cracked if statements but I'm getting a syntax error
with this
= IIf ( [tblInstitution1]![txtInstitution] isNull,"
[tblInstitution1]![txtInitial]" & " " &
"[tblInstitution1]![txtlastname]" ,
"[tblInstitution1]![txtInstitution]" )
I'm using it as a control on a report. can anyone tell me what's
wrong with this?
Thanks
Tony
 
B

BruceM

Glad to hear it worked, but you should still check for name duplication.
Good luck with the project.

Tony Williams said:
Thanks Bruce I used your second option and it worked just fine. Thanks for
sticking with me, much appreciated.
Tony
BruceM said:
Assuming the query works on its own (i.e. it returns the expected results
in datasheet view), do you have any text boxes that have the same names
as the fields? That can cause the #Error problem.
Since you have a query you can use the IIf expression in a new query
field. In query design view, at the top of an empty column, do something
like:
InitialName: IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])
Then you can set the control source of the text box to InitialName (or
whatever you decide to call the query field).
In any case, you should check for name duplication. When you drag a
field from the field list onto a report it gives the control the same
name as the field. This was not one of Microsoft's better choices with
Access. Even if you use a query as I have described, duplicate names for
fields and controls can cause problems in other areas.

Tony Williams said:
This is what I have as the source of the control
=IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])
and this is the sql of the query
SELECT tblInstitution1.txtRefNbr, tblInstitution1.txtInstitution,
tblInstitution1.txtInitial, tblInstitution1.txtlastname,
tblInstitution1.txtCountry
FROM tblInstitution1
WHERE (((tblInstitution1.txtCountry)<>"UK"));

This is the same sorce statment that I have in my other report but it
wont work in my three others the above being a typical example??????
Thanks Bruce
Tony
What is the expression that generates the #Error indication?

Bruce it worked on one report and not the others? They are all based
on queries and I've added the extra fields initial and lastname to the
queries but I'm getting#Error. Any ideas? Real odd!
Thanks
Tony
Is tblInstitution1 the report's record source? If so, there is no
need to reference it. If txtInstitution comes from a form or some
other object, you need to name the object type (table, form, etc.):
[Forms]![frmYourForm]![YourField]. You don't put quotes around the
fields; doing so causes the literal text to appear. Also, IsNull is
placed elsewhere, and needs its own parentheses:

IIf(IsNull([txtInstitution]),[txtInitial] & " " &
[txtlastname],[txtInstitution])

I thought I had cracked if statements but I'm getting a syntax error
with this
= IIf ( [tblInstitution1]![txtInstitution] isNull,"
[tblInstitution1]![txtInitial]" & " " &
"[tblInstitution1]![txtlastname]" ,
"[tblInstitution1]![txtInstitution]" )
I'm using it as a control on a report. can anyone tell me what's
wrong with this?
Thanks
Tony
 

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

Similar Threads


Top