Programming with Built-In Functions
The Formula Workshop in Crystal Reports gives you the ability to write very powerful formulas. In addition to writing your own formulas, Crystal Reports has dozens of built-in functions that decrease the amount of work you have to do. After all, why re-invent the wheel when you don’t have to? This chapter lists and explains the different functions that come with Crystal syntax. The functions are grouped by category so you can quickly find the ones you want. The categories are: String Functions, Converting Data Types, Formating Values for Output, Math Functions, Generating Random Numbers, and Date and Time Functions. If you prefer to have an alphabetical list, then reference the Index at the back of the book to see the page number of the one you are looking for.
Crystal syntax is the first programming language that came with Crystal Reports. It is also the default language in the Formula Workshop dialog box. Basic syntax was added to Crystal Reports at some point later in its evolution because people needed a language that was easier to use. If you are familiar with programming languages, then you might notice that Crystal syntax has a lot of similarities to the C programming language. Unfortunately, the C language is a difficult language to learn and not exactly the easiest one to get started with. With the huge popularity of the Microsoft Office suite of products (Word, Excel, and Access), many people are already familiar with how to program macros using VBA (Visual Basic for Applications). It’s also very common for universities to teach VBA in their basic business courses. There was a need for Crystal Reports to have a second programming language that was easier for people to learn, and making it similar to Microsoft’s VBA would be even better. Hence, Basic syntax was added to Crystal Reports.
This chapter and the next presents all the code listings in Crystal syntax. Since this is the default programming language, it is the one most people learn first. For those of you who want to learn Basic syntax, Appendix A is a Basic syntax programming reference that can be used in conjunction with these two chapters.
The Formula Workshop makes it easy to use the built-in functions. It gives you a Formula Functions window which lists every available function in Crystal Reports. Each node in the tree is a function category. Clicking on the plus sign next to a node expands it and shows all the functions in that category. As a refresher, the Formula Functions window is shown again here.
Figure 6-1. Formula Functions window.
Not only is the function listed for you, but if it needs you to pass any additional information to it, this is listed as well. For example, if you want to select a certain number of characters from the left-most portion of a string, you would use the Left() function. In the Formula Functions window it is shown as Left(str, Length). This tells you that the first argument is the string you want to parse and the second argument is how many character you want to copy.
While most of the functions give you helpful argument information, some are not as helpful as others. For example, the CStr() function converts any data type to a string and it can take up to four arguments. But when you look at this function in the Formula Functions window, it only says CStr(x, y, z, w, q). This is pretty cryptic and to figure out what each argument means you’ll have to either consult the help file or look for the reference in this chapter. When you see the function you want to use, double-click on it and it gets added to the Definition area at the bottom.