Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 Date format problem

Status
Not open for further replies.

Orcanut

Programmer
Sep 3, 2002
24
CA
I am trying to write a program to check whether or not a CreditCardDate, expressed as 0204 where the first 2 digits are the month and the last 2 are the year, has expired. The following only works if the card month is less than the current month. It doesn't however recognize that 12/01 is < Now. ExpDate, Next Month and Now all print correctly so I am at a loss regarding the logic. Any help most welcome.

Private Sub CheckDate_Click()
Dim NextMonth As Date
Dim ExpDate As Date
ExpDate = Format$(CreditCardDate, &quot;m/YY&quot;)
NextMonth = DateAdd(&quot;m&quot;, 1, ExpDate)
If NextMonth < Now Then
MsgBox &quot;Expired Card&quot;
Print ExpDate
Print NextMonth
Print Now
End If
End Sub
 

Private Sub CheckDate_Click(CreditCardDate As String)
Dim NextMonth As Date
Dim ExpDate As Date
ExpDate = &quot;01/&quot; & Left$(CreditCardDate, 2) & &quot;/&quot; & Right$(CreditCardDate, 2)

NextMonth = DateAdd(&quot;m&quot;, 1, ExpDate)
If NextMonth < Now Then
MsgBox &quot;Card expired on: &quot; & NextMonth

End If
End Sub [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Actually, this is more accurate:

Private Sub CheckDate_Click(CreditCardDate As String)
Dim NextMonth As Date
Dim ExpDate As Date

ExpDate = DateSerial(Right$(CreditCardDate, 2), Left$(CreditCardDate, 2), 1)
NextMonth = DateAdd(&quot;m&quot;, 1, ExpDate)
If NextMonth <= Date Then
MsgBox &quot;Card expired on: &quot; & NextMonth

End If
End Sub [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 

or try
[tt]
Option Explicit

Private Sub Form_Load()

If CheckDate(&quot;0101&quot;) = False Then
MsgBox &quot;Expired Card&quot;
Else
MsgBox &quot;Good Card&quot;
End If

If CheckDate(&quot;1202&quot;) = False Then
MsgBox &quot;Expired Card&quot;
Else
MsgBox &quot;Good Card&quot;
End If

If CheckDate(&quot;1102&quot;) = False Then
MsgBox &quot;Expired Card&quot;
Else
MsgBox &quot;Good Card&quot;
End If


End Sub

Public Function CheckDate(CardDate As String) As Boolean

On Error GoTo CheckDateError

Dim CardYear As Integer, CardMonth As Integer
Dim DateYear As Integer, DateMonth As Integer

CardMonth = Int(Left(CardDate, 2))
CardYear = Int(Mid(CardDate, 3))

DateYear = Int(Right(Trim(Str(Year(Now))), 2))
DateMonth = Int(Trim(Str(Month(Now))))

If CardYear < DateYear Then
CheckDate = False
Exit Function
End If

If CardMonth < Int(Format(Month(Now), &quot;00&quot;)) Then
CheckDate = False
Exit Function
End If

CheckDate = True

Exit Function
CheckDateError:

MsgBox Err.Description

End Function

[/tt]
 
Thanks all! I finally came up with one similar but not quite so eligent as CClint's second version.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top