Report Printing
Print Preview for your program.
|
|
Download Now!
Download your FREE copy of the SoftReports Free Viewer now.
|
Completely Free
Click Here
|
Add Report Printing and Preview to your program in as little as half an hour.
|
|
The following code shows how to set up the SortReports Viewer control on a form to display reports from a report layout disk file. To use this code in Visual Basic you need to:-
1. Make sure that you have DBViewer.ocx selected in the "Project/Components" dialog.
2. Make sure that you have DBViewer2.dll selected in the "Project/References" dialog.
3. Create a new form in Visual Basic.
4. Drop a Viewer.Preview control onto it and name it "Preview1". (You will need to have the control referenced in the Visual Basic "Project / References" dialogue box)
5. Add the code below to the form.
6. Modify the constants cDBFile and cReportDirectory to point to your database location and your default report directory.
7. You may need to change the Connection String, cConnStr, depending on which version of Microsoft ADODB you have installed on your computer.
|
That's it. You've got a fully functional reports system ready to go.
Const cConnStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source="
Const cDBFile = "\dbTest.mdb"
Const cReportDirectory = "\Reports"
' set default directory when form loads
Private Sub Form_Load()
' the default directory for the "Open File" dialogue
Preview1.DefDirectory = App.Path & cReportDirectory
End Sub
' set control size to match form size
Private Sub Form_Resize()
Preview1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
End Sub
' event handler - respond to user button click
Private Sub Preview1_LoadRequest(FileName As String)
' arrive here when the "Open File" button is clicked
' on the preview form. The filename is the one that
' has been selected in the "File Open" dialogue
LoadPreview FileName
End Sub
' event handler - respond to user button click
Private Sub Preview1_CloseForm()
' arrive here when the "Exit" button is clicked
' on the preview form
Unload Me
End Sub
' load the report
Public Sub LoadPreview(Optional FileName As String = "")
Dim sql As String, connstr As String, rs As New ADODB.Recordset
Me.Caption = "Viewer Demo - Disk File Report - " & FileName
' read report format from disk
Preview1.ReadFromDisk FileName
' get the sql statement that was read in from the file
sql = Preview1.SQLStatement
' construct the connection string
connstr = cConnStr & App.Path & cDBFile
' open the database
rs.Open sql, connstr, adOpenDynamic, adLockReadOnly, adCmdText
' set the report datasource
Set Preview1.DataSource = rs
' show the report
Preview1.ShowReport
' for tidyness set rs to nothing,
' but DO NOT CLOSE THE DATABASE
Set rs = Nothing
End Sub
|
|