You probably want to convert your dates to variant-date values before you start trying to manipulate them. This will allow you to use functions like DateAdd to compare sequential months.
Here are series of functions I had stashed away for this purpose.
Code:
Option Compare Database
'Functions included:
'Y4MDToDate - Takes string input from YYYYMM or YYYYMMDD format and outputs a variant date
'DateToY4MD - Takes variant date in and outputs YYYYMMDD format
'DateToY4M - Same as DateToY4MD but drops calendar day, outputs YYYYMM format
'Y2MDToDate - Takes string input from YYMM or YYMMDD format and outputs a variant date
'DateToY2MD - Takes variant date in and outputs YYMMDD format
'DateToY2M - Same as DateToY2MD but drops calendar day, outputs YYMM format
Public Function Y4MDToDate(varDate As Variant) As Variant
On Error GoTo errHandle
If Len(varDate) = 8 Then
'YYYYMMDD format
Y4MDToDate = DateSerial(CInt(Left(varDate, 4)), CInt(Mid(varDate, 5, 2)), CInt(Right(varDate, 2)))
ElseIf Len(varDate) = 6 Then
'YYYYMM format
Y4MDToDate = DateSerial(CInt(Left(varDate, 4)), CInt(Mid(varDate, 5, 2)), 1)
Else
'Incorrect format
Y4MDToDate = Null
End If
Exit Function
errHandle:
Y4MDToDate = Null
End Function
Public Function DateToY4MD(varDate As Variant) As Variant
On Error GoTo errHandle
DateToY4MD = Format(varDate, "YYYYMMDD")
Exit Function
errHandle:
DateToY4MD = Null
End Function
Public Function DateToY4M(varDate As Variant) As Variant
On Error GoTo errHandle
DateToY4M = Format(varDate, "YYYYMM")
Exit Function
errHandle:
DateToY4M = Null
End Function
Public Function Y2MDToDate(varDate As Variant) As Variant
On Error GoTo errHandle
If Len(varDate) = 6 Then
'YYMMDD format
Y2MDToDate = DateSerial(CInt(Left(varDate, 2)), CInt(Mid(varDate, 3, 2)), CInt(Right(varDate, 2)))
ElseIf Len(varDate) = 4 Then
'YYMM format
Y2MDToDate = DateSerial(CInt(Left(varDate, 2)), CInt(Mid(varDate, 3, 2)), 1)
Else
'Incorrect format
Y2MDToDate = Null
End If
Exit Function
errHandle:
Y2MDToDate = Null
End Function
Public Function DateToY2MD(varDate As Variant) As Variant
On Error GoTo errHandle
DateToY2MD = Format(varDate, "YYMMDD")
Exit Function
errHandle:
DateToY2MD = Null
End Function
Public Function DateToY2M(varDate As Variant) As Variant
On Error GoTo errHandle
DateToY2M = Format(varDate, "YYMM")
Exit Function
errHandle:
DateToY2M = Null
End Function
Good luck,
Jeff