8/2/2019 Leccion 10.3 - Data Report Con Codigo
1/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. Iwould 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 forthe report. This How-To will demonstrate the ways inwhich the application can enable the user to choose the output method for the report.
The PrintReportmethod will be used to print the report directly to the Printer. This method can
be set to print directly to theprinter without any user intervention, or the method can be used to display adialog box to enable the user to select the print range and the number of copies to beprinted.
The ExportReportmethod will be used to generate a file of the report data. This method can beset to generate the file without any userintervention or a dialog can be displayed so the user can select the file
type and the page range.
The Showmethod 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 exportthe report. When printing or exporting a report, the user can select the page range to be used. This is usefulin 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
8/2/2019 Leccion 10.3 - Data Report Con Codigo
2/9
[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012
Figure 9.8. Arrangement of formfrmOutPut.frm.
Table 9.9. rptFunction control functions description.
OBJECT Property Setting
Form Name frmOutPut
Caption Data Report Output
Frame Name frExport
Caption Export Options
CheckBox Name chkExport
Caption Show Export Dialog
TextBox Name txtStartPageEx
TextBox Name txtPrintToEx
ComboBox Name cbExportType
Label Name lbExportType
Caption Export Type
Label Name lbStartPageEx
Caption Start Page:
Label Name lbPrintToEx
Caption Print to:
CommonDialog Name CD
Frame Name frPrint
Caption Print Options
TextBox Name txtPrintTo
TextBox Name txtStartPage
CheckBox Name chkShowDialog
Caption Show Print Dialog
Label Name lbPrintTo
Leccion 10.3 DataReport con codigo Pgina 2
http://popup%28%2709vdb41.gif%27%29/http://popup%28%2709vdb41.gif%27%29/8/2/2019 Leccion 10.3 - Data Report Con Codigo
3/9
[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012
Caption Print To:
Label Name lbStart
Caption Start Page:
CommandButton Name cmdExit
Caption Exitr
CommandButton Name cmdExport
Caption Export
CommandButton Name cmdPrint
Caption Print
CommandButton Name cmdPreview
Caption Preview
3. Add the following code in the Loadevent of the Form:
Private Sub Form_Load()
cbExportType.AddItem "1 - HTML"
cbExportType.AddItem "2 - Text"
cbExportType.AddItem "3 - Unicode HTML"
cbExportType.AddItem "4 - Unicode Text" cbExportType.ListIndex = 0End 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 Clickevent of the cmdPreviewbutton:
Private Sub cmdPreview_Click()DataReport1.StartUpPosition = 0DataReport1.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
8/2/2019 Leccion 10.3 - Data Report Con Codigo
4/9
[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 IfIf 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
PrintReportmethod. The first value is a Boolean value that determines if the Print dialog box isdisplayed. The second determines if all the pages are to be printed. If the second value is not set torptRangeAllPages, then a third value can be used to determine the range of pages that will beprinted.
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 OrIsNumeric(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
8/2/2019 Leccion 10.3 - Data Report Con Codigo
5/9
[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 = FalseEnd 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
8/2/2019 Leccion 10.3 - Data Report Con Codigo
6/9
8/2/2019 Leccion 10.3 - Data Report Con Codigo
7/9
[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS 2012
AuthorrptTextBox Name txtAuthor
Font ArialFontSize 12DataField Author
DataMember Command1_GroupingrptLabel Name Label2
Caption Title:rptLabel Name Label3
Caption Year PublishedrptTextBox Name txtTitle
DataField TitleDataMember Command1
rptTextBox Name txtYearPublishedDataField Year PublishedDataMember Command1
rptFunction Name Function1
DataField TitleFunctionType 4 rptFuncCntDataMember Command1
rptLabel Name lbTotalBooksCaption Total Books by Author
rptLabel Name Label11Caption %p
rptLabel Name lbPageTitleCaption Page
rptLabel Name lbofCaption of
rptLabel Name Label4Caption %P
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 beset. 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
8/2/2019 Leccion 10.3 - Data Report Con Codigo
8/9
[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 thedefault 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
8/2/2019 Leccion 10.3 - Data Report Con Codigo
9/9
[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