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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loops - For, Do..While, etc

Status
Not open for further replies.

bobgsysop

Programmer
Nov 3, 2000
25
US
Hi -

I wrote a formula using a Do...While loop in v8, crystal syntax. It works fine.

However, the client who wants the report uses Crystal 7, and apparently the only control structure available is an If statement.

Is this true? How do you get around this with version 7?

Thanks
BobG
 
Loops did not come into play until Version 8 of CR. I do not know a workaround. Software Support for Macola, Crystal Reports and Goldmine
dgilsdorf@mchsi.com
 
Explain what the Loop was doing, and we can see if there is an alternative without the Loop. Ken Hamady, On-site Custom Crystal Reports Training & Consulting
Public classes and individual training.
Guide to using Crystal in VB
tek@kenhamady.com
 
Here's the formula in Crystal 8. The field {OEShipping.CartsUsed} contains a string that keeps track of the number of carts of different configurations. For example, "5=4(4);7=1(3);9=2(0);" would mean 4 carts with 5 shelves, 1 cart with 7 shelves, and 2 cart with 9 shelves. The number inside the parenthesis does not apply in this function;, it has no bearing on the calculation. I'm getting the total carts by adding the number right after each equal sign.

The formula:
numberVar intCount:=0;
numberVar intPos:=0;
numberVar intLen:=0;
numberVar GetCartCount:=0;
stringVar strWork:={OEShipping.CartsUsed};
stringvar boolFlag:="N";

If strWork <> &quot;&quot; Then
(
Do
(
intPos := InStr(1, strWork, &quot;=&quot;);
If intPos <> 0 Then
(
intLen := Len(strWork);
strWork := Mid(strWork, intPos + 1, intLen);
intCount := Val(strWork);
GetCartCount := GetCartCount + intCount;
boolflag := &quot;N&quot;;
)
Else
boolFlag := &quot;Y&quot;;
)
while boolFlag = &quot;N&quot;;
);

GetCartCount;

 
You might need to use the Instr() function to find the positions of the equal signs. Is there a reasonable limit to the number of the equal signs in one entry? Ken Hamady, On-site Custom Crystal Reports Training & Consulting
Public classes and individual training.
Guide to using Crystal in VB
tek@kenhamady.com
 
Hi Ken-

I am using InStr - right after the Do.

There can be as many as 12 equal signs, but usually no more than 4.

Thanks
BobG
 
Since V7 can't loop, you would have to have a formula with 12 Instr() function instances, each starting where the last left off. The Instr() function has an optional argument that tells it where to start. Not as elegant, but can be done. Ken Hamady, On-site Custom Crystal Reports Training & Consulting
Public classes and individual training.
Guide to using Crystal in VB
tek@kenhamady.com
 
Hi Ken-

Ouch.

Perhaps I should look into creating a User Defined Function.

Thanks!
Bob
 
Actually I don't think IF-then Blocks also exist in CR 7 either...but I would do it Ken's suggested way too....the size of the formula is large but it operates quite fast.

I have done the formula as below for a few cycles of &quot;=&quot; signs...just add more blocks to extend the number of cycles


***********************************************************

WhilePrintingRecords;
numberVar intCount:=0;
numberVar intPos:=0;
numberVar intLen:=0;
numberVar GetCartCount:=0;
stringVar strWork:={OEShipping.CartsUsed};
stringvar boolFlag:= &quot;N&quot;;
stringVar NullFlag:= &quot;N&quot;;

If not isnull(strWork) Then
NullFlag := &quot;Y&quot;;

if NullFlag = &quot;N&quot; then
intPos := InStr(1, strWork, &quot;=&quot;);
If NullFlag = &quot;N&quot; and intPos <> 0 Then
intLen := Len(strWork);
If NullFlag = &quot;N&quot; and intPos <> 0 Then
strWork := Mid(strWork, intPos + 1, intLen);
If NullFlag = &quot;N&quot; and intPos <> 0 Then
GetCartCount := GetCartCount + Val(strWork);
If NullFlag = &quot;N&quot; and intPos <> 0 Then
boolflag := &quot;N&quot;
else
boolFlag := &quot;Y&quot;;

if boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; then
intPos := InStr(1, strWork, &quot;=&quot;);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
intLen := Len(strWork);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
strWork := Mid(strWork, intPos + 1, intLen);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
GetCartCount := GetCartCount + Val(strWork);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
boolflag := &quot;N&quot; else boolFlag := &quot;Y&quot;;

if boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; then
intPos := InStr(1, strWork, &quot;=&quot;);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
intLen := Len(strWork);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
strWork := Mid(strWork, intPos + 1, intLen);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
GetCartCount := GetCartCount + Val(strWork);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
boolflag := &quot;N&quot; else boolFlag := &quot;Y&quot;;

if boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; then
intPos := InStr(1, strWork, &quot;=&quot;);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
intLen := Len(strWork);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
strWork := Mid(strWork, intPos + 1, intLen);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
GetCartCount := GetCartCount + Val(strWork);
If boolflag = &quot;N&quot; and NullFlag = &quot;N&quot; and intPos <> 0 Then
boolflag := &quot;N&quot; else boolFlag := &quot;Y&quot;;

//(** Clone as many tests(starting at the second &quot;if Block&quot;
// as you wish **)

......

GetCartCount;

*****************************************************

Hope this helps....jim

 
Ngolem,

If then else is logic is available as far back as v4, and probably further. What is an if-then 'block'? Ken Hamady, On-site Custom Crystal Reports Training & Consulting
Public classes and individual training.
Guide to using Crystal in VB
tek@kenhamady.com
 
I mean where more than one statement is associated with an if-then condition

ie: I think that the following example is only available as of Crystal 8.0

if {something} = true then
(
do this;
do that;
do the other;
)
else
do the false thing;


Before that then you had to repeat the if-then condition for every action within the brackets (I call the statements between the brackets a &quot;block&quot;)

Jim
 
Jim,

You made me curios, so I just tried it in all versioins back to v4.5. They all seemed to work. My simple test used the sample data:

WhilePrintingRecords;
StringVar one;
StringVar two;

If {Customer.Country} = &quot;USA&quot;
then (one := {Customer.Region};two := {Customer.Customer Name}) else (one := one; two := two);

one + '-' + two Ken Hamady, On-site Custom Crystal Reports Training & Consulting
Public classes and individual training.
Guide to using Crystal in VB
tek@kenhamady.com
 
hmmm....I have George Peck's book on CR 7.0 and there is not one reference to using multiple statements in an If-Then block...

I have CR 7.0 on my machine here and in the help file there was no reference to this either.

IT HAS BEEN THERE ALL ALONG!!!

I never thought to try it I guess...would have made life alot easier for many-many reports...I never saw an example until CR 8.0 ... so I never questioned it.

thanks Ken

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top