1

I'm new to Crystal Reports and I'm looking for a way to cut off a string if the string is too long and replace the end with ...

So I'm using Crystal Reports to generate Word or PDF Files. The Problem now is that I have a textfield that is too small for long strings.

The thing I want to do is check if the string is too long for the field. If it is, cut the string at the last , and replace the end with ....

How and where can I do that? How would the code look like?

I am using Crystal Reports 2011.

MatSnow
  • 7,357
  • 3
  • 19
  • 31
Hebi
  • 15
  • 4

1 Answers1

0

One way is to use the Len-, Left-, Instr- and Instrrev-functions.

The following formula should give you the expected result. (Only works with a monospaced font.)

NumberVar MaxLen := 200;

If Len({MyTable.MyColumn}) > MaxLen Then
    //Length is > MaxLen
    If Instr(Left({MyTable.MyColumn}, MaxLen),",")>0 Then
        //comma (,) found in first 200 chars -> cut at last comma
        Left({MyTable.MyColumn}, Instrrev(Left({MyTable.MyColumn}, MaxLen), ",")-1) & "..."
    Else    
        //NO comma (,) found in first 200 chars -> cut after char 197
        Left({MyTable.MyColumn}, MaxLen-3) & "..."
Else
    //Length is <= 200  -> use the whole text
    {MyTable.MyColumn}

In this example, the maximum length is 200 chars.
Just adjust the variable MaxLen to the number of chars you need.

MatSnow
  • 7,357
  • 3
  • 19
  • 31
  • Thank you very much for your respond. I tested it and it doesnt seem to work atm. Do i need to set a specific setting. like a Variable Size of the Field itself. Could i give you the file by any chance and you look over it quickly. Would be very much appreciate. – Hebi Jan 26 '18 at 09:25
  • Are you using a monospace font like Courier? If not, then the width of each character will vary. A "w" is much wider than an "i" and counting the number of characters isn't a reliable way to predict how many characters will fit in a field unless you are using a monospace font. – R. McMillan Jan 26 '18 at 19:52
  • I tried using the Courier font and i didnt work. I put out 120 characters before the space of the field run out. Even with the Query above set to 100 max Chars. Normally i use the Font Verdana. – Hebi Jan 29 '18 at 06:35
  • @PhilippeHebeisen Sorry, seems that I misunderstood your requirements. I'm afraid there's no reliable way to achieve this with crystal reports, except with this kind of formula and a monospace font, like R. McMillan wrote. – MatSnow Jan 29 '18 at 08:43
  • @MatSnow i tried using a Monospace font with the same Query and it didnt work, what do you mean with "this kind of formula"? I dont mind using a monospace font if it works. – Hebi Jan 29 '18 at 09:44
  • `it didnt work` does not really describe the problem or **what** is not working. Set the font to a monospaced font. ("Consolas" for example) and set the `MaxLen`-variable to `20`. What happens? With "this kind of formula" i just wanted to say that my formula is just an idea and not the only way to cut the text. – MatSnow Jan 29 '18 at 13:15
  • what i meant with it didnt work is that the Field just gives out the whole text to a point where there is no more space for it. I got 2 Screenshots on how it looks. I set the Font to Consolas for this example and the Query is set to 20Chars Max. https://imgur.com/a/MI1ef The First screenshot is the Output. The Text just ends mid sentence.(Sorry the Programm language is german...) – Hebi Jan 29 '18 at 15:42
  • It looks like you have just put the original database-field `Vehicle-EquipmentFull` on the report, instead of the formula-field `@EqFullStringCut`. ;-) – MatSnow Jan 29 '18 at 15:47