VB - Check Date

Posted on 2006-05-22
Last Modified: 2012-05-05
 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.

Question by:wzm
    LVL 26

    Accepted 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
    LVL 1

    Expert Comment

    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
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Introduction This article makes the case for using two modules in your VBA/VB6 applications to provide both case-sensitive and case-insensitive text comparison operations.  Recently, I solved an EE question using the LIKE function.  In order for th…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now