Date compare isn't working like it should

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmarcrum
    New Member
    • Oct 2007
    • 105

    Date compare isn't working like it should

    Hi everyone!!

    I have a continuous form that allows users to select a record and chnage the DATE that the record is assigned to another DATE within the same year. The button is called "Change plan Date Within 2008." The user can select a particular record, click that button and a new small form appears that displays the current Plan Date and a textbox for entering a new Plan Date.

    [CODE=VB]'If the New Plan Date is earlier than today's date, this is not allowed
    If DateDiff("d", Date, NDate) < 0 Then
    MsgBox "You can not enter a Plan Date earlier than today's date."
    Exit Sub
    End If[/CODE]

    I want to make it to where when the user clicks on a record and then clicks that button and types in a new Plan Date and clicks OK, that it won't allow the user to type in a Plan Date that is EARLIER than the one already assigned to that record.

    here's my code on that command, but so far it doesn't seem to be working like it should.

    [CODE=VB]Private Sub cmdOK_Click()
    Dim NDate As String
    Dim oDate As String

    'Check to see if there is anything typed into the New Plan Date field, if not, close the form
    NewPlanDate.Set Focus

    If NewPlanDate.Tex t = "" Then
    DoCmd.Close acForm, "frmPlanChangeD ate"
    Exit Sub
    End If

    oDate = OldPlanDate.val ue
    NDate = NewPlanDate.val ue

    'If the New Plan Date is earlier than today's date, this is not allowed
    If DateDiff("d", Date, NDate) < 0 Then
    MsgBox "You can not enter a Plan Date earlier than today's date."
    Exit Sub
    End If

    'If the user tries to change the year in this form, this is not allowed
    If Year(oDate) <> Year(NDate) Then
    MsgBox "You can not change the year of the Plan Date here." & vbCrLf & "Please use the " & """" & "Move To" & """" & " buttons to change years."
    DoCmd.Close acForm, "frmPlanChangeD ate"
    Exit Sub
    End If

    'Figure out the year and set the Plan Date appropriatly

    Forms!frmPlan!f rmPlanList.Cont rols("PlanDate" ).value = NDate

    'Close the form
    DoCmd.Close acForm, "frmPlanChangeD ate"

    End Sub[/CODE]
  • jmarcrum
    New Member
    • Oct 2007
    • 105

    #2
    Nevermind I figured it out!

    [CODE=VB]'If the New Plan Date is earlier than today's date, this is not allowed
    If DateDiff("d", Date, NDate) < 0 Or DateDiff("m", Date, NDate) Then
    MsgBox "You cannot enter a Plan Date earlier than today's date."
    Exit Sub
    End If[/CODE]

    Comment

    • jmarcrum
      New Member
      • Oct 2007
      • 105

      #3
      wait i may have spoke too soon.... it's still not workin...

      Comment

      • jmarcrum
        New Member
        • Oct 2007
        • 105

        #4
        Originally posted by jmarcrum
        Nevermind I figured it out!

        [CODE=VB]'If the New Plan Date is earlier than today's date, this is not allowed
        If DateDiff("d", Date, NDate) < 0 Or DateDiff("m", Date, NDate) Then
        MsgBox "You cannot enter a Plan Date earlier than today's date."
        Exit Sub
        End If[/CODE]
        OK I did figure it out!

        Simple change

        [CODE=VB]'If the New Plan Date is earlier than the old date, this is not allowed
        If DateDiff("d", oDate, NDate) < 0 Then
        MsgBox "You cannot enter a Plan Date earlier than the old date date."
        Exit Sub
        End If[/CODE][/QUOTE]

        Comment

        • missinglinq
          Recognized Expert Specialist
          • Nov 2006
          • 3533

          #5
          Actually, with dates, a simple

          If Date1 < Date2 Then

          works just fine!


          Linq ;0)>

          Comment

          Working...