Click here to Skip to main content
15,894,825 members
Articles / Desktop Programming / Windows Forms

Dynamic Reports UsingSQL Queries

Rate me:
Please Sign up or sign in to vote.
4.32/5 (28 votes)
23 Nov 20052 min read 201.8K   4.4K   89  
Dynamic report by using the PrintDocument component with SQL queries.
Public Class frmTestReport
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Panel2 As System.Windows.Forms.Panel
    Friend WithEvents btnClose As System.Windows.Forms.Button
    Friend WithEvents btnDirectPrint As System.Windows.Forms.Button
    Friend WithEvents btnComponentShow As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.btnClose = New System.Windows.Forms.Button
        Me.Panel2 = New System.Windows.Forms.Panel
        Me.btnComponentShow = New System.Windows.Forms.Button
        Me.btnDirectPrint = New System.Windows.Forms.Button
        Me.Panel1.SuspendLayout()
        Me.Panel2.SuspendLayout()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Panel1.Controls.Add(Me.btnClose)
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.Panel1.Location = New System.Drawing.Point(0, 61)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(368, 40)
        Me.Panel1.TabIndex = 0
        '
        'btnClose
        '
        Me.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.btnClose.Location = New System.Drawing.Point(272, 8)
        Me.btnClose.Name = "btnClose"
        Me.btnClose.Size = New System.Drawing.Size(88, 24)
        Me.btnClose.TabIndex = 0
        Me.btnClose.Text = "Close"
        '
        'Panel2
        '
        Me.Panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Panel2.Controls.Add(Me.btnComponentShow)
        Me.Panel2.Controls.Add(Me.btnDirectPrint)
        Me.Panel2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Panel2.Location = New System.Drawing.Point(0, 0)
        Me.Panel2.Name = "Panel2"
        Me.Panel2.Size = New System.Drawing.Size(368, 61)
        Me.Panel2.TabIndex = 1
        '
        'btnComponentShow
        '
        Me.btnComponentShow.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.btnComponentShow.Location = New System.Drawing.Point(152, 16)
        Me.btnComponentShow.Name = "btnComponentShow"
        Me.btnComponentShow.Size = New System.Drawing.Size(160, 32)
        Me.btnComponentShow.TabIndex = 1
        Me.btnComponentShow.Text = "Use Component Show"
        '
        'btnDirectPrint
        '
        Me.btnDirectPrint.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.btnDirectPrint.Location = New System.Drawing.Point(16, 16)
        Me.btnDirectPrint.Name = "btnDirectPrint"
        Me.btnDirectPrint.Size = New System.Drawing.Size(120, 32)
        Me.btnDirectPrint.TabIndex = 0
        Me.btnDirectPrint.Text = "Direct Print"
        '
        'frmTestReport
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(368, 101)
        Me.Controls.Add(Me.Panel2)
        Me.Controls.Add(Me.Panel1)
        Me.Name = "frmTestReport"
        Me.Text = "Test SQL Dynamic Report"
        Me.Panel1.ResumeLayout(False)
        Me.Panel2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnDirectPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDirectPrint.Click
        Try
            Dim SqlStr As String
            '----- Set SQL string for report by this format (1 set per 1 report column):
            '----- Column 1 -> column name
            '----- Column 2 -> Position on paper, I set all width has range between 0-99
            '----- Column 3 -> Justify (L-Left, R-Right, C-Center)
            '----- Column 4 -> Has summarize in this column (Y/N)
            '----- Column 5 -> Display format(such as #,##0.00)
            '----- Column 6 -> Data Column
            '----- Please look at this example-------------------------------------------
            SqlStr = " select 'Product Name',0,'L','N','',ProductName," & _
                     " 'Order Value',60,'R','Y','#,##0.00',sum((a.UnitPrice*a.Quantity)-a.Discount) as Ordervalue " & _
                     " from Products, [Order Details] a " & _
                     " where(Products.ProductID = a.ProductID) " & _
                     " group by ProductName"
            Dim frmDynamicPrint As New frmPrintDynamicForm
            Dim oledbcon As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=Products.mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False")
            Dim oleda As New OleDb.OleDbDataAdapter(SqlStr, oledbcon)
            oleda.Fill(frmDynamicPrint.dbSet)

            '--- We can set properties of PageSetDlg and prnSetDlg object for print option 
            'frmDynamicPrint.PageSetDlg.PageSettings.Landscape = True

            '---If it has sum column is this report, we have to set hasSum flag to true. 
            frmDynamicPrint.hasSum = True
            frmDynamicPrint.FontName = "Tahoma"
            frmDynamicPrint.FontSize = 12
            frmDynamicPrint.FontSizeHead = 16
            frmDynamicPrint.FontSizeHead2 = 14
            frmDynamicPrint.InitPrint()
            frmDynamicPrint.HeadFirstString = "Report for Product Order Value Summary"
            frmDynamicPrint.HeadSecondString = "Test Report"
            frmDynamicPrint.DateString = Now.ToString
            frmDynamicPrint.PrintPreview()
            frmDynamicPrint.Dispose()
        Catch ex As Exception
            MessageBox.Show(Err.Description)
        End Try
    End Sub

    Private Sub btnComponentShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComponentShow.Click
        Dim frmDynamicPrint As New frmInheritPrintDynamicForm
        frmDynamicPrint.ShowDialog()
        frmDynamicPrint.Dispose()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Thailand Thailand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions