You are on page 1of 9

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012

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

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012


Figure 9.8. Arrangement of form frmOutPut.frm. Table 9.9. rptFunction control functions description. OBJECT Form Property Name Caption Frame Name Caption CheckBox Name Caption TextBox TextBox ComboBox Label Name Name Name Name Caption Label Name Caption Label Name Caption CommonDialog Frame Name Name Caption TextBox TextBox CheckBox Name Name Name Caption Label Name Setting frmOutPut Data Report Output frExport Export Options chkExport Show Export Dialog txtStartPageEx txtPrintToEx cbExportType lbExportType Export Type lbStartPageEx Start Page: lbPrintToEx Print to: CD frPrint Print Options txtPrintTo txtStartPage chkShowDialog Show Print Dialog lbPrintTo Pgina 2

Leccion 10.3 DataReport con codigo

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012


Caption Label Name Caption CommandButton Name Caption CommandButton Name Caption CommandButton Name Caption CommandButton Name Caption Print To: lbStart Start Page: cmdExit Exitr cmdExport Export cmdPrint Print cmdPreview Preview

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

HTML" Text" Unicode HTML" Unicode Text" cbExportType.ListIndex = 0

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

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012


Dim fReturn As Long If txtStartPage.Text <> Or txtPrintTo.Text <> Then If IsNumeric( txtStartPage.Text) = False Or : IsNumeric( txtPrintTo.Text) = False Then MsgBox The start or end pages to print is invalid., 64 Exit Sub End If End If If txtStartPage.Text = And txtPrintTo.Text = Then fReturn = DataReport1.PrintReport( chkShowDialog.Value * -1, rptRangeAllPages) Else fReturn = DataReport1.PrintReport( chkShowDialog.Value * -1, txtStartPage.Text, _ txtPrintTo.Text) End If If fReturn = 2 Then MsgBox Print Job Sent to Printer Else MsgBox Print Job Cancelled End If End Sub

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

Leccion 10.3 DataReport con codigo

Pgina 4

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012


End If End If CD.ShowSave If CD.FileName <> "" Then If Dir(CD.FileName) <> "" Then Ans% = MsgBox("Do you want to overwrite this file ?", _ vbQuestion Or vbYesNo) If Ans% = 6 Then Overwrite = True Else Overwrite = False End If Else Overwrite = False End If If txtStartPageEx.Text = "" And txtPrintToEx.Text = "" Then DataReport1.ExportReport DataReport1.ExportFormats(CLng(Left$(cbExportType.List _ (cbExportType.ListIndex), 1))).Key, CD.FileName, Overwrite, _ chkExport.Value * -1, rptRangeAllPages Else DataReport1.ExportReport DataReport1.ExportFormats _ (CLng(Left$(cbExportType.List(cbExportType.ListIndex) _ , 1))).Key, CD.FileName, Overwrite, chkExport.Value * -1, _ txtStartPageEx.Text, txtPrintToEx.Text End If End If End Sub

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.

Leccion 10.3 DataReport con codigo

Pgina 5

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012


This example uses the ExportFormats collection of the report. These are predefined types as outlined in Table 9.10. The second value is the filename of the report to be written. In this example, the filename is set from the Common dialog box. The third variable determines if a file should be overwritten. The fourth variable determines if the Export dialog box should be displayed. The next two variables are identical to those used in PrintReport method and determine the page range to be exported.

Table 9.10. Export file types.


EXPORT TYPE HTML HTML Unicode Text Text Unicode Constant rptKeyHTML rptKeyUnicodeHTML_UTF8 rptKeyText rptKeyUnicodeText Description Export in HTML Export in HTML Export in Text Export in Text

Format Unicode Format Format Unicode Format

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.

(Figure 9.9. The arrangement of the report DataReport1.dsr.)

Table 9.11. Object and Properties for DataReport1.


OBJECT DataReport DataMember rptLabel Property Name Name Font FontSize Caption Setting DataReport1 Command1_Grouping lbTitle Arial 14.25 Number of Books per Pgina 6

Leccion 10.3 DataReport con codigo

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012


Author txtAuthor Arial 12 Author Command1_Grouping Label2 Title: Label3 Year Published txtTitle Title Command1 txtYearPublished Year Published Command1 Function1 Title 4 rptFuncCnt Command1 lbTotalBooks Total Books by Author Label11 %p lbPageTitle Page lbof of Label4 %P

rptTextBox

rptLabel rptLabel rptTextBox rptTextBox rptFunction

rptLabel rptLabel rptLabel rptLabel rptLabel

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

9. Run the project. Try the various combinations of report output.

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

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012


appear to ask the user the page range and number of copies to be printed. Setting the range determines the pages that will be printed. If a value for the range is not set, all the pages will be printed.

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.

ExportReport(ExportFormat, filename, Overwrite, ShowDialog, Range, PageFrom, PageTo)

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.

Comments Leccion 10.3 DataReport con codigo Pgina 8

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012

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.

Leccion 10.3 DataReport con codigo

Pgina 9