Search code examples
stringvbacrystal-reportscrystal-reports-xi

Formatting strings in a text field in Crystal Reports XI


Good morning!

I'm hoping someone can help me with what is probably a simple question but I just cant get things to work the way I would like. I am currently working with a report that has a "catch-all" text field. In this field the user can input anything they want, and on occasion, will input information in a column format (ex. 1.1). This field allows carriage returns which are used to create "rows" in this field. The problem is that the user wants the "columns" in this field to line up on the report without having to count spaces between the columns when the information is entered (ex. 1.2). The problem is that even when this type of information is entered there is no set protocol or formatting guidelines. There may be multiple "subtitles", rows, rows separated by subtitles, etc.. The carriage return (Chr(10)) at the end of each line (or beginning of each new line) is the only thing that can be relied on consistantly.

I am currently trying to seperate each individual row, format each as desired, and put it back together like so:

    Dim output As String
    Dim sections as String
    Dim returnCount as Int 
    Dim leftText as String
    Dim rightText as String
    Dim sectionTogether as String
    Dim totalText as String
    Dim textLength as Int

    output = {table.textfield}

    sections = ExtractString(output, Chr(10), Chr(10))

    If Instr(sections,"  ") > 0 Then
      leftText = Left(sections, Instr(sections, "  "))
      textLength = Length(Left(sections, Instr(sections, "  "))
      rightText = Right(sections, Instr(sections, "  "))
      Replace(sections,"  "," ")
      sectionTogether = rightText + (Space(20) - (textLength - 3)) + leftText
      totalText = totalText + sectionTogether
      formula = totalText
    else
      formula = output

This is the gist of what I'm trying to do. A couple of notes: 1) I know I am missing a loop of some kind to format every section but I dont know how to set that up in crystal 2) I have VB programming experience, but I am noob in crystal and its limited tools so I feel hamstringed and I'm having trouble finding the methods and tools I would use in Visual Studio 3) My syntax may also be off in a few places because I am still learning how to set this up and I REALLY miss a debugger.

I hope someone can help me, I have been researching for over a week and it feels like I'm just beating my head against a wall.

Thank you in advance.

The output examples
ex. 1.1
"Your current charges are:
Jan          12.89
Feb         117.44
Mar          15.02
Apr        4.17"

ex. 1.2
"Your current charges are:
Jan                       12.89
Feb                      117.44
Mar                       15.02
Apr                        4.17"

Solution

  • The first thing you're going to want to do is split up your rows via split(), like this:

    local stringvar array wallOText := split({table.textfield},chr(10));

    This will give you an array of strings where each array entry is a "row". Now you can loop over your rows with the following:

    for i := 1 to ubound(wallOText) step 1 do <some stuff to wallOText[i]>

    Now, getting the columns right-justified is a little trickier, but here's some code to get you started. You can adjust the column widths to whatever you may need (in this case, 20 spaces). Also note, you have to use a fixed-width font.

    local stringvar output;
    local stringvar array row;
    local numbervar i;
    local numbervar j;
    local stringvar array wallOText := split({@some text},chr(10));
    local stringvar elem;
    
    for i := 1 to ubound(wallOText) do  //loop over lines
        (row := split(wallOText[i]," ");
         for j := 1 to ubound(row) do //loop over words
            (elem := trim(row[j]); //get current element and cut white space
             if not(elem="") then 
             output := output + space(20-len(elem)) + elem); //build output string
         output := output + chr(10));
    
    output