Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27355

Help with Looping through HTML break reports

$
0
0
Hey Guys,

I have a CREATE,READ,UPDATE,DELETE program that uses an Access Database.

Break reports can be generated to show sales by each sales person ID.

Users will select the sales person ID the wish through a combo box, sales person id's are produced from the relevant table in the access database.

All sales by sales person ids will be listed in a data grid view.

A user will then be able to generate a HTML break report for the selected Sales Person ID.

This is done with the following code:

Module:

Code:

Option Explicit On
Option Strict On
Imports System.Data.OleDb
Imports System.Globalization.DateTimeFormatInfo
Module mBreakReport

Public Function g_HTMLReport(ByVal sCurrentSaleReader As String) As String
Dim sSql As String
Dim sConnection As String
Dim sColOutput As String
Dim sTableRow, sTableCell As String

Dim iRowCounter As Integer
Dim iColCounter As Integer
Dim iTotal As Integer

Dim oConn_Report As OleDbConnection 'To instantiate Connection obj.
Dim oCmd_Report As OleDbCommand 'To Instantiate a Command obj.
Dim oDataReader As OleDbDataReader 'To instantiate a DataReader obj.

Dim sPageText As String
Try
' –- Step1: Open Output File stream (This instantiates a
' -- StreamWriter object used for outputing a Text file)

' –- Step2: Get Sql String

sSql = "SELECT SALE_ID, PRODUCT_ID, SALE_DATE, DESCRIPTION, SALES_PERSON_ID, CATEGORY_ID, UNIT_PRICE, QTY, GIVEN, SURNAME FROM QSALES_REPORT WHERE SALES_PERSON_ID='" + sCurrentSaleReader + "' ORDER BY SALES_PERSON_ID;"

' –- Step3: Build connection string to connect to
' -- an OleDB Data provider
sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
sConnection = sConnection & "User ID=Admin;"
sConnection = sConnection & "Data Source="
sConnection = sConnection & Application.StartupPath & _
"\ComputerShop2013_STUDENTS.mdb"

' –- Step4: establish connection to the student database
' -- using the connection string
oConn_Report = New OleDbConnection(sConnection)
oConn_Report.Open()

' –- Step5: create a database command object using the connection
' -- object and set the SQL statement it will execute
oCmd_Report = oConn_Report.CreateCommand()
oCmd_Report.CommandText = sSql

' –- Step6: execute SQL command and place results into a datareader
oDataReader = oCmd_Report.ExecuteReader()

'sCurrentSaleReader = CStr(oDataReader.Item("SALES_PERSON_ID"))

Dim sGiven As String
Dim sSurname As String

'Do page hEADING
sPageText = g_sHTMLHeader("Sales Person ID " & sCurrentSaleReader & ", Name:" & sGiven & "" & sSurname) & vbCrLf

If Not oDataReader.Read() Then
'**** There is nothing to read
sPageText = sPageText & "</table> <br>" & vbCrLf
sPageText = sPageText & "<font color=""red""><b>No data found.</b></font>" & vbCrLf
Else
iTotal = 0
iRowCounter = 0
Dim sPreviousSaleID As String = ""
Dim sCurrentSubject As String
Dim bFirstTime As Boolean = True
' startNewTable
sPageText = StartNewHTMLTable(sPageText, sCurrentSaleReader)


' startNewTable
sPageText = StartNewHTMLTable(sPageText, sCurrentSaleReader)
Do
'sCurrentSale = CStr(oDataReader.Item("SALES_PERSON_ID"))
Dim bDoBreak As Boolean = False
If sCurrentSaleReader <> sPreviousSaleID Then
bDoBreak = True
End If
If bFirstTime Then bDoBreak = False
bFirstTime = False
'**** Note: break must be done before line is put into HTML
If bDoBreak Then
' showBreakData

iRowCounter = 0
sPageText = showBreakData(sPageText, sCurrentSaleReader, iRowCounter, 7)
' startNewTable
sPageText = StartNewHTMLTable(sPageText, sCurrentSaleReader)
End If
'**** Add a line of data to the table
'iRowCounter = iRowCounter + 1
iRowCounter = CInt(iRowCounter + CDbl(oDataReader.Item("UNIT_PRICE")))
iTotal = iTotal + 1
sTableRow = "<tr>"
sTableRow = sTableRow & "<td>" & CStr(oDataReader.Item("SALE_ID")) & "</td>"
sTableRow = sTableRow & "<td>" & CStr(oDataReader.Item("PRODUCT_ID")) & "</td>"
sTableRow = sTableRow & "<td>" & CStr(oDataReader.Item("SALE_DATE")) & "</td>"
sTableRow = sTableRow & "<td>" & CStr(oDataReader.Item("DESCRIPTION")) & "</td>"
sTableRow = sTableRow & "<td>" & CStr(oDataReader.Item("CATEGORY_ID")) & "</td>"
sTableRow = sTableRow & "<td>" & CStr(oDataReader.Item("UNIT_PRICE")) & "</td>"
sTableRow = sTableRow & "<td>" & CStr(oDataReader.Item("QTY")) & "</td>"
sTableRow = sTableRow & "</tr>" & vbCrLf
sPageText = sPageText & sTableRow

sPreviousSaleID = sCurrentSaleReader
Loop While oDataReader.Read()
'**** Now show the final report
' showBreakData
sPageText = showBreakData(sPageText, sCurrentSaleReader, iRowCounter, 7)
End If


sPageText = sPageText & "<br>Number in table: " & CStr(iTotal) & "<br>" & vbCrLf

sPageText = sPageText & g_sHTMLFooter() & vbCrLf


Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
' --- Close down various objects used.
If Not IsNothing(oDataReader) Then oDataReader.Close()
If Not IsNothing(oCmd_Report) Then oCmd_Report.Dispose()
If Not IsNothing(oConn_Report) Then oConn_Report.Close()
End Try
Return sPageText

End Function

Public Function g_sHTMLHeader(ByVal sTitleNameA As String) As String
Dim sHeading As String

sHeading = "<html><head><title>" & sTitleNameA & "</title></head><body>"
sHeading = sHeading & vbCrLf & "<font color=""red""><h2>" & sTitleNameA & "</h2></font><hr>"

Return sHeading
End Function

Public Function g_sHTMLFooter() As String
Dim sTemp As String = "</body>" & vbCrLf & "</html>"
Return sTemp
End Function

Public Function showBreakData(ByVal sPageA As String, ByVal sSubjCodeA As String, _
ByVal iRecordCountA As Integer, _
ByVal iNumberOfCols As Integer) As String
Dim sPage As String = sPageA & "<tr><td valign=""center"" colspan=""" & iNumberOfCols & _
"""><b><font size=""3"" color=""blue"">" & _
"</font> Sub total: " & FormatCurrency(iRecordCountA) & _
"</b></td></tr></table><p>" & vbCrLf
Return sPage
End Function

Public Function StartNewHTMLTable(ByVal sPageA As String, ByVal sCurrentSaleReader As String) As String
Dim sPage As String = "<table border=""1""><tr><th>Sales Person ID: " & sCurrentSaleReader & "</tr></tr><tr><th>Sale ID</th><th>Product ID</th><th>Sale Date</th><th>Description</th><th>Category</th><th>Unit Price</th><th>Quantity</th>" & _
"<th> </th>"
Return sPage
End Function
End Module

Code behind the "Produce Break Report" button

Code:


Private Sub btnBreakReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBreakReport.Click
Dim sCommand As String
Dim sParam As String
Dim sPage As String
Dim oSW_HTMLOutputFile As StreamWriter

Dim oConn_Report As OleDbConnection 'To instantiate Connection obj.
Dim oCmd_Report As OleDbCommand 'To Instantiate a Command obj.
Dim oDataReader As OleDbDataReader 'To instantiate a DataReader obj.
Dim sConnection As String
Dim sPageText As String
Dim sSql As String



Try

sPage = g_HTMLReport(sCurrentSaleReader)
oSW_HTMLOutputFile = New StreamWriter(Application.StartupPath & _
"\SalesBreakReport.html")
If Not (oSW_HTMLOutputFile Is Nothing) Then ' Is the File Open?
oSW_HTMLOutputFile.Write(sPage)
oSW_HTMLOutputFile.Close()
End If

' automatically load internet explorer to show the file created.
sCommand = "IExplore.exe"
sParam = """" & Application.StartupPath & "\SalesBreakReport.html"""
System.Diagnostics.Process.Start(sCommand, sParam)
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & sCommand)
End Try
End Sub

What i would like to happen is, instead of having to individual select each Sales Person ID to create the individual break reports, i want to be able to produce all the break reports and load them by pressing NEXT and PREVIOUS buttons on the Break Report. (as pictured)



Can someone please show me how I can do this?
Thanks a lot in advanced.

Viewing all articles
Browse latest Browse all 27355

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>