Code:
Option Explicit
Option Compare Database
Private Sub Form_Load()
Me.fraDate.Visible = False
Me.txtDate.Visible = False
End Sub
Private Sub fraReport_AfterUpdate()
Me.cboConstraint.RowSource = ""
Select Case Me.fraReport.Value
Case 1
Me.cboConstraint.AddItem "Work Order #"
Me.cboConstraint.AddItem "Quality #"
Case 2
Me.cboConstraint.AddItem "Event Type"
Me.cboConstraint.AddItem "Event Category"
Me.cboConstraint.AddItem "Work Area/Cell"
Case 3
Me.cboConstraint.AddItem "Specific Date to Present"
Me.cboConstraint.AddItem "Work Area/Cell"
Me.cboConstraint.AddItem "Event Type"
Me.cboConstraint.AddItem "Event Category"
Case 4
Me.cboConstraint.AddItem "6 Month Trend"
Me.cboConstraint.AddItem "12 Month Trend"
Me.cboConstraint.AddItem "24 Month Trend"
Me.fraConValue.Visible = False
Me.cboConValue.Visible = False
End Select
Me.cboConstraint.Value = ""
End Sub
Private Sub cboConstraint_AfterUpdate()
Dim strTrendSQL As String
Dim strParetoSQL As String
Dim strConEventReportSQL As String
Dim strConEventSummarySQL As String
strParetoSQL = ""
strConEventReportSQL = ""
strConEventSummarySQL = ""
Me.cboConValue.RowSource = ""
If Me.cboConstraint.Value = "Specific Date to Present" Then
Me.fraConValue.Visible = False
Me.cboConValue.Visible = False
Me.txtDate.Visible = True
Me.fraDate.Visible = True
Else
Me.fraConValue.Visible = True
Me.cboConValue.Visible = True
Me.txtDate.Visible = False
Me.fraDate.Visible = False
End If
Select Case Me.fraReport.Value
Case 1
Select Case Me.cboConstraint.Value
Case "Work Order #"
strConEventReportSQL = "SELECT RCAData1.WorkOrderNo, RCAData1.QualityNo FROM RCAData1 WHERE WorkOrderNo IS NOT NULL ORDER BY RCAData1.DefectDate;"
Me.cboConValue.RowSource = strConEventReportSQL
Me.cboConValue.Requery
Case "Quality #"
strConEventReportSQL = "SELECT RCAData1.QualityNo, RCADAta1.WorkOrderNo FROM RCAdata1 WHERE (QualityNo IS NOT NULL) ORDER BY RCAData1.DefectDate;"
Me.cboConValue.RowSource = strConEventReportSQL
Me.cboConValue.Requery
End Select
Case 2
Select Case Me.cboConstraint.Value
Case "Event Type"
strConEventSummarySQL = "SELECT DISTINCT RCAData1.Type FROM RCAData1;"
Me.cboConValue.ColumnCount = 1
Me.cboConValue.RowSource = strConEventSummarySQL
Me.cboConValue.Requery
Case "Event Category"
strConEventSummarySQL = "SELECT DISTINCT RCAData1.Category FROM RCAData1;"
Me.cboConValue.ColumnCount = 1
Me.cboConValue.RowSource = strConEventSummarySQL
Me.cboConValue.Requery
Case "Work Area/Cell"
strConEventSummarySQL = "SELECT DISTINCT RCAData1.AreaCell FROM RCAData1;"
Me.cboConValue.ColumnCount = 1
Me.cboConValue.RowSource = strConEventSummarySQL
Me.cboConValue.Requery
End Select
Case 3
Call ExportRecordsetToExcel
Case 4
Call ExportRecordsetToExcel
End Select
End Sub
Public Sub cboConValue_AfterUpdate()
Dim strEventReportSQL As String
Dim strEventSummarySQL As String
Select Case Me.fraReport.Value
Case 1
Select Case Me.cboConstraint.Value
Case 1
strEventReportSQL = "SELECT * FROM RCAData1 WHERE RCAData.WorkOrderNo = '" & Me.cboConValue.Value & "';"
Case 2
strEventReportSQL = "SELECT * FROM RCAData1 WHERE RCAData1.QualityNo = '" & Me.cboConValue.Value & "';"
End Select
Case 2
Select Case Me.cboConstraint.Value
Case 1
strEventSummarySQL = "SELECT * FROM RCAData1 WHERE RCAData1.Type = '" & Me.cboConValue.Value & "';"
Case 2
strEventSummarySQL = "SELECT * FROM RCAData1 WHERE RCAData1.Category = '" & Me.cboConValue.Value & "';"
Case 3
strEventSummarySQL = "SELECT * FROM RCAData1 WHERE RCAData1.AreaCell = '" & Me.cboConValue.Value & "';"
End Select
End Select
End Sub
Public Sub cmdRptPrint_Click()
On Error Resume Next
Select Case Me.fraConValue.Value
Case 1
DoCmd.OpenReport "RCAEventReport", acViewReport, strEventReportSQL
DoCmd.Close acForm, Me.Name, acSaveNo
DoCmd.SelectObject acReport, "RCAEventReport"
DoCmd.RunCommand acCmdPrint
If Err.Number <> 0 And Err.Number <> 2501 Then
MsgBox Err.Number & ":" & Err.Description, vbCritical
End If
Case 2
DoCmd.OpenReport "RCASummaryReport", acViewReport, strEventSummarySQL
DoCmd.Close acForm, Me.Name, acSaveNo
DoCmd.SelectObject acReport, "RCASummaryReport"
DoCmd.RunCommand acCmdPrint
If Err.Number <> 0 And Err.Number <> 2501 Then
MsgBox Err.Number & ":" & Err.Description, vbCritical
End If
Case 3
DoCmd.OpenReport "ParetoChart", acViewReport
DoCmd.Close acForm, Me.Name, acSaveNo
DoCmd.SelectObject acReport, "objSummaryChart"
DoCmd.RunCommand acCmdPrint
If Err.Number <> 0 And Err.Number <> 2501 Then
MsgBox Err.Number & ":" & Err.Description, vbCritical
End If
Case 4
DoCmd.OpenReport "ParetoChart", acViewReport
DoCmd.Close acForm, Me.Name, acSaveNo
DoCmd.SelectObject acReport, "objSummaryChart"
DoCmd.RunCommand acCmdPrint
If Err.Number <> 0 And Err.Number <> 2501 Then
MsgBox Err.Number & ":" & Err.Description, vbCritical
End If
End Select
End Sub
Private Sub cmdRptPreview_Click()
Select Case Me.fraConValue.Value
Case 1
DoCmd.OpenReport "RCAEventReport", acViewPreview, strEventReportSQL
DoCmd.Close acForm, Me.Name, acSaveNo
Case 2
DoCmd.OpenReport "RCASummaryReport", acViewPreview, strEventSummarySQL
DoCmd.Close acForm, Me.Name, acSaveNo
Case 3
DoCmd.OpenReport "Pareto Chart", acViewPreview
DoCmd.Close acForm, Me.Name, acSaveNo
Case 4
DoCmd.OpenReport "TrendChart", acViewPreview
DoCmd.Close acForm, Me.Name, acSaveNo
End Select
End Sub
'Export Pareto or Trend data to Excel for Charting
Sub ExportRecordsetToExcel()
Dim strParetoSQL As String
Dim strTrendSQL As String
strParetoSQL = ""
strTrendSQL = ""
Select Case Me.fraReport.Value
Case 3
DoCmd.SetWarnings False
DoCmd.RunSQL ("DELETE ChartTable.* FROM ChartTable")
Call ClearParetoData
Select Case Me.cboConstraint.Value
Case "Specific Date to Present"
strParetoSQL = "SELECT COUNT(*) INTO ChartTable FROM RCAData1 WHERE '" & Me.txtDate & "' < DefectDate GROUP BY month(DefectDate);"
Case "Work Area/Cell"
strParetoSQL = "INSERT INTO ChartTable (Field1, Field2, Field3, Field4) SELECT COUNT (*) FROM RCAData1 GROUP BY AreaCell;"
Case "Event Type"
strParetoSQL = "SELECT COUNT (*) INTO ChartTAble FROM RCAData1 GROUP BY Type;"
Case "Event Category"
strParetoSQL = "SELECT COUNT (*) INTO ChartTable FROM RCAData1 GROUP BY Category"
End Select
DoCmd.SetWarnings True
DoCmd.RunSQL (strParetoSQL)
Call SendParetoData
Case 4
DoCmd.SetWarnings False
DoCmd.RunSQL ("DELETE ChartTable.* FROM ChartTable")
Call ClearTrendData
Select Case Me.cboConstraint.Value
Case "6 Month Trend"
strTrendSQL = "SELECT COUNT(*) INTO ChartTable FROM RCAData1 WHERE DefectDate > dateadd(mm,-6,getdate());"
Case "12 Month Trend"
strTrendSQL = "SELECT COUNT(*) INTO ChartTable FROM RCAData1 WHERE DefectDate > dateadd(mm,-12,getdate());"
Case "24 Month Trend"
strTrendSQL = "SELECT COUNT(*) INTO ChartTable FROM RCAData1 WHERE DefectDate > dateadd(mm,-24,getdate());"
End Select
DoCmd.SetWarnings True
DoCmd.RunSQL (strTrendSQL)
Call SendTrendData
End Select
End Sub
'Clears the pareto data worksheet in Excel
Function ClearParetoData()
Dim oXL As Object
Dim oBook As Object
Dim oSheet As Object
Set oXL = CreateObject("Excel.Application")
On Error Resume Next
oXL.UserControl = True
On Error GoTo 0
On Error GoTo ErrHandle
Set oBook = oXL.Workbooks.Open("E:\RCADatabase(FE)\RCACharting.xls")
Set oSheet = oBook.ParetoData
oSheet.Range("A1:E50").Select
oXL.Selection.ClearContents
oBook.Save
ErrExit:
oBook.Close
oXL.Application.Quit
Set oXL = Nothing
Set oBook = Nothing
Set oSheet = Nothing
Exit Function
ErrHandle:
oXL.Visible = False
MsgBox Err.Description
GoTo ErrExit
End Function
'Clears the trend dataworksheet in Excel
Function ClearTrendData()
Dim oXL As Object
Dim oBook As Object
Dim oSheet As Object
Set oXL = CreateObject("Excel.Application")
On Error Resume Next
oXL.UserControl = True
On Error GoTo 0
On Error GoTo ErrHandle
Set oBook = oXL.Workbooks.Open("E:\RCADatabase(FE)\RCACharting.xls")
Set oSheet = oBook.TrendData
oSheet.Range("A1:E50").Select
oXL.Selection.ClearContents
oBook.Save
ErrExit:
oBook.Close
oXL.Application.Quit
Set oXL = Nothing
Set oBook = Nothing
Set oSheet = Nothing
Exit Function
ErrHandle:
oXL.Visible = False
MsgBox Err.Description
GoTo ErrExit
End Function
Public Function SendParetoData()
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As DAO.Field
Dim strPath As String
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
strPath = "E:\RCADatabase(FE)\RCACharting.xls"
On Error GoTo err_handler
Set rst = CurrentDb.OpenRecordset("ChartTable")
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(strPath)
ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets("ParetoData")
xlWSh.Activate
xlWSh.Range("A1").Select
For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("A2").CopyFromRecordset rst
xlWSh.Range("1:1").Select
rst.Close
Set rst = Nothing
Exit Function
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Function
End Function
Public Function SendTrendData()
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As DAO.Field
Dim strPath As String
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
On Error GoTo err_handler
strPath = "E:\RCADatabase(FE)\RCACharting.xls"
Set rst = CurrentDb.OpenRecordset("ChartTable")
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(strPath)
ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets("TrendData")
xlWSh.Activate
xlWSh.Range("A1").Select
For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("A2").CopyFromRecordset rst
xlWSh.Range("1:1").Select
rst.Close
Set rst = Nothing
Exit Function
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Function
End Function