Question : select case logic

I have a sub that is part of an on click event. I cannot get the select case entirely correct. this case determines if the append to a table can go ahead based on a stored date vs current date.

If the stored date is = to the current date (comparing month for now unless you see better way) then the append has already been done so dont call the append routine.

if the stored date is one month less than the current month then call the append routine.

one scenario is that the current month is more than 1 month greater than the stored month ie we forgot to append last month. I cannot see how to change the case to reflect that.

help would be appreciated.

regards in advance
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
Private Sub BtnMonthly_Click()
Dim MyDB As Database
Dim RstStoredMonth As Recordset
  
Set MyDB = CurrentDb
Set RstStoredMonth = MyDB.OpenRecordset("qrydatecurrentstored")
Dim MonthNow As String
Dim StoredMonth As String
Dim StoredDate As String
Dim MyDate, CurrentMonth
'get current date
MyDate = Now()
'get current month
CurrentMonth = Month(MyDate)

'traverse recordset to get stored date
With RstStoredMonth
.MoveFirst
StoredDate = .Fields("currentstoreddate").Value

End With
'get stored month
StoredMonth = Month(StoredDate)


Select Case StoredMonth
    'table uptodate
    Case StoredMonth = CurrentMonth
    'AppendMonthly() 'append monthly data remmed out for now
    'table needs appended snapshot
    Case StoredMonth < CurrentMonth
    
    Case Else
        MsgBox ("should never be here contact pb, no append made")

End Select

Debug.Print CurrentMonth

Debug.Print StoredMonth

'AppendMonthly

End Sub

Answer : select case logic

Bum...is it Monday?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
Dim MyDB As Database
Dim RstStoredMonth As Recordset
Dim StoredMonth As Integer
Dim CurrentMonth As Integer
  
Set MyDB = CurrentDb
Set RstStoredMonth = MyDB.OpenRecordset("qrydatecurrentstored")

'get current month
CurrentMonth = Year(Date) * 12 + Month(Date) - 1

'traverse recordset to get stored date
RstStoredMonth.MoveFirst
StoredMonth = Year(RstStoredMonth!currentstoreddate) * 12 + Month(RstStoredMonth!currentstoreddate) - 1

Select Case CurrentMonth - StoredMonth
Case 0
  'StoredMonth = CurrentMonth, do nothing
  MsgBox ("do nothing already appended this month")
Case 1
  'Append current month data to table
  MsgBox ("append new snapshot data")
Case 2
  'You have missed one month
  MsgBox ("Missed snapshot last month")
Case Is > 2
  'You have missed (CurrentMonth-StoredMonth) months
  MsgBox ("Missed snapshot last " & CurrentMonth - StoredMonth - 1 & " months")
Case Else
  'Error, StoredMonth is greater than CurrentMonth
End Select
Random Solutions  
 
programming4us programming4us