MikeWaller
New Member
- Joined
- Jan 28, 2013
- Messages
- 5
Hello All,
I've got two functions(actually a lot more than that, but if I can get help figuring these out I think I can handle the rest),one to retrieve data from an SQL database and one to fill the data. Originally, I had it using a static range for date and times and then filling to a static cell. I think I may have the loop for the retrieve, but I can not, for the life of me, figure out how to loop the fill part.
Here is the function for retrieving so far:
And here is the fill function. nCount is the data I am trying to fill into ranges C10:N10 and P10:AA10 on a sheet called "MON PROD" based on the time ranges from the previous code. So far I have tried multiple things and can only get the value for the last time range into C10 on "MON PROD".
I've got two functions(actually a lot more than that, but if I can get help figuring these out I think I can handle the rest),one to retrieve data from an SQL database and one to fill the data. Originally, I had it using a static range for date and times and then filling to a static cell. I think I may have the loop for the retrieve, but I can not, for the life of me, figure out how to loop the fill part.
Here is the function for retrieving so far:
Code:
Private Function GetDataPT() As Boolean
On Error GoTo ERROR_TRAP
Dim sStartCSN As String
Dim sEndCSN As String
Dim dtStartDate As String
Dim dtEndDate As String
Dim sSQL As String
Dim ProcStateUserID As String
Dim LastCol As Integer
Dim FirstCol As Integer
Dim ws As Worksheet
For FirstCol = 1 To LastCol
LastCol = Worksheets("Settings").Cells(19, Columns.Count).End(xlToLeft).Column
If LastCol < 2 Then LastCol = 2
Set ws = ThisWorkbook.Worksheets(8)
'Display Status
ws.Cells(3, 6) = "Getting Data"
GetDataPT = False
'Create Query
sSQL = "SELECT CAST(CSN as varchar(25)) as CSN, CAST(VIN as varchar(25)) as VIN, ContID, ProcDefName, ProcStateStart, ProcStateEnd, ProcGrpName, ProcStateUserID, ProcStateTranType, ProcStateComp"
sSQL = sSQL & " FROM vblReportIMProcStateWithContAttrib"
'Time ranges are A19-B19,B19-C19,C19-D19 etc.
' Start Time is in A19 - V19 on Run Worksheet
dtStartDate = Worksheets("Settings").Cells(19, LastCol - 1)
' End Time is in B19 - W19 on Run Worksheet
dtEndDate = Worksheets("Settings").Cells(19, LastCol)
If dtStartDate <> #12:00:00 AM# Then
If dtEndDate <> #12:00:00 AM# Then 'apply both
sSQL = sSQL & " WHERE ProcStateEnd >= '" & dtStartDate & "' AND ProcStateEnd <= '" & dtEndDate & "'"
Else 'apply just start
sSQL = sSQL & " WHERE ProcStateEnd = '" & dtStartDate & "'"
End If
sSQL = sSQL & " AND ProcGrpName in ('PowerTrain')"
sSQL = sSQL & " AND ProcStateComp = 'YES'"
sSQL = sSQL & " AND ProcStateTranType = 'Completed'"
sSQL = sSQL & " AND ProcDefName <> 'SMSSV'"
sSQL = sSQL & " AND ProcDefName <> 'SMRSV'"
sSQL = sSQL & " AND ProcDefName <> 'SMSSV2'"
sSQL = sSQL & " AND ProcDefName <> 'RSSSV'"
sSQL = sSQL & " AND ProcDefName = 'PT002L'"
sSQL = sSQL & " ORDER BY CSN"
Set moRSWips = New Recordset
moRSWips.Open sSQL, moDBConn
Set moRSWips.ActiveConnection = Nothing
GetDataPT = True
Else
MsgBox "No Start Date Entered", vbInformation, "GetDataPT"
End If
Next FirstCol
Exit_Function:
Exit Function
ERROR_TRAP:
Select Case Err.Number
Case 0
Case Else
DisplayError msModName & "GetDataPT", Err.Number, True, Err.Description
Err.Clear
Resume Exit_Function
End Select
End Function
Code:
Private Sub FillSheetPT()
On Error GoTo ERROR_TRAP
Dim ws As Worksheet
Dim nRow As Integer 'Column
Dim nCol As Integer 'Row
Dim sCSN As String 'CSN
Dim sVIN As String 'VIN
Dim sStation As String
Dim sProcessGroup As String
Dim sCSNold As String
Dim sCSNnew As String
Dim sCSNInput As String
Dim sWIP As String
Dim sUserID As String
Dim nCount As Integer 'Unique CSN Count
Dim nLoop As Integer
Dim ShiftStart As Date
Dim sDateEnd As String
Dim sStartTime As String
Dim nRowStart As Integer
Dim nColStart As Integer
Dim ProcStateUserID As String
Dim bDateDisplayed As Boolean
Set ws = ThisWorkbook.Worksheets(9)
'Deactivate Automatic Worksheet Recalculation
Application.Calculation = xlCalculationManual
nRowStart = 2
nColStart = 3
'Display Status
Me.Cells(3, 6) = "Initialize Sheet"
ShiftStart = Worksheets("Run Report").Cells(4, 2)
sProcessGroup = moRSWips!ProcGrpName
'Get Distinct Column Count
nCount = 0
While Not moRSWips.EOF
' sProcessGroup = moRSWips!ProcGrpName & ""
If sCSN <> moRSWips!CSN Then
sCSN = moRSWips!CSN
If (moRSWips!ProcGrpName = "CradleBatchAssy") Or (moRSWips!ProcGrpName = "PowerTrainSTSV") Or (moRSWips!ProcGrpName = "StaBarAssyBatch") Then
sCSN = moRSWips!VIN & ""
End If
'Increment Column Counter
nCount = nCount + 1
End If
'Get Next Record
moRSWips.MoveNext
Wend
If Not moRSWips.BOF Then
moRSWips.MoveFirst
End If
'Clear Old Data
ClearWorksheetPT
'Clear First Column
ws.Range("B2:C2") = ""
ws.Range("A2:C2") = ""
'Fill Data
If nCount > 0 Then
'Display Status
Me.Cells(3, 6) = "Create Grid"
'Display Status
Me.Cells(3, 6) = "Fill Data"
nRow = nRowStart - 1
While Not moRSWips.EOF
sCSN = moRSWips!CSN & ""
If (moRSWips!ProcGrpName = "CradleBatchAssy") Or (moRSWips!ProcGrpName = "PowerTrainSTSV") Or (moRSWips!ProcGrpName = "StaBarAssyBatch") Then
sCSN = moRSWips!VIN & ""
End If
sUserID = moRSWips!ProcStateUserID & ""
sWIP = moRSWips!ContID & ""
sStartTime = moRSWips!ProcStateStart & ""
sCSNnew = sCSN & sWIP & sStation & sUserID
While Not (sCSNold = sCSNnew)
If Not (sCSNold = sCSNnew) Then
'First Record with NEW CSN
'Increment Row Counter
nRow = nRow + 1
nCol = nCol + 1
'Display CSN
ws.Cells(nRow, 1) = sCSN
ws.Cells(nRow, 2) = sStartTime
'ws.Cells(nRow, 3) = nCount
Worksheets("MON PROD").Cells(10, 3) = nCount
If Not bDateDisplayed Then
'Display First CSN ProcStateEnd
ws.Cells(6, 2) = sDateEnd
bDateDisplayed = True
End If
End If
'Make OLD Copies
sCSNold = sCSN & sVIN & sWIP & sStation & sUserID
Wend
'Get Next Record
moRSWips.MoveNext
Wend
'Display Status
Me.Cells(3, 6) = "Format Data"
'ReActivate Automatic Worksheet Recalculation
Application.Calculation = xlCalculationAutomatic
Format_Cells:
'Display Status
Me.Cells(3, 6) = "Sheet Filled"
Me.Cells(4, 6) = Now
End If
EXIT_SUB:
Exit Sub
ERROR_TRAP:
Select Case Err.Number
Case 0
Case Else
DisplayError msModName & "FillSheetPT", Err.Number, True, Err.Description
Err.Clear
Resume EXIT_SUB
End Select
End Sub