' Defining variable
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim SQL As String
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim rs3 As DAO.Recordset
Dim Firsti As Integer
Dim Secondi As Integer
Dim Thirdi As Integer
Dim qdf1 As DAO.QueryDef
Dim qdf2 As DAO.QueryDef
Dim qdf3 As DAO.QueryDef
Dim RowNumber As Integer
Dim Count As Integer
Dim FixedTotal As Integer
'Make sure that we are using the current db
Set db = CurrentDb()
'Set the row counters to zero
Firsti = 0
Secondi = 0
Thirdi = 0
'Set the query definitions
Set qdf1 = db.QueryDefs("Stats SKU Issue Type")
qdf1("[forms]![FrmStatsSpecSKU]![txtSKU]") = [Forms]![FrmStatsSpecSKU]![txtSKU]
'This query definition:[Forms]![FrmStatsSpecSKU]![txtSKU] is also being used in Stats SKU Issue Totals",
'but since it is already defined here, we do not need to do it again.
Set qdf2 = db.QueryDefs("Stats SKU Return Errors")
qdf2("[forms]![FrmStatsSpecSKU]![txtSKU]") = [Forms]![FrmStatsSpecSKU]![txtSKU]
'This query definition:[Forms]![FrmStatsSpecSKU]![txtSKU] is also being used in Stats SKU Issue Totals",
'but since it is already defined here, we do not need to do it again.
Set qdf3 = db.QueryDefs("Stats SKU Resolution")
qdf3("[forms]![FrmStatsSpecSKU]![txtSKU]") = [Forms]![FrmStatsSpecSKU]![txtSKU]
'This query definition:[Forms]![FrmStatsSpecSKU]![txtSKU] is also being used in Stats SKU Issue Totals",
'but since it is already defined here, we do not need to do it again.
'Show user work is being performed
DoCmd.Hourglass (True)
' RETRIEVE DATA
'First Recordset
'SQL statement to retrieve data from database for the first set of data
SQL = "SELECT [Stats SKU Issue Type Percent].[IssueType], [Stats SKU Issue Type Percent].[Stats SKU Issue Type].[CountOfIssueTypeID] FROM [Stats SKU Issue Type Percent]"
'Execute query and populate recordset
Set rs1 = qdf1.OpenRecordset()
'Count the records in the first recordset
Firsti = rs1.RecordCount
'Second Recordset
'SQL statement to retrieve data from database for the first set of data
' SQL = "SELECT [Stats SKU Return Error Percent].[ErrorCodes], [Stats SKU Return Error Percent].[CountOfCallID], [Stats SKU Return Error Percent].[CountOfErrorCodes] FROM [Stats SKU Return Error Percent]"
SQL = "SELECT [Stats SKU Return Error Percent].[ErrorCodes], [Stats SKU Return Error Percent].[CountOfCallID] FROM [Stats SKU Return Error Percent]"
'Execute query and populate recordset
Set rs2 = qdf2.OpenRecordset()
'Count the records in the first recordset
Secondi = rs2.RecordCount
'Third Recordset
'SQL statement to retrieve data from database for the first set of data
' SQL = "SELECT [Stats SKU Resolution Percent].[ResolutionDetails], [Stats SKU Resolution Percent].[Stats SKU Resolution].[CountOfResolutionDetails] FROM [Stats SKU Resolution Percent]"
SQL = "SELECT [Stats SKU Resolution Percent].[ResolutionDetails], [Stats SKU Resolution Percent].[Stats SKU Resolution].[CountOfResolutionDetails] FROM [Stats SKU Resolution Percent]"
'Execute query and populate recordset
Set rs3 = qdf3.OpenRecordset()
'Count the records in the first recordset
Thirdi = rs3.RecordCount
'MsgBox Thirdi
' BUILD SPREADSHEET
'Create an instance of Excel and start building a spreadsheet
'Early Binding
Set xlApp = Excel.Application
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
With xlSheet
.Name = [Forms]![FrmStatsSpecSKU]![txtSKU]
.Cells.Font.Name = "Calibri"
.Cells.Font.Size = 11
'Set column widths
.Columns("A").ColumnWidth = 8.5
.Columns("B").ColumnWidth = 45
.Columns("C").ColumnWidth = 18
.Columns("D").ColumnWidth = 12.5
'Add title
.Range("A1").Value = "Specielle Artikle Statistik"
.Range("A1", "E1").Merge
.Range("A1").HorizontalAlignment = xlCenter
.Range("A1").Cells.Font.Bold = True
.Range("A1").Cells.Font.Underline = True
.Range("A1").RowHeight = 21
.Range("A1").Font.Size = 16
'Add header info
.Range("A3").Value = "Artikel:"
.Range("A3").Cells.Font.Bold = True
.Range("B3").Value = [Forms]![FrmStatsSpecSKU]![txtSKU]
.Range("B3").HorizontalAlignment = xlCenter
.Range("A4").Value = "Date:"
.Range("A4").Cells.Font.Bold = True
.Range("B4").Value = Date
.Range("B4").HorizontalAlignment = xlCenter
.Range("C3").Value = "Erste Reklamation:"
.Range("C3").Cells.Font.Bold = True
.Range("D3").Value = [Forms]![FrmStatsSpecSKU]![txtFirstIssue]
.Range("D3").HorizontalAlignment = xlCenter
.Range("C4").Value = "Letzte Reklamation:"
.Range("C4").Cells.Font.Bold = True
.Range("D4").Value = [Forms]![FrmStatsSpecSKU]![txtLastIssue]
.Range("D4").HorizontalAlignment = xlCenter
.Range("C5").Value = "Reklamation Ges.:"
.Range("C5").Cells.Font.Bold = True
.Range("D5").Value = [Forms]![FrmStatsSpecSKU]![txtTotIssues]
.Range("D5").HorizontalAlignment = xlCenter
.Range("C6").Value = "Kontakte Ges.:"
.Range("C6").Cells.Font.Bold = True
.Range("D6").Value = [Forms]![FrmStatsSpecSKU]![txtTotContacts]
.Range("D6").HorizontalAlignment = xlCenter
.Range("C7").Value = "Average Kont./Rekl."
.Range("C7").Cells.Font.Bold = True
.Range("D7").Value = [Forms]![FrmStatsSpecSKU]![txtContactsAv]
.Range("D7").HorizontalAlignment = xlCenter
'Format the header date fields
.Range("D3").NumberFormat = "d/m/yy"
.Range("D4").NumberFormat = "d/m/yy"
'Add Reklamationsgrund column headers (1st set of data)
.Range("B9").Value = "Reklamationsgrund (Prozentual)"
.Range("B9").Cells.Font.Bold = True
.Range("B10").Value = "Reklamations Art"
.Range("B10").Cells.Font.Bold = True
.Range("B10").HorizontalAlignment = xlCenter
.Range("C10").Value = "Wie Viele?"
.Range("C10").Cells.Font.Bold = True
.Range("C10").HorizontalAlignment = xlCenter
.Range("D10").Value = "% Gesamt"
.Range("D10").Cells.Font.Bold = True
.Range("D10").HorizontalAlignment = xlCenter
.Range("B" & Firsti + 11).Value = "Total"
.Range("B" & Firsti + 11).HorizontalAlignment = xlRight
.Range("B" & Firsti + 11).Cells.Font.Bold = True
'Add first data, from the first recordset
.Range("A11").CopyFromRecordset rs1
'Add calculated values to the first set of data
'Calculate total
Count = 11
Do While Not Count = 11 + Firsti
RecTotal = RecTotal + .Range("C" & Count).Value
Count = Count + 1
Loop
.Range("C" & Firsti + 11).Value = RecTotal
.Range("C" & Firsti + 11).HorizontalAlignment = xlCenter
.Range("C" & Firsti + 11).Cells.Font.Bold = True
'Calculate percent
Count = 11
FixedTotal = Firsti + 11
Do While Not Count = 11 + Firsti
.Range("D" & Count).Formula = "=C" & Count & "/c" & FixedTotal & "* 100"
.Range("D" & Count).Cells.NumberFormat = "0#.#0"
.Range("D" & Count).HorizontalAlignment = xlCenter
.Range("C" & Count).HorizontalAlignment = xlCenter
Count = Count + 1
Loop
'Format fields for the first recordset
'Add headers for second recordset
RowNumber = Firsti + 12
.Range("B" & RowNumber).Value = "Fehlercode (Prozentual)"
.Range("B" & RowNumber).Cells.Font.Bold = True
.Range("B" & RowNumber + 1).Value = "Code"
.Range("B" & RowNumber + 1).Cells.Font.Bold = True
.Range("B" & RowNumber + 1).HorizontalAlignment = xlCenter
.Range("C" & RowNumber + 1).Value = "Wie Viele?"
.Range("C" & RowNumber + 1).Cells.Font.Bold = True
.Range("C" & RowNumber + 1).HorizontalAlignment = xlCenter
.Range("D" & RowNumber + 1).Value = "% Gesamt"
.Range("D" & RowNumber + 1).Cells.Font.Bold = True
.Range("D" & RowNumber + 1).HorizontalAlignment = xlCenter
.Range("B" & Firsti + Secondi + 14).Value = "Total"
.Range("B" & Firsti + Secondi + 14).HorizontalAlignment = xlRight
.Range("B" & Firsti + Secondi + 14).Cells.Font.Bold = True
'Add data from the second recordset
RowNumber = RowNumber + 2
.Range("A" & RowNumber).CopyFromRecordset rs2
'Add calculated values to the second set of data
'Calculate total
Count = Firsti + 14
'MsgBox Count
Do While Not Count = Firsti + 14 + Secondi
ErrorTotal = ErrorTotal + .Range("C" & Count).Value
Count = Count + 1
Loop
.Range("C" & Firsti + 14 + Secondi).Value = ErrorTotal
.Range("C" & Firsti + 14 + Secondi).HorizontalAlignment = xlCenter
.Range("C" & Firsti + 14 + Secondi).Cells.Font.Bold = True
FixedTotal = Firsti + 14 + Secondi
'Calculate percent
Count = Firsti + 14
Do While Not Count = Firsti + 14 + Secondi
' MsgBox Count
.Range("D" & Count).Formula = "=C" & Count & "/c" & FixedTotal & " * 100"
.Range("D" & Count).Cells.NumberFormat = "0#.#0"
.Range("D" & Count).HorizontalAlignment = xlCenter
.Range("C" & Count).HorizontalAlignment = xlCenter
Count = Count + 1
Loop
'Add headers for third recordset
RowNumber = Firsti + Secondi + 15
.Range("B" & RowNumber).Value = "Lösungsvorschläge (Prozentual)"
.Range("B" & RowNumber).Cells.Font.Bold = True
.Range("B" & RowNumber + 1).Value = "Ergebnis"
.Range("B" & RowNumber + 1).Cells.Font.Bold = True
.Range("B" & RowNumber + 1).HorizontalAlignment = xlCenter
.Range("C" & RowNumber + 1).Value = "Wie Viele?"
.Range("C" & RowNumber + 1).Cells.Font.Bold = True
.Range("C" & RowNumber + 1).HorizontalAlignment = xlCenter
.Range("D" & RowNumber + 1).Value = "% Gesamt"
.Range("D" & RowNumber + 1).Cells.Font.Bold = True
.Range("D" & RowNumber + 1).HorizontalAlignment = xlCenter
.Range("B" & Firsti + Secondi + Thirdi + 17).Value = "Total"
.Range("B" & Firsti + Secondi + Thirdi + 17).HorizontalAlignment = xlRight
.Range("B" & Firsti + Secondi + Thirdi + 17).Cells.Font.Bold = True
'Add data from the third recordset
RowNumber = RowNumber + 2
.Range("A" & RowNumber).CopyFromRecordset rs3
'Add calculated values to the third set of data
'Calculate total
Count = Firsti + Secondi + 17
Do While Not Count = Firsti + Secondi + Thirdi + 23
'MsgBox Count
ResolutionTotal = ResolutionTotal + .Range("C" & Count).Value
Count = Count + 1
Loop
.Range("C" & Firsti + 17 + Secondi + Thirdi).Value = ResolutionTotal
.Range("C" & Firsti + 17 + Secondi + Thirdi).HorizontalAlignment = xlCenter
.Range("C" & Firsti + 17 + Secondi + Thirdi).Cells.Font.Bold = True
FixedTotal = Firsti + Secondi + Thirdi + 17
'Calculate percent
Count = Firsti + Secondi + 17
Do While Not Count = Firsti + 17 + Secondi + Thirdi
' MsgBox Count
.Range("D" & Count).Formula = "=C" & Count & "/c" & FixedTotal & " * 100"
.Range("D" & Count).Cells.NumberFormat = "0#.#0"
.Range("D" & Count).HorizontalAlignment = xlCenter
.Range("C" & Count).HorizontalAlignment = xlCenter
Count = Count + 1
Loop
'Delete the values in column A that show the SKU number. Fromm A11 down.
Count = 11
Do While Not Count = Firsti + Secondi + Thirdi + 23
.Range("A" & Count).Cells.Value = ""
Count = Count + 1
Loop
'Add borders and colors to the boxes
'Header Boxes on the left
.Range("B3").Borders(xlEdgeTop).LineStyle = XlLineStyle.xlContinuous
.Range("B3").Borders(xlEdgeTop).Weight = XlBorderWeight.xlMedium
.Range("B3:B4").Borders(xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous
.Range("B3:B4").Borders(xlEdgeLeft).Weight = XlBorderWeight.xlMedium
.Range("B3:B4").Borders(xlEdgeRight).LineStyle = XlLineStyle.xlContinuous
.Range("B3:B4").Borders(xlEdgeRight).Weight = XlBorderWeight.xlMedium
.Range("B4").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous
.Range("B4").Borders(xlEdgeBottom).Weight = XlBorderWeight.xlMedium
.Range("B3:B4").Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
.Range("B3:B4").Interior.Color = RGB(255, 255, 179)
'Header Boxes on the right
.Range("D3").Borders(xlEdgeTop).LineStyle = XlLineStyle.xlContinuous
.Range("D3").Borders(xlEdgeTop).Weight = XlBorderWeight.xlMedium
.Range("D3:D7").Borders(xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous
.Range("D3:D7").Borders(xlEdgeLeft).Weight = XlBorderWeight.xlMedium
.Range("D3:D7").Borders(xlEdgeRight).LineStyle = XlLineStyle.xlContinuous
.Range("D3:D7").Borders(xlEdgeRight).Weight = XlBorderWeight.xlMedium
.Range("D7").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous
.Range("D7").Borders(xlEdgeBottom).Weight = XlBorderWeight.xlMedium
.Range("D3:D7").Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
.Range("D3:D7").Interior.Color = RGB(255, 255, 179)
'First set of data
.Range("b10:d10").Borders(xlEdgeTop).LineStyle = XlLineStyle.xlContinuous
.Range("b10:d10").Borders(xlEdgeTop).Weight = XlBorderWeight.xlMedium
.Range("B10:b" & Firsti + 10).Borders(xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous
.Range("B10:b" & Firsti + 10).Borders(xlEdgeLeft).Weight = XlBorderWeight.xlMedium
.Range("D10:b" & Firsti + 10).Borders(xlEdgeRight).LineStyle = XlLineStyle.xlContinuous
.Range("D10:b" & Firsti + 10).Borders(xlEdgeRight).Weight = XlBorderWeight.xlMedium
.Range("b" & Firsti + 10 & ":D" & Firsti + 10).Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous
.Range("b" & Firsti + 10 & ":D" & Firsti + 10).Borders(xlEdgeBottom).Weight = XlBorderWeight.xlMedium
.Range("B10:D" & Firsti + 10).Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
.Range("B10:D" & Firsti + 10).Borders(xlInsideVertical).LineStyle = XlLineStyle.xlContinuous
.Range("B11:D" & Firsti + 10).Interior.Color = RGB(255, 255, 179)
'Second Set of data
.Range("B" & Firsti + 13 & ":D" & Firsti + 13).Borders(xlEdgeTop).LineStyle = XlLineStyle.xlContinuous
.Range("B" & Firsti + 13 & ":D" & Firsti + 13).Borders(xlEdgeTop).Weight = XlBorderWeight.xlMedium
.Range("B" & Firsti + 13 & ":B" & Firsti + 13 + Secondi).Borders(xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous
.Range("B" & Firsti + 13 & ":B" & Firsti + 13 + Secondi).Borders(xlEdgeLeft).Weight = XlBorderWeight.xlMedium
.Range("D" & Firsti + 13 & ":D" & Firsti + 13 + Secondi).Borders(xlEdgeRight).LineStyle = XlLineStyle.xlContinuous
.Range("D" & Firsti + 13 & ":D" & Firsti + 13 + Secondi).Borders(xlEdgeRight).Weight = XlBorderWeight.xlMedium
.Range("B" & Firsti + 13 + Secondi & ":D" & Firsti + 13 + Secondi).Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous
.Range("B" & Firsti + 13 + Secondi & ":D" & Firsti + 13 + Secondi).Borders(xlEdgeBottom).Weight = XlBorderWeight.xlMedium
.Range("B" & Firsti + 13 & ":D" & Firsti + 13 + Secondi).Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
.Range("B" & Firsti + 13 & ":D" & Firsti + 13 + Secondi).Borders(xlInsideVertical).LineStyle = XlLineStyle.xlContinuous
.Range("B" & Firsti + 14 & ":D" & Firsti + 13 + Secondi).Interior.Color = RGB(255, 255, 179)
'Third Set of data
.Range("B" & Firsti + 16 + Secondi & ":D" & Firsti + 16 + Secondi).Borders(xlEdgeTop).LineStyle = XlLineStyle.xlContinuous
.Range("B" & Firsti + 16 + Secondi & ":D" & Firsti + 16 + Secondi).Borders(xlEdgeTop).Weight = XlBorderWeight.xlMedium
.Range("B" & Firsti + 16 + Secondi & ":B" & Firsti + 16 + Secondi + Thirdi).Borders(xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous
.Range("B" & Firsti + 16 + Secondi & ":B" & Firsti + 16 + Secondi + Thirdi).Borders(xlEdgeLeft).Weight = XlBorderWeight.xlMedium
.Range("D" & Firsti + 16 + Secondi & ":D" & Firsti + 16 + Secondi + Thirdi).Borders(xlEdgeRight).LineStyle = XlLineStyle.xlContinuous
.Range("D" & Firsti + 16 + Secondi & ":D" & Firsti + 16 + Secondi + Thirdi).Borders(xlEdgeRight).Weight = XlBorderWeight.xlMedium
.Range("B" & Firsti + 16 + Secondi + Thirdi & ":D" & Firsti + 16 + Secondi + Thirdi).Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous
.Range("B" & Firsti + 16 + Secondi + Thirdi & ":D" & Firsti + 13 + Secondi + Thirdi).Borders(xlEdgeBottom).Weight = XlBorderWeight.xlMedium
.Range("B" & Firsti + 16 + Secondi & ":D" & Firsti + 16 + Secondi + Thirdi).Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
.Range("B" & Firsti + 16 + Secondi & ":D" & Firsti + 16 + Secondi + Thirdi).Borders(xlInsideVertical).LineStyle = XlLineStyle.xlContinuous
.Range("B" & Firsti + 17 + Secondi & ":D" & Firsti + 16 + Secondi + Thirdi).Interior.Color = RGB(255, 255, 179)
'Border Around the outside of everything
.Range("A1:E1").Borders(xlEdgeTop).LineStyle = XlLineStyle.xlContinuous
.Range("A1:E1").Borders(xlEdgeTop).Weight = XlBorderWeight.xlMedium
.Range("A1:A" & Firsti + Secondi + Thirdi + 18).Borders(xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous
.Range("A1:A" & Firsti + Secondi + Thirdi + 18).Borders(xlEdgeLeft).Weight = XlBorderWeight.xlMedium
.Range("A" & Firsti + Secondi + Thirdi + 18 & ":E" & Firsti + Secondi + Thirdi + 18).Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous
.Range("A" & Firsti + Secondi + Thirdi + 18 & ":E" & Firsti + Secondi + Thirdi + 18).Borders(xlEdgeBottom).Weight = XlBorderWeight.xlMedium
.Range("E1:E" & Firsti + Secondi + Thirdi + 18).Borders(xlEdgeRight).LineStyle = XlLineStyle.xlContinuous
.Range("E1:E" & Firsti + Secondi + Thirdi + 18).Borders(xlEdgeRight).Weight = XlBorderWeight.xlMedium
End With
SubExit:
DoCmd.Hourglass False
xlApp.Visible = True
rs1.Close
Set rs1 = Nothing
Exit Sub