combine fields

R

rml

I'm using the following code to combine four fields. It works fine. My
problem is when all the fields are blank I get #Error on the report for that
record. What I would like when all fields are blank is "NA" to be placed.

Thanks.

=Left(IIf(IsNull([np1]),"",[np1] & ", ") & IIf(IsNull([np2]),"",[np2] & ",
") & IIf(IsNull([np3]),"",[np3] & ", ") & IIf(IsNull([np4]),"",[np4] & ",
"),Len(IIf(IsNull([np1]),"",[np1] & ", ") & IIf(IsNull([np2]),"",[np2] & ",
") & IIf(IsNull([np3]),"",[np3] & ", ") & IIf(IsNull([np4]),"",[np4] & ",
"))-2)
 
B

boblarson

Look into using the NZ function. However, it looks like you have a
normalization problem which could be solved with some redesign. There really
shouldn't be repeating fields np1, np2, np3, etc. But, only you can decide
how you're going to go about it.
--
Bob Larson
Access World Forums Super Moderator

Tutorials at http://www.btabdevelopment.com

__________________________________
 
R

rml

Someone from microsoft help me create this code originally. It may not be
perfect but it works. I just need to add if all the fields are empty then
"NA" and that would be fine. Can you show me an example?

Thanks.
 
P

Pieter Wijnen

=IIF(IsNull(np1+np2+np3+np4),"NA",Left(IIf(IsNull([np1]),"",[np1] & ", ") &
IIf(IsNull([np2]),"",[np2] & ", ") & IIf(IsNull([np3]),"",[np3] & ", ") &
IIf(IsNull([np4]),"",[np4] & ", "),Len(IIf(IsNull([np1]),"",[np1] & ", ") &
IIf(IsNull([np2]),""[np2] & ", ") & IIf(IsNull([np3]),"",[np3] & ", ") &
IIf(IsNull([np4]),"",[np4] & ", "))-2))

HtH

Pieter

rml said:
Someone from microsoft help me create this code originally. It may not be
perfect but it works. I just need to add if all the fields are empty then
"NA" and that would be fine. Can you show me an example?

Thanks.

rml said:
I'm using the following code to combine four fields. It works fine. My
problem is when all the fields are blank I get #Error on the report for
that
record. What I would like when all fields are blank is "NA" to be
placed.

Thanks.

=Left(IIf(IsNull([np1]),"",[np1] & ", ") & IIf(IsNull([np2]),"",[np2] &
",
") & IIf(IsNull([np3]),"",[np3] & ", ") & IIf(IsNull([np4]),"",[np4] & ",
"),Len(IIf(IsNull([np1]),"",[np1] & ", ") & IIf(IsNull([np2]),"",[np2] &
",
") & IIf(IsNull([np3]),"",[np3] & ", ") & IIf(IsNull([np4]),"",[np4] & ",
"))-2)
 
T

Tom Wickerath

Perhaps I'm not seeing something, but I think you can greatly simplify your
expression to the following:

=(np1 + ", ") & (np2 + ", ") & (np3 + ", ") & np4

Make sure that the name of the control is *not* the same as the name of any
of your fields, or else you will get a circular reference error. You're
apparently okay on this, since you indicated an error only if all four fields
are null. If you had a circular reference error, you'd have it all the time.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________

:

I'm using the following code to combine four fields. It works fine. My
problem is when all the fields are blank I get #Error on the report for that
record. What I would like when all fields are blank is "NA" to be placed.

Thanks.

=Left(IIf(IsNull([np1]),"",[np1] & ", ") & IIf(IsNull([np2]),"",[np2] & ",
") & IIf(IsNull([np3]),"",[np3] & ", ") & IIf(IsNull([np4]),"",[np4] & ",
"),Len(IIf(IsNull([np1]),"",[np1] & ", ") & IIf(IsNull([np2]),"",[np2] & ",
") & IIf(IsNull([np3]),"",[np3] & ", ") & IIf(IsNull([np4]),"",[np4] & ",
"))-2)
 
R

rml

Do I add it at the beginning before the =Left? Can you show me how the
entire code would look?

Thanks.
 
R

rml

I see it now. Thanks. It works fine but one small problem. How do I get
rid of the comma at the end of each? Like I had originally?

Thanks again.
 
R

rml

It works but need the comma removed at the end. It should look like this:

Now: 1234, 4534, Should be: 1234, 4534

It need to eliminate the last comma and leave the ones between.

Thanks.
 
T

Tom Wickerath

Okay, let's try a few variations. The first one (easier expression) will work
as long as the npx values are filled in from x=1 to x=4. In other words, np1
will never be null if there is a value entered for np2, and np2 will never be
null if there is a value for np3, etc. The expressions I've been suggesting
all rely on the idea of null concatentation, where null plus anything is
still null. Thus, if np2 is null, then (np2 + ", ") will also be null, as
will (", "+[np2]):

=Nz((([np1]) & (", "+[np2]) & (", "+[np3]) & (", "+[np4])),"NA")

However, I think you can see how the expression above will result in a
leading comma + space if np1 is null but np2 is not null. If this is a
possibility, ie. the numbers are *not* filled in from x=1 to x=4, then the
expression becomes a lot more complicated. This one seems to work (I think):

=IIf(Left$(Nz((([np1]) & (", "+[np2]) & (", "+[np3]) & (",
"+[np4])),"NA"),2)=", ",Mid$(Nz((([np1]) & (", "+[np2]) & (", "+[np3]) & (",
"+[np4])),"NA"),3),Nz((([np1]) & (", "+[np2]) & (", "+[np3]) & (",
"+[np4])),"NA"))



Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 

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