Use custom functions of Crystal Reports for code reuse

Source: Internet
Author: User
Tags date1

 

Generally, we can use formulas to implement some special functions, but for some situations that need to be processed repeatedly
User-defined functions can be used for implementation.

On the formula editing page, you can see two items in the upper left corner:

Report user-defined functions and reserve User-Defined Functions

A report user-defined function is only available in a single report template. Different templates cannot be shared.
The custom function of the repository is unavailable for separate Crystal Reports and. Net versions. This is a function of the Crystal Report Server version or BOE.
After the function is converted to a custom function of the repository, the function is stored in the system database, so that different reports can be used for function sharing.
However, there is no impact on a single function.

Now let's talk about the use of user-defined functions in combination with an instance.
There is a proposition that shows a Date Field October 28, 2008]
This effect cannot be achieved using a custom date format. Although we can use a formula, it is very troublesome to write formulas for multiple such fields each time.
So here we choose to use a custom function.

Switch to the formula Editor interface, right-click [report user-defined functions], and create a function named mydate.
Switch the syntax to Basic (you can also use the Crystal syntax), and then write the function body.


I will not explain the specific function content. Here I use two functions to implement this function.
Another function is ChNumber, which is used to process reuse in mydate.

The function body is as follows:

 

'For date conversion
Function myDate (date1 as date)
Dim sY as string
Dim sM as string
Dim sD as string
'Retrieve year, month, and day
SY = totext (year (date1), 0)
SM = totext (month (date1), 0)
SD = totext (day (date1), 0)
MyDate = chNumber (sY, "0") & "year" & chNumber (sM, "1") & "month" & chNumber (sD, "1") & "day"
End Function

 

The type of the parameter t to be processed. 0 is the year, replace the characters one by one, 1 is the month and date, and 10 carry
Function ChNumber (s as string, t as string)
Dim s0 as string
'Replace all first
S0 = Replace (s ,",","")
S0 = Replace (s0, "0", "0 ")
S0 = Replace (s0, "1", "1 ")
S0 = Replace (s0, "2", "2 ")
S0 = Replace (s0, "3", "3 ")
S0 = Replace (s0, "4", "4 ")
S0 = Replace (s0, "5", "5 ")
S0 = Replace (s0, "6", "6 ")
S0 = Replace (s0, "7", "7 ")
S0 = Replace (s0, "8", "8 ")
S0 = Replace (s0, "9", "9 ")
'Perform decimal processing on the day of the month
If t = "1" then
'Decimal processing, adding a 10 character to the middle of two digits
If Len (s0) = 2 Then
S0 = left (s0, 1) & "10" & right (s0, 1)
'Processing is just 10, 20, 30.
If left (s0, 1) = "1" then s0 = replace (s0, "10", "10 ")
If right (s0, 1) = "zero" then s0 = replace (s0, "zero ","")
End if
End if
ChNumber = s0
End Function

 

Then let's do a simple test:

Create a date parameter xx,

Create a formula yy with the following content: mydate ({? Xx })

Shown as: January 1, October 28, 2008

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.