Using VSView Reporting Edition > Typical Usage Scenarios > Web Scenarios |
In typical Web scenarios, VSReport8 runs on the server machine and creates reports either in batch mode or on demand. The user can select the reports and preview or print them on the client machine, using a Web browser.
This scenario is based on a server application that runs periodically and creates a predefined set of reports, saving them to HTML or PDF files. These files are referenced by Web pages on your site, and they are downloaded to the client machine like any other Web page.
To implement this type of application, follow these steps:
Use the VSReport8 Designer to create all the reports you will need. (See the Designer documentation for details on how to do this.)
Create an application on the server that contains a VSReport8 control. If you don't want to use forms and windows, create the control using the CreateObject function in Visual Basic or CoCreateInstance in Visual C++.
Add a routine that runs periodically and updates all the reports you want to make available to your users. The loop would look like this:
Example Title |
Copy Code
|
---|---|
' ** this runs every 6 hours: ' get a tab-separated list all ' reports in the definition file sFile = App.Path & "\MyReports.xml" sList = vsr.GetReportInfo(sFile, vsrRIList) vList = Split(sList, vbTab) ' refresh the reports on the server For i = 0 To UBound(vList) vsr.Load sFile, vList(i) vsr.RenderToFile "Reports\Auto\" & _ vList(i) & ".htm", vsrHTMLPaged Next |
The code uses the GetReportInfo method to retrieve a tab-separated list of all reports contained in the MyReports.xml report definition file (created in step 1), then parses the list and exports each report to a paged HTML file. (Paged HTML files contain one HTML page for each page in the original report, and all pages are hyperlinked.)
Edit the "home" HTML page by adding links to the reports that were saved.
You are not restricted to HTML. VSReport8 can also export to PDF files, which can be viewed on any browser with freely available plug-ins. The PDF format is actually superior to HTML in many ways, especially when it comes to producing hard copies of your Web reports.
Another option you have is saving the reports into VSPrinter format and using the VSPrinter control as a viewer. To do this, you would host the VSPrinter control on an HTML page and use its URL property to load the report from the server. This solution gets you the same quality you would have with the PDF alternative, without using any plug-ins. The drawback is that only Internet Explorer can host ActiveX controls, so people using other browsers would not be able to see the reports. This makes this option most suitable to Intranet environments.
In this scenario, reports are created on-demand, possibly based on data supplied by the user. This type of solution typically involves using an ASP page that presents a form to the user and collects the information needed to create the report, then creates a VSReport8 control to render the report into a temporary file, and returns a reference to that file.
The example that follows is a simple ASP page that allows users to enter some information and to select the type of report they want. Based on this, the ASP code creates a custom version of the NorthWind Employee Sales by Country report and presents it to the user in the selected format.
The sample uses a temporary file on the server to store the report. In a real application, you would have to generate unique file names and delete them after a certain time, to avoid overwriting reports before the users get a chance to see them. Despite this, the sample illustrates the main techniques involved in delivering reports over the Web with VSReport8.
The ASP page starts by including a couple of header files that define control constants, and some standard HTML:
Example Title |
Copy Code
|
---|---|
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>VSReport8 ASP Sample</title> </head> <!--#include file="VSPrinterVBS.inc" --> <!--#include file="VSReportVBS.inc" --> <style type='text/css'> body {font:10pt Arial;} </style> <body> <p><font size=5 face=Verdana> <b>VSReport8 ASP Sample</b></font></p> <p> This sample demonstrates how you can use the ComponentOne <b>VSReport8</b> control to generate custom Web reports from an ASP page. </p> <hr> |
Note that this code includes two files, VSPrinterVBS.inc and VSReportVBS.inc, which contain definitions for all constants used by the controls. These files are included in the distribution disk, along with the ASP sample. Although these constants are not strictly necessary (you could type their values directly into the code), they do make the code more readable.
After this introduction, the page contains a form where the user can enter the year for the report, the sales goal for the employees that year, and buttons that generate different types of reports. There are three options: drill-down HTML, PDF, and VSPrinter format. When the user clicks a button, the page is submitted and the ASP script can process the data:
Example Title |
Copy Code
|
---|---|
<form> <p><b>Employee Sales by Country Report</b></p> <p>Report for Year: <input type=text name=theYear value='<%= Request("theYear") %>'></p> <p>Sales Goal: <input type=text name=theQuota value='<%= Request("theQuota") %>'></p> <p><input type=submit name=docType value=' 1 '> Click here to create an HTML report. </p> <p><input type=submit name=docType value=' 2 '> Click here to create a PDF report. You will need Adobe's PDF plug-ins to see the report. </p> <input type=submit name=docType value=' 3 '> Click here to create a VSPrinter report. You will need the VSPrinter control to see the report. </p> </form> <hr> |
Now comes the interesting part of the sample. The first time the page is displayed, it only shows the form. When the page is submitted, the following script gets executed for the second time and generates the report based on the contents of the theYear, theQuota and docType variables.
Example Title |
Copy Code
|
---|---|
<% ' get path to use when saving temp files thePath = Request.ServerVariables("PATH_TRANSLATED") i = InstrRev(thePath, "\") thePath = Left(thePath, i) ' create document and insert it in this page docType = Request("docType") Select Case docType Case " 1 ": CreateDocHTML Case " 2 ": CreateDocPDF Case " 3 ": CreateDocVSPrinter Case Else: Response.Write _ "Click a button to select a report." End Select |
This code simply interprets the user input and delegates the job to the appropriate function. The main part of the code is the function that creates the report and stores it in a temporary file. Here it is:
Example Title |
Copy Code
|
---|---|
' create VSReport8 document and save it on the server Sub CreateDoc(sFileName) ' create VSReport8 object set vsr = CreateObject("VSReport8.VSReport") ' load report definition vsr.Load thePath & "NWind.xml", _ "Employee Sales by Country" ' validate the information submitted by the user theYear = CLng(Request("theYear")) theQuota = CLng(Request("theQuota")) If theYear < 1994 Then theYear = 1994 If theYear > 1996 Then theYear = 1996 ' customize the report's RecordSource sSQL = "SELECT DISTINCTROW " & _ "Employees.Country, Employees.LastName, " & _ "Employees.FirstName, Orders.ShippedDate, Orders.OrderID, " & _ " [Order Subtotals].Subtotal AS SaleAmount " & _ "FROM Employees INNER JOIN (Orders INNER JOIN " & _ " [Order Subtotals] ON Orders.OrderID = " & _ " [Order Subtotals].OrderID) " & _ " ON Employees.EmployeeID = Orders.EmployeeID " & _ "WHERE Year(Orders.ShippedDate) = " & theYear & ";" vsr.DataSource.RecordSource = sSQL ' customize the report's event handlers sScript = "If SalespersonTotal > " & theQuota & " Then " & vbCrLf & _ " ExceededGoalLabel.Visible = True " & vbCrLf & _ " SalespersonLine.Visible = True " & vbCrLf & _ "Else " & vbCrLf & _ " ExceededGoalLabel.Visible = False " & vbCrLf & _ " SalespersonLine.Visible = False " & vbCrLf & _ "End If" vsr.Sections(vsrGroupHeader2).OnPrint = sScript ' render report to temp file on the server If Instr(sFileName, ".htm") > 0 Then vsr.RenderToFile sFileName, vsrHTMLDrillDown ElseIf Instr(sFileName, ".pdf") > 0 Then vsr.RenderToFile sFileName, vsrPDF ElseIf Instr(sFileName, ".vp") > 0 Then vsr.RenderToFile sFileName, vsrVSPrinter End If ' we're done with the object Set vsr = Nothing End Sub |
The function creates a VSReport8 control, loads the report definition, and customizes it by changing the RecordSource and OnPrint properties to incorporate the parameters supplied by the user. Then the function uses the RenderToFile method to render the report into a temporary file on the server. The format used is deduced from the file extension.
We are almost done. Now that the report has been saved into a file on the server, all we have to do is make it available to the user. Here's how you can do that:
Example Title |
Copy Code
|
---|---|
Sub CreateDocHTML() ' create HTML report sFileName = thePath & "temp.htm" CreateDoc(sFileName) ' read HTML document into a variable and ' delete temporary file const ForReading = 1 set fso = CreateObject("Scripting.FileSystemObject") set f = fso.OpenTextFile(sFileName, ForReading) s = f.ReadAll f.Close fso.DeleteFile(sFileName) Set fso = Nothing ' insert HTML document in output stream Response.Write "Click headings to collapse/expand groups." Response.Write s End Sub |
This function calls CreateDoc to create the HTML report, then reads the whole report file and inserts it directly into the page. This approach is good for short reports such as the one we are using. For longer reports, you would probably want to use the paged HTML file and insert a link to the first page of the report. In this sample, we are using the HMTL drill-down flavor. This allows users to click on group headers to collapse and expand the groups.
Next comes the function used to create and deliver the PDF report. It is similar to the HTML function, but even simpler:
Example Title |
Copy Code
|
---|---|
Sub CreateDocPDF() ' create HTML report sFileName = thePath & "temp.pdf" CreateDoc(sFileName) ' create link to new PDF file s = "" s = s & "Click here to see the PDF report." Response.Write s End Sub |
This function inserts a link into the main document. The user can click on the link to see the PDF report (provided he has the necessary Adobe plug-ins installed on his computer).
Finally, here is the function that delivers the report using the VSPrinter control as a viewer:
Example Title |
Copy Code
|
---|---|
Sub CreateDocVSPrinter() ' create VSPrinter report sFileName = thePath & "temp.vp" CreateDoc(sFileName) ' create VSPrinter control on the client and set its URL property ' to read the newly created document s = "<object classid='clsid:A8561647-E93C-11D3-AC3B-CE6078F7B616' " & _ "id=vp width=497 height=609>" Response.Write s Response.Write "<param name=URL value='temp.vp'>" Response.Write "<param name=ZoomMode value=3>" ' zmWholePage Response.Write "</object>" End Sub %> |
The function inserts a VSPrinter object on the page (using its unique ClassID value), and sets the VSPrinter's URL property to the name of the temporary file. When the VSPrinter control is initialized, it will download the file and show it on the page, where the user will be able to browse and print the report.
That is the end of the ASP code. To finish the page, don't forget to close the body and html tags:
Example Title |
Copy Code
|
---|---|
</body> </html> |
That is the end of the ASP sample. It contains about 100 lines of code and 50 of HTML. That is not much when you consider that it can create custom reports and deliver them on the Web in three different formats.
The picture below shows what the result looks like in the browser, after selecting the HTML output option: