Click here to Skip to main content
15,892,059 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.7K   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
    Friend WithEvents btnDirectLineH As System.Windows.Forms.Button
    Friend WithEvents btnDirectMultiLine As System.Windows.Forms.Button
    Friend WithEvents btnWrap 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.btnWrap = New System.Windows.Forms.Button
        Me.btnDirectMultiLine = New System.Windows.Forms.Button
        Me.btnDirectLineH = New System.Windows.Forms.Button
        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, 181)
        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.btnWrap)
        Me.Panel2.Controls.Add(Me.btnDirectMultiLine)
        Me.Panel2.Controls.Add(Me.btnDirectLineH)
        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, 181)
        Me.Panel2.TabIndex = 1
        '
        'btnWrap
        '
        Me.btnWrap.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.btnWrap.Location = New System.Drawing.Point(16, 136)
        Me.btnWrap.Name = "btnWrap"
        Me.btnWrap.Size = New System.Drawing.Size(192, 32)
        Me.btnWrap.TabIndex = 4
        Me.btnWrap.Text = "Direct Print - Wrap Text"
        '
        'btnDirectMultiLine
        '
        Me.btnDirectMultiLine.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.btnDirectMultiLine.Location = New System.Drawing.Point(16, 96)
        Me.btnDirectMultiLine.Name = "btnDirectMultiLine"
        Me.btnDirectMultiLine.Size = New System.Drawing.Size(192, 32)
        Me.btnDirectMultiLine.TabIndex = 3
        Me.btnDirectMultiLine.Text = "Direct Print - Multilines"
        '
        'btnDirectLineH
        '
        Me.btnDirectLineH.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.btnDirectLineH.Location = New System.Drawing.Point(16, 56)
        Me.btnDirectLineH.Name = "btnDirectLineH"
        Me.btnDirectLineH.Size = New System.Drawing.Size(192, 32)
        Me.btnDirectLineH.TabIndex = 2
        Me.btnDirectLineH.Text = "Direct Print - Change Line Hight"
        '
        '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 like component"
        '
        '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 - Normal"
        '
        'frmTestReport
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(368, 221)
        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 -> Start position on paper, I set all width has range between 0-99
            '----- Column 3 -> End position on paper, I set all width has range between 0-99
            '----- Column 4 -> Justify (L-Left, R-Right, C-Center)
            '----- Column 5 -> Has summarize in this column (Y/N)
            '----- Column 6 -> Display format(such as #,##0.00)
            '----- Column 7 -> Rest in line? Begin with 1
            '----- Column 8 -> Data Column
            '----- Please look at this example-------------------------------------------
            SqlStr = " select 'Product Name',0,40,'L','N','',1,ProductName," & _
                     " 'Order Value',41,60,'R','Y','#,##0.00',1,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.LineHeight = 1
            frmDynamicPrint.HeadFirstString = "Report for Product Order Value Summary"
            frmDynamicPrint.HeadSecondString = "Test Normal Direct Report"
            frmDynamicPrint.DateString = Now.ToString
            frmDynamicPrint.InitPrint()
            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

    Private Sub btnDirectLineH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDirectLineH.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 -> Start position on paper, I set all width has range between 0-99
            '----- Column 3 -> End position on paper, I set all width has range between 0-99
            '----- Column 4 -> Justify (L-Left, R-Right, C-Center)
            '----- Column 5 -> Has summarize in this column (Y/N)
            '----- Column 6 -> Display format(such as #,##0.00)
            '----- Column 7 -> Rest in line? Begin with 1
            '----- Column 8 -> Data Column
            '----- Please look at this example-------------------------------------------
            SqlStr = " select 'Product Name',0,20,'L','N','',1,ProductName," & _
                     " 'Sum Discount',21,40,'R','Y','#,##0.00',1,sum(a.Discount) as Discount, " & _
                     " 'Sum Quantity',41,60,'R','Y','#,##0.00',1,sum(a.Quantity) as Quantity, " & _
                     " 'Sum Order Value',71,90,'R','Y','#,##0.00',1,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.LineHeight = CInt(InputBox("Input Line Hight", "Please input line hight", 1))
            frmDynamicPrint.HeadFirstString = "Report for Product Order Value Summary"
            frmDynamicPrint.HeadSecondString = "Test Change Line Hight Report"
            frmDynamicPrint.DateString = Now.ToString
            frmDynamicPrint.InitPrint()
            frmDynamicPrint.PrintPreview()
            frmDynamicPrint.Dispose()
        Catch ex As Exception
            MessageBox.Show(Err.Description)
        End Try
    End Sub

    Private Sub btnDirectMultiLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDirectMultiLine.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 -> Start position on paper, I set all width has range between 0-99
            '----- Column 3 -> End position on paper, I set all width has range between 0-99
            '----- Column 4 -> Justify (L-Left, R-Right, C-Center)
            '----- Column 5 -> Has summarize in this column (Y/N)
            '----- Column 6 -> Display format(such as #,##0.00)
            '----- Column 7 -> Rest in line? Begin with 1
            '----- Column 8 -> Data Column
            '----- Please look at this example-------------------------------------------
            SqlStr = " select 'Product Name',0,40,'L','N','',1,ProductName," & _
                     " 'Unit Price',41,60,'R','N','#,##0.00$',1,UnitPrice, " & _
                     " 'Units In Stock',61,80,'R','N','#,##0.00',1,UnitsInStock, " & _
                     " 'Value',81,99,'R','Y','#,##0.00$',1,(UnitPrice*UnitsInStock) as ProductValue, " & _
                     " 'Units On Order',11,30,'R','N','#,##0.00',2,UnitsOnOrder, " & _
                     " 'Reorder Level',31,50,'R','N','#,##0.00',2,ReorderLevel, " & _
                     " 'Quantity Per Unit',61,99,'L','N','',2,QuantityPerUnit " & _
                     " from Products" & _
                     " order 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 
            'PageSetDlg.PageSettings.Landscape = True

            frmDynamicPrint.hasSum = True
            frmDynamicPrint.FontName = "Tahoma"
            frmDynamicPrint.FontSize = 12
            frmDynamicPrint.FontSizeHead = 16
            frmDynamicPrint.FontSizeHead2 = 14
            frmDynamicPrint.LineHeight = 1
            frmDynamicPrint.SkipLinePerRecord = 1
            frmDynamicPrint.InitPrint()
            frmDynamicPrint.HeadFirstString = "Report for Product Detail"
            frmDynamicPrint.HeadSecondString = "Test Multilines Report"
            frmDynamicPrint.DateString = Now.ToString
            frmDynamicPrint.PrintPreview()
        Catch ex As Exception
            MessageBox.Show(Err.Description)
        End Try
    End Sub

    Private Sub btnWrap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrap.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 -> Start position on paper, I set all width has range between 0-99
            '----- Column 3 -> End position on paper, I set all width has range between 0-99
            '----- Column 4 -> Justify (L-Left, R-Right, C-Center)
            '----- Column 5 -> Has summarize in this column (Y/N)
            '----- Column 6 -> Display format(such as #,##0.00)
            '----- Column 7 -> Rest in line? Begin with 1
            '----- Column 8 -> Data Column
            '----- Please look at this example-------------------------------------------
            SqlStr = " select 'Product Name',0,20,'L','N','',1,ProductName," & _
                     " 'Sum Discount',21,40,'R','Y','#,##0.00',1,sum(a.Discount) as Discount, " & _
                     " 'Sum Quantity',41,60,'R','Y','#,##0.00',1,sum(a.Quantity) as Quantity, " & _
                     " 'Sum Order Value',71,90,'R','Y','#,##0.00',1,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.HeadFirstString = "Report for Product Order Value Summary"
            frmDynamicPrint.HeadSecondString = "Test Change Line Hight Report"
            'frmDynamicPrint.LineHeight = 3
            frmDynamicPrint.DateString = Now.ToString
            frmDynamicPrint.WrapTextFlag = True
            frmDynamicPrint.InitPrint()
            frmDynamicPrint.PrintPreview()
            frmDynamicPrint.Dispose()
        Catch ex As Exception
            MessageBox.Show(Err.Description)
        End Try
    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