Results 1 to 10 of 10

Thread: [RESOLVED] Date Validation Problem..

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Resolved [RESOLVED] Date Validation Problem..

    Hello

    I am taking input of 2 date as mm/dd/yy format in txtdate1.text and txtdate2.text

    Here is the validation I am taking
    VB Code:
    1. If ((chkdate(txtdate1.Text) = False) Or (chkdate(txtdate2.Text) = False)) Then
    2. MsgBox "Invalid Date Input.. Please input in the format mm/dd/yy", vbCritical, "Invalid Date"

    the chkdate() function is doing the validation as

    VB Code:
    1. Public Function chkdate(strdate As Date) As Boolean
    2.     If IsDate(strdate) Then
    3.         If strdate Like Format("##/##/##", "mm/dd/yy") Then
    4.           chkdate = True
    5.         Else
    6.           chkdate = False
    7.         End If
    8.     Else
    9.         chkdate = False
    10.     End If
    11.    
    12. End Function

    But even if the date is entered correct it gives error msgbox

    what is wrong in code.. please help

    also please give me a code which can check that 1st date is lesser than 2nd date inserted

    thanks

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Date Validation Problem..

    Validating dates by the format isn't best way - simply check if it's a date at all using IsDate() and advise user if it is not.
    If you need specific format you can format the value any time in your code when it is necessary.
    VB Code:
    1. If Not IsDate(txtdate1.Text) Then
    2.     MsgBox "Invalid date. Please Correct it."
    3.     txtdate1.SelStart = 0
    4.     txtdate1.SelLength = Len(txtdate1.Text)
    5.     txtdate1.SetFocus
    6.     'perhaps you need to exit sub as well
    7.     Exit Sub
    8. End If
    9.  
    10. 'do the same for txtdate2
    However, I would recommend to substitute textboxes for DateTimePicker or MonthView controls to avoid unnecessary validations.
    Both controls could be found in the Windows Common Controls-2 6.0 library.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Re: Date Validation Problem..

    can u tell me how to use it

    I am unable to figure it out.. how to put the chosen date in txt box or how else to do it??

    Also how to validate between the two dates that 1st is before then 2nd date choosen

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Date Validation Problem..

    Quote Originally Posted by khandu
    can u tell me how to use it...
    To use what?

    Quote Originally Posted by khandu
    ... how to put the chosen date in txt box or how else to do it??
    I did tell you:
    Quote Originally Posted by RhinoBull
    ... I would recommend to substitute textboxes for DateTimePicker or MonthView controls to avoid unnecessary validations.
    Both controls could be found in the Windows Common Controls-2 6.0 library.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Date Validation Problem..

    Quote Originally Posted by khandu
    ...Also how to validate between the two dates that 1st is before then 2nd date choosen
    Perhaps like this:
    VB Code:
    1. If CDate(txtDate1.Date) > CDate(txtDate2.Date) Then
    2.     MsgBox "[Date From] cannot be greater than [Date To]."
    3.     'highlight date1 textbox (and set focus to it) as I demonstrated in my first reply
    4.     'do not continue and exit procedure
    5.     Exit Sub
    6. End If

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Re: Date Validation Problem..

    Sorry

    what i meant was i took 2 DTPicker.. dt1 and dt2

    Now when a person chooses a date.. where is it stored ?? or how to store it in a variable ??

    EDIT : i got it i think its in dt1.value

    and how to compare dt1 < dt2 <-- help on this

    Last edited by khandu; Feb 4th, 2007 at 09:56 AM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Re: Date Validation Problem..

    ok.. to rephrase it better..

    how do i make it so that

    when dt1 is selected to a date.. then the dt2.date starting date becomes that.. so when a person selects dt2.. it cannot be less than dt1

    so the problem of dt1 being more than dt2 cannot happen..

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Date Validation Problem..

    You can do something like this:
    VB Code:
    1. Private Sub DTPicker1_Change()
    2.     If DTPicker1.Value > DTPicker2.Value Then
    3.         DTPicker2.Value = DTPicker1.Value
    4.     End If
    5. End Sub
    6.  
    7. Private Sub DTPicker2_Change()
    8.     If DTPicker1.Value > DTPicker2.Value Then
    9.         DTPicker1.Value = DTPicker2.Value
    10.     End If
    11. End Sub
    If you want to use the MonthView control then it's similar:
    VB Code:
    1. Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
    2.     If MonthView1.Value > MonthView2.Value Then
    3.         MonthView2.Value = MonthView1.Value
    4.     End If
    5. End Sub
    6.  
    7. Private Sub MonthView2_DateClick(ByVal DateClicked As Date)
    8.     If MonthView1.Value > MonthView2.Value Then
    9.         MonthView1.Value = MonthView2.Value
    10.     End If
    11. End Sub

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Re: Date Validation Problem..

    Quote Originally Posted by RhinoBull
    You can do something like this:
    VB Code:
    1. Private Sub DTPicker1_Change()
    2.     If DTPicker1.Value > DTPicker2.Value Then
    3.         DTPicker2.Value = DTPicker1.Value
    4.     End If
    5. End Sub
    6.  
    7. Private Sub DTPicker2_Change()
    8.     If DTPicker1.Value > DTPicker2.Value Then
    9.         DTPicker1.Value = DTPicker2.Value
    10.     End If
    11. End Sub
    Dosent seem to work... when a dt1 is selected.. i want all the dates before that in dt2 to become disabled.. so that some cannot choose an earlier date in dt2

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Re: Date Validation Problem..

    got it working myself

    VB Code:
    1. Private Sub dtpicker1_change()
    2.  
    3. DTPicker2.MinDate = DTPicker1.Value
    4.  
    5.  
    6. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width