The Page Header section repeats itself on every page. For
the invoice sample we want to repeat the company logo, company name, invoice
number, purchase order number, order date, ship date, due date, customer name,
the bill to address, and the ship to address. The image for the logo can be
found with the sample code that you can download. Make sure you copy logo.bmp
into your web site project. You can simply copy the file using Windows Explorer
into the web site folder and then refresh the project in Visual Studio for it
to appear in the Solution Explorer. Follow these steps to create the page
header.
1.
Make the height of the Page Header section taller by placing your mouse
pointer over the top of the Details section. The mouse pointer should change to
a horizontal bar. Once you see the horizontal bar for the mouse pointer, click
and drag down. This will make the section larger.
2.
Right click on the white space in the Page Header section and select
InsertàPicture… from the pop-up
menu. Select the logo.bmp file and click Open. Drop the picture in the upper
left hand corner of the page header section.
3.
Using the Properties window change the Height property of the picture to
2058 and the Width to 3066.
Figure 8
4.
Next, you need to add the company name. Right click in the white space
next to the picture and select InsertàText
Object from the pop-up menu. Drop the text object to the right of the picture.
5.
Enter "Adventure Works Cycle" for the text. Be sure not to hit
the Enter key when you are done typing. To stop editing the text object click
somewhere else on the report. If you press the Enter key it would put a hard
return in the text object.
6.
Change the Height property of the text object to 2058 and set the Width
to 5400.
7.
Right click on the text object and click Format Object from the pop-up
menu. This menu gives you more options for formatting than the Property window.
8.
Click the Font tab. Change the font size to 28 and the color to Blue.
9.
Click the Paragraph tab. Change the horizontal alignment to Centered and
click OK.
Figure 9
10. The
next step is to drag the database fields on to the report. Start with the
SalesOrderHeader.SalesOrderNumber field. Even though this is not the invoice
number, we will treat it as such for this example. Expand the Database Fields
node in the Field Explorer. Expand the SalesOrderHeader table. Drag the SalesOrderNumber
field to the right corner of the Page Header section.
11. Change
the width to 1365. This will make the field smaller so you will have to move
the field again to the left corner.
12. Insert
a text object to the left of the SalesOrderNumber field by right clicking in
the Page Header section and inserting a text object. Set the text to
"Invoice #:." Right align the text object. You can use the right
align button in the toolbar to set the horizontal alignment or right click on
the object and use the Format Object dialog to set the horizontal alignment.
13. Resize
the Invoice # text object so it does not overlap with the SalesOrderNumber
field.
Figure 10
14. Add
the PurchaseOrderNumber, OrderDate, ShipDate, and DueDate the same way. You will
need to add a text object for each field to act as a label.
15. You
can easily make all the database fields the same size by holding down the Ctrl
key and clicking on each field.
16. Right
click the SalesOrderNumber field and select SizeàSame
Size from the pop-up menu. This will make all the fields the same size as the
SalesOrderNumber field.
17. You
also can align all the fields at once by keeping them selected, right clicking
the SalesOrderNumber field and selecting AlignàRights
from the pop-up menu.
18. Preview
your report by clicking the Main Report Preview button at the bottom of the
report.
Figure 11
19. Notice
that the three date fields have a time appended to them. To remove the time
component click back to the Main Report view. Hold down Ctrl and click on each
field. Right click on the OrderDate field and select Format Multiple Objects
from the pop-up menu.
20. Click
the Date and Time tab and choose "03/01/1999" from the Style list. Click
OK.
21. Click
the Main Report Preview button again and the time should be gone.
The next step is to add the customer name, bill to, and ship
to address fields. We will use the Formula Fields feature in Crystal Reports to
accomplish this. Formula Fields are a powerful feature in Crystal Reports which
allow you to use programming logic to create fields. You can use If\Then, for
loops, do loops, while loops, create local and global variable, manipulate
arrays, and a wide variety of other features. The Formula Fields enable you to do
almost anything in a report. Let us start by creating the Customer text object
and a formula field to display the customer name.
1.
Click back to the Main Report view. Insert a text object under the
picture and set the text to "Customer:." Resize the text object so
it is only the size of the text.
2.
Right click on the Formula Fields node in the Field Explorer and select
New… from the pop-up menu.
3.
Enter CustomerFullName for the formula name and click Use Editor.
4.
Not all customers have a title, middle name, and suffix. We will create
a local string variable in the formula and some if logic to concatenate the
name fields if they exist. Enter the following code for the formula.
Listing 1
local stringVar customerName := "";
if isnull({Contact.Title}) = false then
customerName := customerName + {Contact.Title} + " ";
customerName := customerName + {Contact.FirstName} + " " + {Contact.LastName};
if isnull({Contact.Suffix}) = false then
customerName := customerName + " " + {Contact.Suffix};
customerName;
5.
Click the Save and close button in the upper left hand corner of the
Formula Editor.
6.
Expand the formula field in the Field Explorer and then drag the
CustomerFullName field next to the Customer text object. You can make the width
of the field larger by clicking on the field and dragging the window handle on
the right side of the box. I made the field stretch to the right side of the
Adventure Works Cycle text object.
7.
Click the Main Report Preview button and you should see the customer
name appear. Use the page scrolling buttons to page through so you can see
some of the names that have titles and middle names. Be aware that some of the
data in the sample database is screwy. There are people that have a title of
Ms., but also have a suffix of Jr.
Figure 12
The next step is to create the "bill to address"
formula. This has a similar issue as the name, in that all customers do not
have an address line 2. We only want to include that field if it is not null.
Now we could just drag all the fields on the report on separate lines, but then
any record missing address line 2 would have a blank spot on the report.
Formula field can solve this problem.
8.
Click back to the Main Report view.
9.
Insert a text object under the Customer text object and enter "Bill
To:" for the text. Format the text object so the text is Bold.
10. Right
click on the Formula Fields in the Field Explorer and click New… from the
pop-up menu. Enter BillToAddress for the name and click the Use Editor button.
11. Enter
the following code.
Listing 2
local StringVar billTo := {addressbillto.AddressLine1} + chr(13);
if isnull({addressbillto.AddressLine2}) = false then
billTo := billTo + {addressbillto.AddressLine2} + chr(13);
billTo := billTo + {addressbillto.City} + ", " +
{stateprovincebillto.StateProvinceCode} + " " + {addressbillto.PostalCode}
This code creates a local string variable and initializes it
to AddressLine1 and adds a line feed. The chr(13) creates a line feed in the
formula. Next it checks if the AddressLine2 is null and appends it to the
string if it has a value. Then it concatenates the City, State\Province and
Postal Code.
12. Click
the Save and close button.
13. Drag
the BillToAddress formula field below the Bill To text object. Make the field
about have the width of the report.
14. Since
you do not know how much height is needed because it will vary depending on if
the customer has an Address Line 2, you can leave the height of the field the
same. However, you can make the field adjust its height automatically by right
clicking on the field and selecting Format Object from the pop-up menu.
15. Check
the Can Grow check box. This allows a field to grow vertically.
16. We
also want to surround the field with a box so it stands out on the report.
Click the Border tab from the Format Editor dialog.
17. Select
Single for the Left, Right, Top, and Bottom Line Style drop down lists. Click
OK.
18. Click
the Main Report Preview button to see the formula in action.
Figure 13
19. Now
you can create the Ship To address the same way. First, insert a text object
and set the text to "Ship To:/" Make the text object Bold. Place the
text object on the middle of the report.
20. Right
click on the Formula Fields in the Field Explorer and select New….
21. Enter
BillToAddress for the name and click Use Editor. Enter the following formula.
Listing 3
local StringVar shipTo := {addressshipto.AddressLine1} + chr(13);
if isnull({addressshipto.AddressLine2}) = false then
shipTo := shipTo + {addressshipto.AddressLine2} + chr(13);
shipTo := shipTo + {addressshipto.City} + ", " +
{stateprovinceshipto.StateProvinceCode} + " " + {addressshipto.PostalCode}
22. Click
the Save and close button and then drag the formula to the right of the Bill To
Address field on the report. Make the field width stretch close to the right
side of the report.
23. Right
click on the field and select Format Object. Check the Can Grow box and set the
border fields as you did for the Bill To Address.
Figure 14