Search code examples
crystal-reportscrystal-reports-2010

Crystal Reports automatically add spaces to left of input for parameter.


So right from the start, I would like to apologize for my ignorance and I assume that this answer is one that is very simple. Unfortunately, I cannot find the answer anywhere. This issue is this:

I am creating a report and it has a parameter to limit the results to one record. if the end user types in the number that they are looking for it will produce no results. If two spaces are placed before that number it will produce the appropriate results. The field that is being referenced is limited to 9 characters and the records right now only have 7 digits, that is why the two spaces are required.

Is there a way if someone enters the 7 digits that Crystal would append the two spaces to the left.

The user enters:"1234567" but it gets interpreted as:" 1234567"

Any assistance will be appreciated.


Solution

  • As long as the user will always enter exactly 7 characters and you always want to append exactly 2 spaces to the beginning of the data they enter, then you can solve this with a very simple formula such as the following.

    " " & ToText({?ParameterFieldName})

    The ToText() function may not be needed. When concatenating strings its not a bad idea to include it though to ensure you avoid any potential for a data mismatch error.

    If you think there is a chance for someone to enter more or less than 7 characters though, then the formula will need to be a bit more robust. If you want to enforce the requirement for 7 characters to always be entered into the parameter field there are properties for Min Length and Max Length that can be set when creating/editing your parameter field that may be useful to you.

    EDIT To answer follow-up question

    There aren't any looping structures available in CR formulas, so the best you could do would be a CASE statement or Nested IF structure. Here is what the nested if would look like. With the following formula the user can enter any number of character from 2 to 8 and it would append enough spaces to the front of the string to ensure the result is 9 characters in length. I omitted the ToText() function in this example. The formula would be safer with that function used like it was in the first formula above. As long as the parameter field is setup to accept String text it shouldn't be necessary though.

    If Length({?ParameterField}) = 8 Then
         " " & {?ParameterField})
    Else If Length({?ParameterField}) = 7 Then
         "  " & {?ParameterField})
    Else If Length({?ParameterField}) = 6 Then
         "   " & {?ParameterField})
    Else If Length({?ParameterField}) = 5 Then
         "    " & {?ParameterField})
    Else If Length({?ParameterField}) = 4 Then
         "     " & {?ParameterField})
    Else If Length({?ParameterField}) = 3 Then
         "      " & {?ParameterField})
    Else If Length({?ParameterField}) = 2 Then
         "       " & {?ParameterField})