'******Create and export the "Orders" records
'export the results of the "ForecastOrders_qry" query to the new Excel file
'when the data is actully exported, a new worksheet named for the query used
'in the export is created and the data is placed on this new worksheet
'Code used later, moves this data to the appropriate worksheet and formats it
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "ForecastOrders_qry", strNewPathAndFileName, True
'update the progress bar - 7
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
Me.lblUserInfo.Caption = "Creating ""SSP"" information for Fleet Forecast!"
Me.Repaint
DoEvents
'******Create and export the "SSP" records
'export the results of the "ForecastSSP_qry" query to the new Excel file
'when the data is actully exported, a new worksheet named for the query used
'in the export is created and the data is placed on this new worksheet
'Code used later, moves this data to the appropriate worksheet and formats it
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "ForecastSSP_qry", strNewPathAndFileName, True
'update the progress bar - 8
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
Me.lblUserInfo.Caption = "Creating ""Removals"" information for Fleet Forecast!"
Me.Repaint
DoEvents
'******Create and export the "Forecast Removals" records
'export the results of the "ForecastRemovals_qry" query to the new Excel file
'when the data is actully exported, a new worksheet named for the query used
'in the export is created and the data is placed on this new worksheet
'Code used later, moves this data to the appropriate worksheet and formats it
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "ForecastRemovals_qry", strNewPathAndFileName, True
DoEvents
'update the progress bar - 9
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
'******Create and export the "Forecast Inventory" records
'process the "Inventory" exported records here
Me.lblUserInfo.Caption = "Creating the ""Inventory"" data to the Fleet Forecast!"
Me.Repaint
DoEvents
'at this point, the new "tblFleetForecastInv" table has been created, and populated with data
'remove any records where there is a zero for every reporting month
'read the field names from the "tblFleetForecastInv" table into the "FldNames" array variable
'Note: The month field names are created programatically above
Set TblRs = CurrentDb.OpenRecordset("tblFleetForecastInv")
cntr = 1
For Each fld In TblRs.Fields
If fld.Name <> "Zone" And fld.Name <> "Type" And fld.Name <> "Make" And fld.Name <> "Model" Then
FldNames(cntr) = fld.Name
'Debug.Print FldNames(cntr)
cntr = cntr + 1
End If
Next fld
'create the "Delete" type sql statement using the value in the array variable for field names
strSql = "DELETE * FROM tblFleetForecastInv " _
& "WHERE (((tblFleetForecastInv.[" & FldNames(1) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(2) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(3) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(4) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(5) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(6) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(7) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(8) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(9) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(10) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(11) & "]) Is Null) " _
& "AND ((tblFleetForecastInv.[" & FldNames(12) & "]) Is Null));"
'run the delete query
CurrentDb.Execute strSql
'Export the "Inventory" records to the Excel workbook
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "qryFleetForcastInvExport", strNewPathAndFileName, True
'update the progress bar - 10
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
With Me.lblUserInfo
.Caption = "Creating new Fleet Forecast Workbook!"
.Visible = True
End With
'open the Excel spreadsheet with the exported data
Set objXLApp = CreateObject("Excel.Application")
DoEvents
Set objXlBook = objXLApp.Workbooks.Open(strNewPathAndFileName)
DoEvents
'update the progress bar -11
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
With Me.lblUserInfo
.Caption = "Moving the ""Orders"" data to the ""Orders"" worksheet!"
.Visible = True
End With
'With the Workbook open:
'***MOVE THE "ORDERS" EXPORTED DATA
'set focus to the sheet that was created when the query was exported
'the name of the sheet will always be the name of the query that was used to do the export
'Sheets("ForecastOrders_qry").Select
Set objXLSheet = objXlBook.Sheets("ForecastOrders_qry")
DoEvents
'find the last used cell in Column "A" (Center)
LastRow = objXLSheet.Range("A65536").End(xlUp).Row
'select and copy all of the data that was exported
objXLSheet.Range("A1:P" & LastRow).Copy
'make the "Orders" worksheet the active worksheet
Set objXLSheet = objXlBook.Sheets("Orders")
'paste in only the values copied from the "ForecastOrders_qry" worksheet
objXLSheet.Range("B2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'delete the worksheet where the data was orginally places
objXLApp.DisplayAlerts = False
objXlBook.Sheets("ForecastOrders_qry").Delete
objXLApp.DisplayAlerts = True
'update the progress bar -12
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
With Me.lblUserInfo
.Caption = "Moving the ""SSP"" data to the ""Orders"" worksheet!"
.Visible = True
End With
Me.Repaint
DoEvents
'***MOVE THE "SSP" EXPORTED DATA
'set focus to the sheet that was created when the query was exported
'the name of the sheet will always be the name of the query that was used to do the export
'objXlBook.Sheets("ForecastSSP_qry").Select
'make the "ForecastSSP_qry" worksheet to be the active worksheet
Set objXLSheet = objXlBook.Sheets("ForecastSSP_qry")
objXLSheet.Activate
DoEvents
'find the last used cell in Column "A" (Center)
LastRow = objXLSheet.Range("A65536").End(xlUp).Row
'select and copy all of the data that was exported
objXLSheet.Range("A2:P" & LastRow).Copy
'make the "Orders" worksheet to be the active worksheet
Set objXLSheet = objXlBook.Sheets("Orders")
objXLSheet.Activate
'find the last used cell in Column "B" (Center)
LastRow = objXLSheet.Range("B65536").End(xlUp).Row
objXLSheet.Range("B" & LastRow + 1).Select
'set the focus to the next available cell in column "B" in the "Orders" workwheet
objXLSheet.Range("B" & LastRow + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'delete the worksheet where the data was orginally placed
objXLApp.DisplayAlerts = False
objXlBook.Sheets("ForecastSSP_qry").Delete
objXLApp.DisplayAlerts = True
'When the exported Orders and SSP records have all been moved to the "Orders" worksheet
Me.lblUserInfo.Caption = "Formatting the ""Orders"" worksheet of the Fleet Forecast workbook!"
Me.Repaint
DoEvents
'make the "Orders" worksheet to be the active worksheet
Set objXLSheet = objXlBook.Sheets("Orders")
'find the last used cell in Column "B" (Center)
LastRow = objXLSheet.Range("B65536").End(xlUp).Row
'set the value of the "StartRow" variable to be used in a "FormulaR1C1" type formula
StartRow = LastRow - 2
'update the progress bar -13
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
With Me.lblUserInfo
.Caption = "Formatting the ""Orders"" worksheet!"
.Visible = True
End With
'format the column headings
For c = 6 To 17
'reformat the YR-Mo Colunm name
strOrgDateVal = objXLSheet.Cells(2, c).Value
objXLSheet.Cells(2, c).Value = FormatColName(strOrgDateVal)
Next c
'apply center formatting to all cells
objXLSheet.Range("B3:R" & LastRow + 1).HorizontalAlignment = xlCenter
'show the cell borders
With objXLSheet.Range("B3:R" & LastRow + 1).Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
'update the progress bar -14
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
'make all blank cells to have a zero instead of just blank
For c = 6 To 17
For i = 3 To LastRow
If IsEmpty(objXLSheet.Cells(i, c).Value) Then
objXLSheet.Cells(i, c).Value = 0
End If
Next i
'add the TOTALS formula below the last row
objXLSheet.Cells(i, c).FormulaR1C1 = "=SUM(R[-" & StartRow & "]C:R[-1]C)"
Next c
'Add TOTALS text and formatting
objXLSheet.Range("B" & LastRow + 1).Value = "TOTALS"
With objXLSheet.Range("B" & LastRow + 1 & ":E" & LastRow + 1)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.MergeCells = True
.Font.Bold = True
.Borders.TintAndShade = -0.14996795556505
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThin
End With
'Bold the range where the count totals are and make it bold
With objXLSheet.Range("B" & LastRow + 1 & ":R" & LastRow + 1).Font
.FontStyle = "Bold"
End With
'format the borders
With objXLSheet.Range("B" & LastRow + 1 & ":R" & LastRow + 1).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
'.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.14996795556505
.PatternTintAndShade = 0
End With
With objXLSheet.Range("B" & LastRow + 1 & ":R" & LastRow + 1).Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
'set focus to "A1"
objXLSheet.Range("A1").Select
'update the progress bar -15
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
With Me.lblUserInfo
.Caption = "Formatting the ""Removals"" worksheet!"
.Visible = True
End With
Me.Repaint
DoEvents
'***MOVE AND FORMAT THE "REMOVALS" EXPORTED DATA
'set focus to the sheet that was created when the query was exported
'the name of the sheet will always be the name of the query that was used to do the export
Set objXLSheet = objXlBook.Sheets("ForecastRemovals_qry")
DoEvents
'find the last used cell in Column "A" (Center)
LastRow = objXLSheet.Range("A65536").End(xlUp).Row
'select and copy all of the data that was exported
objXLSheet.Range("A1:P" & LastRow).Copy
'make the "Removals" worksheet the active worksheet
Set objXLSheet = objXlBook.Sheets("Removals")
'paste the values copied from the "ForecastOrders_qry" worksheet starting in "B2"
objXLSheet.Range("B2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'delete the worksheet where the data was orginally places
objXLApp.DisplayAlerts = False
objXlBook.Sheets("ForecastRemovals_qry").Delete
objXLApp.DisplayAlerts = True
'make the "Removals" worksheet the active worksheet
Set objXLSheet = objXlBook.Sheets("Removals")
'find the last used cell in Column "B" (Center)
LastRow = objXLSheet.Range("B65536").End(xlUp).Row
'set the value of the "StartRow" variable to be used in a "FormulaR1C1" type formula
StartRow = LastRow - 2
'Set the format of the column headings
For c = 6 To 17
'reformat the YR-Mo Colunm name
strOrgDateVal = objXLSheet.Cells(2, c).Value
objXLSheet.Cells(2, c).Value = FormatColName(strOrgDateVal)
Next c
'apply center formatting to all cells
objXLSheet.Range("B3:R" & LastRow + 1).HorizontalAlignment = xlCenter
With objXLSheet.Range("B3:R" & LastRow + 1).Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
'update the progress bar -16
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
'make all blank cells to have a zero instead of just blank
For c = 6 To 17
For i = 3 To LastRow
If IsEmpty(objXLSheet.Cells(i, c).Value) Then
objXLSheet.Cells(i, c).Value = 0
End If
Next i
'add the TOTALS formula
objXLSheet.Cells(i, c).FormulaR1C1 = "=SUM(R[-" & StartRow & "]C:R[-1]C)"
Next c
'Add TOTALS text and formatting
objXLSheet.Range("B" & LastRow + 1).Value = "TOTALS"
With objXLSheet.Range("B" & LastRow + 1 & ":E" & LastRow + 1)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.MergeCells = True
.Font.Bold = True
.Borders.TintAndShade = -0.14996795556505
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThin
End With
'select the range where the count totals are and make it bold
With objXLSheet.Range("B" & LastRow + 1 & ":R" & LastRow + 1).Font
.FontStyle = "Bold"
End With
With objXLSheet.Range("B" & LastRow + 1 & ":R" & LastRow + 1).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
'.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.14996795556505
.PatternTintAndShade = 0
End With
With objXLSheet.Range("B" & LastRow + 1 & ":R" & LastRow + 1).Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
'make the "Removals" worksheet the active worksheet
Set objXLSheet = objXlBook.Sheets("Removals")
objXLSheet.Activate
'set focus to "A1"
objXLSheet.Range("A1").Select
'update the progress bar -17
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
'process the "Inventory" exported records here
With Me.lblUserInfo
.Caption = "Moving the ""Inventory"" data to the ""Inventory"" worksheet!"
.Visible = True
End With
Me.Repaint
DoEvents
'With the Workbook open:
'***MOVE THE "INVENTORY" EXPORTED DATA
'set focus to the sheet that was created when the query was exported
'the name of the sheet will always be the name of the query or table that was used to do the export
'make the "qryFleetForcastInvExport" worksheet as the active worksheet
Set objXLSheet = objXlBook.Sheets("qryFleetForcastInvExport")
DoEvents
'find the last used cell in Column "A" (Center)
LastRow = objXLSheet.Range("A65536").End(xlUp).Row
'select and copy all of the data that was exported
objXLSheet.Range("A1:P" & LastRow).Copy
'make the "Inventory" worksheet the active worksheet
Set objXLSheet = objXlBook.Sheets("Inventory")
'paste in only the values copied from the "ForecastOrders_qry" worksheet
objXLSheet.Range("B2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'delete the worksheet where the data was orginally places
objXLApp.DisplayAlerts = False
objXlBook.Sheets("qryFleetForcastInvExport").Delete
objXLApp.DisplayAlerts = True
'When the exported Inventory records have all been moved to the "Inventory" worksheet
'format the Inventory Worksheet
'update the progress bar -18
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
With Me.lblUserInfo
.Caption = "Formatting the ""Inventory"" worksheet!"
.Visible = True
End With
'make the "Inventory" worksheet as the active worksheet
Set objXLSheet = objXlBook.Sheets("Inventory")
'find the last used cell in Column "B" (Center)
LastRow = objXLSheet.Range("B65536").End(xlUp).Row
'set the value of the "StartRow" variable to be used in a "FormulaR1C1" type formula
StartRow = LastRow - 2
'change the format of the column headers
For c = 6 To 17
'reformat the YR-Mo Colunm name
strOrgDateVal = objXLSheet.Cells(2, c).Value
objXLSheet.Cells(2, c).Value = FormatColName(strOrgDateVal)
Next c
objXLSheet.Range("B3:Q" & LastRow + 1).HorizontalAlignment = xlCenter
With objXLSheet.Range("B3:Q" & LastRow + 1).Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
'update the progress bar -19
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
'make all blank cells to have a zero instead of just blank
For c = 6 To 17
For i = 3 To LastRow
If IsEmpty(objXLSheet.Cells(i, c).Value) Then
objXLSheet.Cells(i, c).Value = 0
End If
Next i
'add the TOTALS formula
objXLSheet.Cells(i, c).FormulaR1C1 = "=SUM(R[-" & StartRow & "]C:R[-1]C)"
Next c
'Add TOTALS text and formatting
objXLSheet.Range("B" & LastRow + 1).Value = "TOTALS"
With objXLSheet.Range("B" & LastRow + 1 & ":E" & LastRow + 1)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.MergeCells = True
.Font.Bold = True
.Borders.TintAndShade = -0.14996795556505
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThin
End With
'select the range where the count totals are and make it bold
With objXLSheet.Range("B" & LastRow + 1 & ":Q" & LastRow + 1).Font
.FontStyle = "Bold"
End With
'format the borders
With objXLSheet.Range("B" & LastRow + 1 & ":Q" & LastRow + 1).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
'.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.14996795556505
.PatternTintAndShade = 0
End With
With objXLSheet.Range("B" & LastRow + 1 & ":Q" & LastRow + 1).Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
'set focus to "A1"
'make the "Inventory" worksheet as the active worksheet
Set objXLSheet = objXlBook.Sheets("Inventory")
objXLSheet.Activate
objXLSheet.Range("A1").Select
'when the processing is finished
''make the "Orders" worksheet as the active worksheet
Set objXLSheet = objXlBook.Sheets("Orders")
'update the progress bar -20
Me.rectProgressBar.Width = (intTotalWidth / intProgressBarMax) * intCurrentProgress
intCurrentProgress = intCurrentProgress + 1
With Me.lblUserInfo
.Caption = "Saving Fleet Forecast workbook!"
.Visible = True
End With
Me.Repaint
DoEvents
'save the changes
objXlBook.Save