Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shared Variables

Status
Not open for further replies.

vangye

Technical User
Jul 24, 2012
2
US
Hi All,
I'm pretty new to the world of Shared Variables.

Could anyone help me, I've gotten as far as concatenating my data sets like so:
John Smith, Jane Doe, Robert Smith, Sam Thomas

would it be possible to add an "and" before my last set
John Smith, Jane Doe, Robert Smith, and Sam Thomas

this is in my subreport

whileprintingrecords;
shared stringvar b;
if not isNull({Name}) then
(
if (b = '') then
b := b + {Name}
else
b := b + ', ' + {Name}
)
 
I think you might be able to do it as an after the fact. In other words after you have built your string (back in the main report?). You would have code like this:
// Find the last comma, get the text to the left of it. Add in the and, concatenate the rest of the string after the last comma.
shared stringvar b;
left(b,revinstr(b,","))+"and "+right(b,len(b)-reinstr(b,","))
 

This is easier to do with an array, so just take the comma separated string you have and plug it into an array using the split function. The if statement makes sure there is more than one value, and if so modifies the last value to include the additional text.


whileprintingrecords;
shared stringvar b;
if not isNull({Command.PatName}) then
(
if (b = '') then
b := b + {Command.PatName}
else
b := b + ', ' + {Command.PatName}
);

stringvar array v_names := split(b,",");

if ubound(v_names) > 1
then
join(v_names[1 to ubound(v_names) - 1],", ") + ", and " + v_names[ubound(v_names)]
else
v_names[1]



One question - this is shared variable, but the problem as I understand it has nothing to do with the sharing part - right?
 
I'm getting this error: A subscript must be between 1 and the size of the array. Details: errorKind
 
You don't need to have Shared Variables unless you're getting data from a subreport.

For most purposes you don't need ordinary variables either. Split the process into several Formula Fields and display the output in a temporary line if it's not clear what you're getting.

For the code shown, I'm suspicious of
ubound(v_names) - 1],", ")


[yinyang] Madawc Williams (East Anglia, UK). Using Crystal 2008 with SQL and Windows XP [yinyang]
 

I couldn't get this to break using anywhere from one to ten records. The only thing I notice is that you don't address null patient names - you test for nulls, but then don't do anything if the field is null. Try adding the additional else at the end.


whileprintingrecords;
shared stringvar b;
if not isNull({Command.PatName}) then
(
if (b = '') then
b := b + {Command.PatName}
else
b := b + ', ' + {Command.PatName}
)
else b; // *** Add this line ***



stringvar array v_names := split(b,",");

if ubound(v_names) > 1
then
join(v_names[1 to ubound(v_names) - 1],", ") + ", and " + v_names[ubound(v_names)]
else
v_names[1]


 
Not sure about subreports. But in reports this code works.

whileprintingrecords;
stringvar ConcatNames;
if not isNull({NAME}) then
(
if (ConcatNames = '') then
ConcatNames := ConcatNames + {NAME}
else
if onlastrecord then
ConcatNames := ConcatNames + ', and ' + {NAME}
else
ConcatNames := ConcatNames + ', ' + {NAME}
)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top