Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

VB - Check Date

 How could i check date tht falls within the 6 months period ? I tried to use DateDiff .. but was not working...
If (DateDiff("m", mydate, Now)) <= 6 And (DateDiff("m", mydate, Now)) > 0 Then
    Validate_Date = True

* the requirement is ..
say today is 20 May 2006.

1 - System will only allow ( 20 Nov 2005 to 20 May 2006 )
- valid cheque = 6 months

2 - not later than today's date.

so if the cheque date is 19 Nov 2005 .. system shld not allowed.. but my codes allow it. oh boy.

Pls help.

1 Solution
Dim d As Long

d = DateDiff("s", DateAdd("m", -6, Now), Now)
If (DateDiff("s", yourcheckdate, Now) <= d) Then
    MsgBox "within 6 month"
End If
Try this.. I forgot what I was doing by the time I finished it so I'm sorry if its sloppy (Its a problem of mine.. I'll forget how i did something, and what certain portions of code are for)

'This code requires nothing
Private Sub Form_Load()
Dim lHold As Long, lHold2 As Long, dHold As Date
dHold = DateSerial(2006, 5, 23)
lHold = DateDiff("m", DateTime.Date, dHold)
lHold2 = DateDiff("d", DateTime.Date, dHold)
If (lHold <= 0 And lHold >= -6) Then
    If (DateTime.Month(dHold) = DateTime.Month(DateTime.Date) And DateTime.Day(dHold) <= DateTime.Day(DateTime.Date)) Then
        MsgBox "COngrats"
    End If
End If
End Sub
Mike TomlinsonMiddle School Assistant TeacherCommented:
If it is possible for your "input" date to hold a time (it may be parsed from user input for example) then you should ensure that the dates you are comparing against start at the beginning of six months ago (0000 hrs) and the end of today (2359 hrs):

    Private Sub Command1_Click()
        If IsDate(Text1.Text) Then
            Dim today As Date
            Dim sixMonthsAgo As Date
            today = Date
            sixMonthsAgo = DateAdd("m", -6, today) ' beginning of six months ago
            today = DateAdd("d", 1, DateAdd("s", -1, today)) ' end of today
            Debug.Print "sixMonthsAgo = " & sixMonthsAgo
            Debug.Print "today = " & today
            Dim dt As Date
            dt = CDate(Text1.Text)
            Debug.Print "dt = " & dt
            If dt >= sixMonthsAgo And dt <= today Then
                MsgBox "OK"
                MsgBox "Date Out of Range", vbExclamation, "Invalid Date"
            End If
            MsgBox "Enter a Date in the TextBox", vbExclamation, "Invalid Date"
        End If
    End Sub

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now