Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on

Hide a field if it has no data in Crystal Reports

If I have:
Name
Address1
Address2
City

and want to hide the "Address2" Database field if it is empty, like:
Name
Address1
City

how can I do that?
I'm using Visual Studio 2005 crystal reports.  thanks
Avatar of kenwagers
kenwagers
Flag of United States of America image

Put each field in a different detail band, and then select "Suppress Blank Section" under the section expert.

To create separate detail bands, right click on the left side of the design window in the 'D' band, and select "Insert Section Below".
Avatar of MikeMCSD

ASKER

thanks ken, . .

so something like this:

Details C (Section 6)
Name
Address1
City

Details D  "Suppress"
Address2

Details E
City
ASKER CERTIFIED SOLUTION
Avatar of kenwagers
kenwagers
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perfect, thank you
Avatar of Mike McCracken
Mike McCracken

That is one method.  Another involves using a formula and testing each field in turn

WhilePrintingRecords;
Local StringVar dispString;

If Not IsNull({Field1}) then
    dispString := {Field1} & chr(13);

If Not IsNull({Field2}) then
    dispString := {Field2} & chr(13);

If Not IsNull({Field3}) then
etc

Left(dispString,Length(dispString)-1)

mlmcc    
But will that suppress the line?  The other method I use is somewhat more involved, but nice if you're doing it in many reports.

use this formula in the report:

ColsToRows({Command.Name},{Command.street_1},
{Command.street_2},{Command.street_3},
{Command.city}+' '+{Command.state}+'  '+{Command.zip})

After creating the "ColsToRows" function in the Crystal Repository:

Function ColsToRows(optional field1 as String = "", optional field2 as String = "", optional field3 as String = "", optional field4 as String = "", optional field5 as String = "", optional field6 as String = "", optional field7 as String = "", optional field8 as String = "", optional field9 as String = "", optional field10 as String = "")
    dim I as number
    dim hold as string

    If not(Trim(field1) in Array(",","")) Then hold = hold + field1 + chr(13)
    If not(Trim(field2) in Array(",","")) Then hold = hold + field2 + chr(13)
    If not(Trim(field3) in Array(",","")) Then hold = hold + field3 + chr(13)
    If not(Trim(field4) in Array(",","")) Then hold = hold + field4 + chr(13)
    If not(Trim(field5) in Array(",","")) Then hold = hold + field5 + chr(13)
    If not(Trim(field6) in Array(",","")) Then hold = hold + field6 + chr(13)
    If not(Trim(field7) in Array(",","")) Then hold = hold + field7 + chr(13)
    If not(Trim(field8) in Array(",","")) Then hold = hold + field8 + chr(13)
    If not(Trim(field9) in Array(",","")) Then hold = hold + field9 + chr(13)
    If not(Trim(field10) in Array(",","")) Then hold = hold + field10 + chr(13)

    ColsToRows = hold
End Function
mlmcc, I see your logic now.  Your method will also suppress the line.  Nice solution.