1

I am a newbie to Crystal Reports. I need to insert a variable calculated at runtime inside a string.

Example:

"You have $[VARIABLE] dollars left in your account"

Do I need to have three elements: the first part of the string "You have $", the variable, and the last part of the string "dollars left in your account"?

Or can I do something like String.Format, where I can insert the value during report generation?

1 Answer 1

2

Is this in a parameter or just laid out on the form? There are two ways to do it.

First way, you can drag a label onto your report. Inside the label, you put "You have " then drag the field you want to move from the field explorer onto the label, then finish with " dollars left in your account.". That is assuming the database field doesn't need formatting. If it does, you can create a formula to format it (then drag the formula field in the same way). I like this way because you can kind see on your design view what it's going to look like, handy for large reports.

Second way, create a formula field to get the text and then drag it onto the form. You can use either "Crystal" or "Basic" syntax. I typically use basic just because I'm good at it and familiar with it. Example:

' Basic Syntax
If IsNull(zip_code) = false Then
    formula = "Your zip code is " & {zip_code.zip_code}
Else
    formula = "There is no zip code available."
End If

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

OK. I think I understand. So the first way you are suggesting is having 3 different fields, like in my question. Is that correct?
Not exactly no. It's 1 label, you're just typing text into it, and then dragging the database field into that 1 label. The benefit of that is, there will be only the space you allocate so it will line up like it's all together. If you use 3 fields, you may have awkward white space. The first method uses 1 label. The second method is a field itself, so you drag just that one field out. Neither of these use 3 labels (that would work, but you have the potential white space issue and it's a little more of a pain to deal with).
Ah. Got it. So I can drag a db field into the label. Correct?
Yes, that is correct. You can drag most fields from the "Field Explorer" into the label.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.