Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. ChildID | ParentID | Data2 | InputDate
  2. ------------------------------------
  3. 1 | 345 | 100 | 3-5-2016
  4. 2 | 345 | 0 | 3-12-2016
  5. 3 | 345 | 150 | 3-19-2016
  6. 4 | 345 | 0 | 4-20-2016
  7. ... more children with different parent IDs
  8.  
  9. Option Compare Database
  10.  
  11. Sub SoldOut()
  12. On Error GoTo ProcError
  13. Dim rs As DAO.Recordset
  14. Set rs = CurrentDb.OpenRecordset("MainDataTable") 'This is the flat file version of the data above
  15.  
  16. 'Check to see if the recordset actually contains rows
  17. If Not (rs.EOF And rs.BOF) Then
  18. rs.MoveLast
  19. rs.MoveFirst 'Unnecessary in this case, but still a good habit
  20. Do Until rs.EOF = True
  21. GetLastWeek = 0
  22. If Not rs("Data") > 0 Then
  23. rs.Edit
  24. rs("Data") = GetLastWeek(rs('ChildID'), rs('ParentID'), rs('Data'), rs('InputDate'))
  25. rs.Update
  26. End If
  27.  
  28. 'Move to the next record. Don't ever forget to do this.
  29. rs.MoveNext
  30. Loop
  31. Else
  32. MsgBox "There are no records in the recordset."
  33. End If
  34.  
  35. ProcExit:
  36. On Error Resume Next
  37. rs.Close 'Close the recordset
  38. Set rs = Nothing 'Clean up
  39. Exit Sub
  40.  
  41. ProcError:
  42. MsgBox Err.Description
  43. Resume ProcExit
  44.  
  45. End Sub
  46.  
  47. Private Function GetLastWeek(ChildID, ParentID As Long, InputDate As Date) As Integer
  48. 'given a record it looks up the weeks before and returns it if it exists
  49. Dim rst As DAO.Recordset, strSQL As String, rc As Integer ' SQL Code for seeing if last week's data exists.
  50.  
  51. strSQL = "SELECT * " & _
  52. "FROM MainDataTable " & _
  53. "WHERE MainDataTable.[ParentId] = " & ParentID & "AND MainDataTable.[InputDate] <# " & InputDate & "AND NOT MainDataTable.[Data] = 0
  54. ORDER BY MainDataTable.[InputDate] DESC;"
  55.  
  56. Set rst = CurrentDb.OpenRecordset(strSQL): rst.MoveLast: rst.MoveFirst
  57. rc = rst.RecordCount
  58. If rc = 0 Then GoTo Cleanup 'if no record, then we are out of luck
  59. If rc > 0 Then 'If there's some Record
  60. Do Until rs.EOF = True Or GetLastWeek > 0
  61. Dim price As Integer: price = rst("Data")
  62. If price > 0 Then: GetLastWeek = price
  63. rs.MoveNext
  64. Loop
  65. End If
  66.  
  67. Cleanup:
  68. rst.Close
  69. Set rst = Nothing
  70. If GetLastWeek = 0 Then GetLastWeek = 1 '1 means no data was found
  71. 'Set so the output if nothing is found to 1 so that the code doesn't have to run on the same rows every single week
  72. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement