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

if statement problem

Status
Not open for further replies.

mjmiller24

Programmer
Aug 29, 2001
17
0
0
US
I have a phone number extension that, sometimes, has a leading zero(s). Sometimes the field is null, sometimes the leading zero(s) is missing, and sometimes it is formatted properly. I have attempted to write a if/else if/else statement.

I only inculded a snippet of code where I numbered the lines. I keep getting the error that a string is required, at line 11, where the Else if statement is.

At some point the formula editor said there were no errors, but then the phone number extension would only give me spaces and zeros for entries when I browsed the results.

I think part of the problem is that I am not familiar with the syntax I am supposed to use in Crystal Reports.

Any help would be appreciated.


1: Local StringVar ext;
2: Local NumberVar extLen;
3: Local NumberVar i;
4:
5: extLen := Length({MAIN.TELEPHONE_EXTENSION});
6:
7: If IsNull({MAIN.TELEPHONE_EXTENSION}) Then
8: (
9: ext := "";
10: )
11: Else If ( not Isnull({MAIN.TELEPHONE_EXTENSION}) and (extLen < 4) Then
12: (
13: for i:= extLen to 3 do
14: ext := &quot;0&quot; & ext;
15: )
16: Else
17: (
18: ext = {MAIN.TELEPHONE_EXTENSION};
19: );
20: ext
 
This is a bit simpler to do it with a ReplicateString function...
Be sure to check that the equality test (=) and assignment:)=) are used appropriately.

Local StringVar ext ;
Local NumberVar extLen ;
Local NumberVar i ;
extLen := Length({MAIN.TELEPHONE_EXTENSION}) ;
If IsNull({MAIN.TELEPHONE_EXTENSION}) Then
ext := &quot;&quot;
Else If extLen < 4 Then
ext := ReplicateString (&quot;0&quot;, extLen) + ext
Else
ext := {MAIN.TELEPHONE_EXTENSION} ;
ext
Malcolm Wynden
Authorized Crystal Engineer
malcolm@wynden.net
 
Okay, that didn't give me any errors, but now I have a different problem.

Let's say I create a formula like this

{table.area_code}

Also note that there are no null values for the area code.
Now, I name the formula areaCode. I compared the number of returned results from @areaCode to the amount returned by directly placing the field in the report. I found that, when I browsed the values, I @areaCode returned fewer values. Why is that? Am I doing something wrong?

Thanks again.
 
When you browse a field's values (using the CR browse feature), you only see a sample taken from the first 500 rows of the table. Ken Hamady, On-site/Phone Crystal Reports Training/Consulting
Quick Reference Guide to using Crystal in VB
 
The code Malcom gave you has some bugs in it

******************************
Local StringVar ext ;
Local NumberVar extLen ;
Local NumberVar i ;
extLen := Length({MAIN.TELEPHONE_EXTENSION}) ;
If IsNull({MAIN.TELEPHONE_EXTENSION}) Then
ext := &quot;&quot;
Else If extLen < 4 Then
ext := ReplicateString (&quot;0&quot;, extLen) + ext
Else
ext := {MAIN.TELEPHONE_EXTENSION} ;
ext
*********************************************

This formula will fail if {MAIN.TELEPHONE_EXTENSION} is null since there is no test for it being null in line 3.

Also in the ReplicateString formula you will pad the extension number with the number of characters of the retrieved extension number.

There is no reason for the variable &quot;i&quot; anymore.

finally EXT is a null value in this line of the formula

ext := ReplicateString (&quot;0&quot;, extLen) + EXT

I think what you are looking for is a consistant 4 number extension number...so you want to pad zeros in the front
ie. an extension no. of &quot;10&quot; would turn into &quot;0010&quot;


***************************************
Local StringVar ext ;
Local NumberVar extLen ;

if not isNull({MAIN.TELEPHONE_EXTENSION}) then
extLen := Length({MAIN.TELEPHONE_EXTENSION}) ;

If IsNull({MAIN.TELEPHONE_EXTENSION}) Then
ext := &quot;&quot;
Else If extLen < 4 Then
ext := ReplicateString (&quot;0&quot;, 4 - extLen) +
{MAIN.TELEPHONE_EXTENSION}
Else
ext := {MAIN.TELEPHONE_EXTENSION} ;

ext;

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

regards jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top