Professional Documents
Culture Documents
LECCION 10.3 MS DataReport para ser desplegado, impreso o exportado usando Codigo
Problem
I need to create an application that enables the user to choose the output method for the report. I would like the user to be able to choose whether the report is previewed to the screen, printed to the printer, or exported to a file. Technique
The data report has various methods defined to output a report. By using these methods, the programmer can determine the output method for the report. This How-To will demonstrate the ways in which the application can enable the user to choose the output method for the report.
The PrintReport method will be used to print the report directly to the Printer. This method can be set to print directly to the printer without any user intervention, or the method can be used to display a dialog box to enable the user to select the print range and the number of copies to be printed. The ExportReport method will be used to generate a file of the report data. This method can be set to generate the file without any user intervention or a dialog can be displayed so the user can select the file type and the page range. The Show method will be used to display a print preview window of the report. This method is the same used to display any other Visual Basic form.
Steps
Open and run the OutputType.vbp project. This project enables the user to display, print, or export the report. When printing or exporting a report, the user can select the page range to be used. This is useful in large reports. 1. Start Visual Basic and choose a new Data Project. Have the Data Environment point to the BIBLO.MDB database. 2. Layout the form as in Figure 9.8 and set the properties of the objects as in Table 9.9. Leccion 10.3 DataReport con codigo Pgina 1
3. Add the following code in the Load event of the Form: Private Sub Form_Load() cbExportType.AddItem "1 cbExportType.AddItem "2 cbExportType.AddItem "3 cbExportType.AddItem "4 End Sub
This procedure will add the export types to the list box so the user can choose the format in which the report will be exported. 4. Add the following code to the Click event of the cmdPreview button: Private Sub cmdPreview_Click() DataReport1.StartUpPosition = 0 DataReport1.WindowState = 2 DataReport1.Show End Sub This procedure will maximize the size of the report windows to the full screen, then display the report. 5. Add the following code to the Click event of the cmdPrint button: Private Sub cmdPrint_Click() Leccion 10.3 DataReport con codigo Pgina 3
This procedure prints the report to the printer. There are three variables that can be passed to the PrintReport method. The first value is a Boolean value that determines if the Print dialog box is displayed. The second determines if all the pages are to be printed. If the second value is not set to rptRangeAllPages, then a third value can be used to determine the range of pages that will be printed. 6. Add the following code to the click event of the cmdExport button: Private Sub cmdExport_Click() Dim Overwrite As Boolean If txtStartPageEx.Text <> "" Or txtPrintToEx.Text <> "" Then If IsNumeric(txtStartPageEx.Text) = False Or IsNumeric(txtPrintToEx.Text) = False Then _ MsgBox "The start or end pages to print is invalid.", 64 Exit Sub
Pgina 4
The ExportReport method is used to export a report to a file. There are two primary file types, HTML and text. Unicode versions of both types can also be exported. The ExportReport method has four (five if all pages are not selected) variables which can be passed to the method. The first is the format type in which the report will be exported. If this value is left blank, a dialog box will appear, asking the user which format is preferred. There are various ways to pass the export type using code.
Pgina 5
Note that if the overwrite variable is set to False and if an attempt is made to overwrite a file, the Export dialog box will appear.
7.
Add the following code to the Click event of the cmdExit button:
Private Sub cmdExit_Click() End End Sub Clicking this button will cause the application to end. 8. Layout the report as in Figure 9.9. Set the properties as in Table 9.11.
rptTextBox
Name Font FontSize DataField DataMember Name Caption Name Caption Name DataField DataMember Name DataField DataMember Name DataField FunctionType DataMember Name Caption Name Caption Name Caption Name Caption Name Caption
How It Works
The Show method works as it does with a Visual Basic form. It has the report display to the screen. Before displaying the report to the screen settings like StartupPosition and WindowState can be set. The same properties used to display Visual Basic forms can be used to tailor the placement and position of the report on the screen when it is displayed.
The PrintReport is used to print the report directly to the printer. Setting the ShowVariable of the PrintReport determines if the report will be automatically routed to the printer or if a dialog box will Leccion 10.3 DataReport con codigo Pgina 7
The ExportReport method is a very powerful function. It can be used to export reports as text, HTML, or a user-defined HTML format. This method has six variables to set to control the way the report will be exported to a file.
The ExportFormat variable (ExportFormats collection item) is used to set the type of file that will be exported. This variable is a member of the ExportFormats collection. The ExportFormats is a collection that stores the type of report formats that can be exported. The ListBox was populated with the default items of the ExportFormats collection. The first character of each item in the ListBox is the index of that export type in the ExportFormats collection. The Left$ function is used to grab the number from the item displayed in the ListBox.
The filename variable (a String value) is used to set the name of the file that will be generated. If a full path (such as C:\data\mynewfile.html) is not defined, the current working folder will be used.
The overwrite variable (a Boolean value) is set to determine if a file already exists as defined by the filename variable should be overwritten. If this value is set to False and a file does exist, then the Export dialog box will appear as if the ShowDialog variable was set to True.
The ShowDialog variable (a Boolean value) determines is the Export File dialog box is shown. If this value is True then the filename variable does not need to be set.
The Range variable(s) (a Long value) is set to determine the range of pages that will be exported.
By using Visual Basic code, a programmer can create an application that generates reports with or without any user intervention. This How-To could be a primer to create an application that prints large reports at night or generates HTML pages to be displayed on the Web.
Pgina 9