cancel
Showing results for 
Search instead for 
Did you mean: 

Best Practice for Debugging Code

Former Member
0 Kudos

Hi!

I am new to development in Crystal. What method do you recommend to verify my formulas are doing what I expect? For example, in other environments I would print or prompt the result.

What is the best approach in Crystal?

Any help you can provide will be appreciated.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

I just tried Carl's suggestion of using the debug statement but must be doing something wrong because I am just getting a "division by zero" prompt as a result, no values.

Any ideas what I forgot to do?

Former Member
0 Kudos

You could try using alerts.

Fuskie

Who expects Crystal Reports does not really have breakpoint/debug support since it is primarily a linear data processor rather than programming code...

Former Member
0 Kudos

I'm sorry, I should have mentioned to put this at the BOTTOM of the formula, not at the top. The values displayed are what they are at the point of the divide by zero error. If the division is first thing in the formula, none of the other calculations have been performed yet... (Actually, you want to put it at the exact point where you want the breakpoint. That can be in the middle of the formula.)

HTH,

Carl

Answers (4)

Answers (4)

Former Member
0 Kudos

Never mind, Carl. I realize now I wasn't looking in the left pane. Your suggestion works.

Thank you!

Former Member
0 Kudos

Hi Carl,

I must still be doing something wrong as it still isn't working for me. Can I use your suggestion within a custom function?

Below is my code:

Function cdQSIPeriod () As String

dim debug as number

dim YY as number

dim MM as number

YY = Year(CurrentDate)

MM = Month(CurrentDate)

If MM = 1 then

MM = 12

YY = YY - 1

else

MM = MM - 1

End If

cdQSIPeriod = MM&YY

debug = debug / debug

End Function

Any help you can provide will be appreciated.

Former Member
0 Kudos

It really depends on what you're trying to achieve. If it's simply seeing if the calculation is right, just drop the field in the appropriate format and have it printed (or add a new format for this purpose that you can suppress or display).

If a formula comes up with a value and you can't figure out why, my approach has been to add the following code to the formula in question (basic syntax):


dim debug as number
debug = debug / debug

This will cause a divide by zero error, which will then cause Crystal to pull up the formula. On the left of the window, all fields and variables used in the formula are displayed with their current value. (You need to use the "debug" variable because Crystal Designer will give a syntatical divide by zero error if you code "formula = 1 / 0"...)

As written, the breakpoint will hit on the first iteration of the formula. You can cause the "breakpoint" to happen on later iterations by using something like:


global dbgcnt as number
dim debug as number
dbgcnt = dbgcnt + 1
if dbgcnt = 5 then
  debug = debug / debug
end if

where the "5" is the iteration number that you want it to break on.

If anyone has a better way to "set a breakpoint", I'd love to hear it! (Ah ha! A great idea for the Suggestions thread. Going there now!)

HTH,

Carl

Former Member
0 Kudos

There is no single or best method. I will sometimes comment out portions of a formula to check results at a certain stage. If you are working with a variable shared across formulas or sub-reports, you might create a display formula to show the current value. I have also created copies of a formula enabling one piece of an expression at a time to make sure it evaluates as I expect it should.

Fuskie

Who thinks the best method is to have validated results to compare the report against...