locked
compare date RRS feed

  • Question

  • i have 2 datetimepicker which are startdatedatetimepicker and enddatedatetimepicker..

    the start date should not be more than end date..thus i need to check these two date..

    here is my code...

     

     Private Sub chk_Date()
    
        curPosition = MasterBindingSource.Position
    
        If EndDateDateTimePicker.Checked = True And StartDateDateTimePicker.Checked = True Then
    
          If DateValue(StartDateDateTimePicker.Text) > DateValue(EndDateDateTimePicker.Text) Then
            MsgBox("Start Date more than End Date.Please do necessary changes.")
            
          End If
        End If
    
      End Sub
    
    
    Private Sub StartDateDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StartDateDateTimePicker.ValueChanged
        chk_Date()
      End Sub
    
    
      Private Sub EndDateDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles EndDateDateTimePicker.ValueChanged
        chk_Date()
      End Sub
    

    supposely, only when user key in both date, the chk_Date() will compare the start date and end date...

    but what happen is, it check the start date from row 1 and check end date from row 2..

    i already set the currentposition..but this still happen..

    may i know where is the mistake..tq..

     


    Humaira Laila
    Friday, August 12, 2011 5:50 AM

Answers

  • I don't explain well. Consider this code: the flag is raised when a datepicker got focus and lowered after the check.

    So, the check is done when user changes the dtp values only.

    (due to the messy code editor, I rewrite the code in another post, in reply to this)
    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it



     Private Sub DateDateTimePicker_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
                         Handles EndDateDateTimePicker.GotFocus, _
                         StartDateDateTimePicker.GotFocus
      DirectCast(sender, DateTimePicker).Tag = True
     End Sub
    
     Private Sub DateDateTimePicker_ValueChanged(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles StartDateDateTimePicker.ValueChanged, _
                           EndDateDateTimePicker.ValueChanged
      checkdate()
     End Sub
    
     Sub checkdate()
      If CType(StartDateDateTimePicker.Tag, Boolean) = True _
       OrElse CType(EndDateDateTimePicker.Tag, Boolean) = True Then
    
       If StartDateDateTimePicker.Checked = True _
        AndAlso EndDateDateTimePicker.Checked = True Then
    
        If DateTime.Compare(StartDateDateTimePicker.Value, EndDateDateTimePicker.Value) > 0 Then
         MsgBox("The start date cannot be greater than end date, please make necessary changes")
        End If
       End If
       StartDateDateTimePicker.Tag = Nothing
       EndDateDateTimePicker.Tag = Nothing
      End If
     End Sub
    


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    • Marked as answer by Mike Feng Tuesday, August 23, 2011 3:21 PM
    Tuesday, August 16, 2011 7:37 AM

All replies

  • I don't understand why the two dtp can reference "row 1" and/or "row 2". Nor why there is a 'curposition'

    I can suppose only that you need (perhaps) to compare also curposition with a static variable: when it changes, the dtps must be unchecked.


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    Friday, August 12, 2011 6:20 AM
  • http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx

     

    Dim date1 As Date = #08/01/2009 12:00AM#
    Dim date2 As Date = #08/01/2009 12:00PM#
    Dim result As Integer = DateTime.Compare(date1, date2)
    Dim relationship As String
    
    If result < 0 Then
      relationship = "is earlier than"
    ElseIf result = 0 Then
      relationship = "is the same time as"     
    Else
      relationship = "is later than"
    End If
    
    Console.WriteLine("{0} {1} {2}", date1, relationship, date2)
    ' The example displays the following output:
    '  8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM
    



    Faraz
    Friday, August 12, 2011 6:31 AM
  • ok i already remove the curposition since there is no use of it..

    n it still compare start date from row 1 and end date from row 2..


    Humaira Laila
    Friday, August 12, 2011 7:16 AM
  • faraz,

    i already change my code as the code given, 

    n it still compare start date from row 1 and end date from row 2..

     

    is the way i call chk_Date() in StartDateDateTimePicker_ValueChanged and EndDateDateTimePicker_ValueChanged correct?


    Humaira Laila
    Friday, August 12, 2011 7:20 AM
  • is the way i call chk_Date() in StartDateDateTimePicker_ValueChanged and EndDateDateTimePicker_ValueChanged correct?

    Sorry but i can not understand what you are trying to say please explain.

    If you are trying to use it as a function

     

     Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
      'Dim dt As Date = Date.Today
      MsgBox(chkdate(Date.Today, "7/12/2008"))
    
     End Sub
    
     Public Function chkdate(ByVal date1 As Date, ByVal date2 As Date)
    
      Dim result As Integer = DateTime.Compare(date1, date2)
      Dim relationship As String
    
      If result < 0 Then
       relationship = "is earlier than"
      ElseIf result = 0 Then
       relationship = "is the same time as"
      Else
       relationship = "is later than"
      End If
    
      Return relationship
    
     End Function
    
    


     

    .


    Faraz
    Friday, August 12, 2011 7:28 AM
  • ok. But what is the two rows? what do them with the dtps? when dtps are checked?

    The 'logic' problem is in your verify of checked state for the dtps, but this problem solution need (at least form my side) more info. I suppose you need two static variables, not two check (or not only).

     


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    Friday, August 12, 2011 7:31 AM
  • faraz, what i mean is,when user insert date in startdatedatetimepicker it will trigger StartDateDateTimePicker_ValueChanged event..then it will execute chk_Date()..but the enddate is not key in yet..

    so supposely the [if EndDateDateTimePicker.Checked = True And StartDateDateTimePicker.Checked = True then] will also not been executed since only StartDateDateTimePicker.Checked return true...

    so that's mean the date is not been compared yet..

    then user insert the end date in enddatedatetimepicker which will trigger EndDateDateTimePicker_ValueChanged event, thus execute again the chk_Date()..

    so this time, both startdatedatetimepicker and enddatedatetimepicker return true, and only then both date will be compared...

    hope u understand what i mean..




    Humaira Laila
    Friday, August 12, 2011 8:07 AM
  • diego,

     

    • i mean row in my table in database..
    • if startdate/enddate in database is null, datetimepicker.check = false and vice versa
    • so if startdatetimepicker.check and enddatetimepicker.check= true, thats mean there is date in my database, so i will compare the both date to determine either user has enter start date earlier than end date or not..
    • because start date logically cannot be more than end date..

     


    Humaira Laila
    Friday, August 12, 2011 8:17 AM
  • And how are dtps binded ? I mean: is there a code to set checks at current row change? if so, what is this code?


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    Friday, August 12, 2011 8:26 AM
  • faraz, what i mean is,when user insert date in startdatedatetimepicker it will trigger StartDateDateTimePicker_ValueChanged event..then it will execute chk_Date()..but the enddate is not key in yet..

    so supposely the [if EndDateDateTimePicker.Checked = True And StartDateDateTimePicker.Checked = True then] will also not been executed since only StartDateDateTimePicker.Checked return true...

    so that's mean the date is not been compared yet..

    then user insert the end date in enddatedatetimepicker which will trigger EndDateDateTimePicker_ValueChanged event, thus execute again the chk_Date()..

    so this time, both startdatedatetimepicker and enddatedatetimepicker return true, and only then both date will be compared...

    hope u understand what i mean..




    Humaira Laila

    Humaira

    you are making us confuse,

    check if startdatetimepicker is set then check if enddatetimepicker is set if both are set (= true) then call checkdate function. you can place the same code in both events StartDateDateTimePicker_ValueChanged and EndDateDateTimePicker_ValueChanged

     

    If StartDateDateTimePicker.Checked = True Then
          If EndDateDateTimePicker.Checked = True Then
            checkdate()
          End If
        End If
    

     

    if this is not what you want please explain what you want to achieve in points\steps

    1. What is the source of date (how you enter it in database)

    2. do you want to check weather  start date or end date was entered in database.

    3. When both date time picker are set you want to call check date.

    4 what else do you want


    Faraz
    Friday, August 12, 2011 8:43 AM
  • on BindingSource_PositionChanged i put the code below:

     

    if isdbnull(dataset.tables("table").row.item(curPosition).item("stardate")) = false then

    startdatetimepicker.check = true

    else

    startdatetimepicker.check = false

    end if

     

     

    while in BindingNavigatorSaveItem_Click i put the code below:

    if startdatetimepicker.check = false then 

    dataset.tables("table").row.item(curPosition).item("stardate") = dbnull.value

    end if

     

    dtps binded using bindingsouce.bind to dataset..

     


    Humaira Laila
    Friday, August 12, 2011 8:45 AM
  • if this is not what you want please explain what you want to achieve in points\steps

    1. What is the source of date (how you enter it in database)

    2. do you want to check weather  start date or end date was entered in database.

    3. When both date time picker are set you want to call check date.

    4 what else do you want


    Faraz

    what i want to do is:

    • first i have 2 datetimepicker which name startdatetimepicker and enddatetimepicker.
    • the enddate cannot be less than startdate
    • for example, startdate = 1 august 2011, so enddate must be greater than the start date which is >1august 2011
    • however, this can only be done or calculated if user insert both date..
    • that's all...

     


    Humaira Laila
    Friday, August 12, 2011 8:55 AM
  • Now this is what i can understand, This code below does what you ask

    If StartDateDateTimePicker.Checked = True Then 'check if start date is entered if not display message
          If EndDateDateTimePicker.Checked = True Then 'check if end date is entered if not display message
    
            Dim result As Integer = DateTime.Compare(startdate, enddate) 'supply start date and end date here
            Dim relationship As String
    
            If result > 0 Then ' if end date is greater then start date condition = true
    
              MsgBox("The end date is greater then start date, Condition = true")
            Else
              MsgBox("Wrong dates inserted") 'display message if start date is greater then end date.
            End If
          Else
            MsgBox("the user did not enter the end date")
    
          End If
          MsgBox("the user did not enter the start date")
        End If
    
    



    Faraz
    Friday, August 12, 2011 9:12 AM
  • sub checkdate()
    
    If StartDateDateTimePicker.Checked = True Then 'check if start date is entered if not display message
       If EndDateDateTimePicker.Checked = True Then 'check if end date is entered if not display message
    
        Dim result As Integer = DateTime.Compare(startdate, enddate) 'supply start date and end date here
      
        If result < 0 Then ' if end date is greater then start date condition = false
    
         MsgBox("The start date cannot be greater than end date, please make necessary changes")
        End If
       
    exit sub
    
    Private Sub EndDateDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles EndDateDateTimePicker.ValueChanged
        checkdate()
      End Sub
    
    Private Sub StartDateDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StartDateDateTimePicker.ValueChanged
        checkdate()
      End Sub
    
    

    ok i use this code..but the msgbox appear twice when both startdatedtp and enddatedtp are checked..

    Humaira Laila
    Friday, August 12, 2011 9:31 AM
  • I suppose you make the same with enddate.

    Try to avoid 'curPosition', like this (I avoid also If statements)

    startdatetimepicker.check = Not IsDBNull(theDataSet.Tables("table").Rows(theBindingSource.Position)("startdate"))
    

     


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    Friday, August 12, 2011 9:37 AM
  • appearing of message for two times means that the code has been run at-least 3 times

    • when start date entered (can not appear on this because the end date is not entered)
    • when end date entered ( can appear )
    • Unknown event

    You need to find out on what other event did you executed the check date function.

     


    Faraz
    Friday, August 12, 2011 10:00 AM
  • however, this can only be done or calculated if user insert both date..


    Humaira Laila

    but, if so, you must trap the changing by user in different way you do on bindingsource row change.

    on dtp.Enter, you set a flag on (dtp.tag = true)

    on dtp.changes, you verufy this flag (if not isnothing(dtp.tag) then dtp.check = true, dtp.tag = nothing)


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    Friday, August 12, 2011 10:29 AM

  • but, if so, you must trap the changing by user in different way you do on bindingsource row change.

    on dtp.Enter, you set a flag on (dtp.tag = true)

    on dtp.changes, you verufy this flag (if not isnothing(dtp.tag) then dtp.check = true, dtp.tag = nothing)


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it

     

    i got ur point..but do u mean dtp.changes is dtp_valuechanges?

    i wrote ad below, but do u have any idea where should i call checkdate() ?

     

    Private Sub EndDateDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles EndDateDateTimePicker.ValueChanged
     flag = true
     End Sub
    
    Private Sub StartDateDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StartDateDateTimePicker.ValueChanged
     flag = true
     End Sub 
    
     
    sub checkdate()
    	if flag = true then 
    		If StartDateDateTimePicker.Checked = True Then 'check if start date is entered if not display message
     			If EndDateDateTimePicker.Checked = True Then 'check if end date is entered if not display message
    
     				Dim result As Integer = DateTime.Compare(startdate, enddate) 'supply start date and end date here
     
     			 	If result < 0 Then ' if end date is greater then start date condition = false
     			 	MsgBox("The start date cannot be greater than end date, please make necessary changes")
     			 	End If
    
    			 End If
    
    		End If
    
    End If
    exit sub
    


     


    Saturday, August 13, 2011 2:25 AM
  • Hi Humaira,

    I suggest you to call the it in the next step.

    Because after you set the start date and end date, I am sure you will go to the next step, for example, click a query button, or click the next step button and so on.

    So you can check it in the click event handler, if it failure, pop up a message box and exit the handler, if it success, go on the next process.

    If the user didn't click the button, we can believe that the user doesn't want to go on his work, so we don't have to check it now, we can check it when the user go back this operation. 

    I hope this will be helpful.

    Best regards,

     


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 16, 2011 5:17 AM
  • I don't explain well. Consider this code: the flag is raised when a datepicker got focus and lowered after the check.

    So, the check is done when user changes the dtp values only.

    (due to the messy code editor, I rewrite the code in another post, in reply to this)
    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it


    Tuesday, August 16, 2011 7:28 AM
  • I don't explain well. Consider this code: the flag is raised when a datepicker got focus and lowered after the check.

    So, the check is done when user changes the dtp values only.

    (due to the messy code editor, I rewrite the code in another post, in reply to this)
    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it



     Private Sub DateDateTimePicker_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
                         Handles EndDateDateTimePicker.GotFocus, _
                         StartDateDateTimePicker.GotFocus
      DirectCast(sender, DateTimePicker).Tag = True
     End Sub
    
     Private Sub DateDateTimePicker_ValueChanged(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles StartDateDateTimePicker.ValueChanged, _
                           EndDateDateTimePicker.ValueChanged
      checkdate()
     End Sub
    
     Sub checkdate()
      If CType(StartDateDateTimePicker.Tag, Boolean) = True _
       OrElse CType(EndDateDateTimePicker.Tag, Boolean) = True Then
    
       If StartDateDateTimePicker.Checked = True _
        AndAlso EndDateDateTimePicker.Checked = True Then
    
        If DateTime.Compare(StartDateDateTimePicker.Value, EndDateDateTimePicker.Value) > 0 Then
         MsgBox("The start date cannot be greater than end date, please make necessary changes")
        End If
       End If
       StartDateDateTimePicker.Tag = Nothing
       EndDateDateTimePicker.Tag = Nothing
      End If
     End Sub
    


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    • Marked as answer by Mike Feng Tuesday, August 23, 2011 3:21 PM
    Tuesday, August 16, 2011 7:37 AM
  • I don't explain well. Consider this code: the flag is raised when a datepicker got focus and lowered after the check.

    So, the check is done when user changes the dtp values only.

    (due to the messy code editor, I rewrite the code in another post, in reply to this)
    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it



     Private Sub DateDateTimePicker_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
               Handles EndDateDateTimePicker.GotFocus, _
               StartDateDateTimePicker.GotFocus
     DirectCast(sender, DateTimePicker).Tag = True
     End Sub
    
     Private Sub DateDateTimePicker_ValueChanged(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) _
                Handles StartDateDateTimePicker.ValueChanged, _
                EndDateDateTimePicker.ValueChanged
     checkdate()
     End Sub
    
     Sub checkdate()
     If CType(StartDateDateTimePicker.Tag, Boolean) = True _
      OrElse CType(EndDateDateTimePicker.Tag, Boolean) = True Then
    
      If StartDateDateTimePicker.Checked = True _
      AndAlso EndDateDateTimePicker.Checked = True Then
    
      If DateTime.Compare(StartDateDateTimePicker.Value, EndDateDateTimePicker.Value) > 0 Then
       MsgBox("The start date cannot be greater than end date, please make necessary changes")
      End If
      End If
      StartDateDateTimePicker.Tag = Nothing
      EndDateDateTimePicker.Tag = Nothing
     End If
     End Sub
    


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it

    your code works perfectly!

    supposedly it must compare startdate in row 1 with enddate in row 1..

    but i still get the same result, which compare startdate in row 1 with enddate in row 2..

    so i think there must be problem somewhere in my code..

    btw big thanks for the solution..at least i've fix something.. :)


    Humaira Laila
    Wednesday, August 24, 2011 6:19 AM