[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:
If ((chkdate(txtdate1.Text) = False) Or (chkdate(txtdate2.Text) = False)) Then
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:
Public Function chkdate(strdate As Date) As Boolean
If IsDate(strdate) Then
If strdate Like Format("##/##/##", "mm/dd/yy") Then
chkdate = True
Else
chkdate = False
End If
Else
chkdate = False
End If
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 :)
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:
If Not IsDate(txtdate1.Text) Then
MsgBox "Invalid date. Please Correct it."
txtdate1.SelStart = 0
txtdate1.SelLength = Len(txtdate1.Text)
txtdate1.SetFocus
'perhaps you need to exit sub as well
Exit Sub
End If
'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.
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
Re: Date Validation Problem..
Quote:
Originally Posted by khandu
can u tell me how to use it...
To use what? :confused: :confused: :confused:
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.
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:
If CDate(txtDate1.Date) > CDate(txtDate2.Date) Then
MsgBox "[Date From] cannot be greater than [Date To]."
'highlight date1 textbox (and set focus to it) as I demonstrated in my first reply
'do not continue and exit procedure
Exit Sub
End If
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
:)
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..
Re: Date Validation Problem..
You can do something like this:
VB Code:
Private Sub DTPicker1_Change()
If DTPicker1.Value > DTPicker2.Value Then
DTPicker2.Value = DTPicker1.Value
End If
End Sub
Private Sub DTPicker2_Change()
If DTPicker1.Value > DTPicker2.Value Then
DTPicker1.Value = DTPicker2.Value
End If
End Sub
If you want to use the MonthView control then it's similar:
VB Code:
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
If MonthView1.Value > MonthView2.Value Then
MonthView2.Value = MonthView1.Value
End If
End Sub
Private Sub MonthView2_DateClick(ByVal DateClicked As Date)
If MonthView1.Value > MonthView2.Value Then
MonthView1.Value = MonthView2.Value
End If
End Sub
Re: Date Validation Problem..
Quote:
Originally Posted by RhinoBull
You can do something like this:
VB Code:
Private Sub DTPicker1_Change()
If DTPicker1.Value > DTPicker2.Value Then
DTPicker2.Value = DTPicker1.Value
End If
End Sub
Private Sub DTPicker2_Change()
If DTPicker1.Value > DTPicker2.Value Then
DTPicker1.Value = DTPicker2.Value
End If
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
Re: Date Validation Problem..
got it working myself
VB Code:
Private Sub dtpicker1_change()
DTPicker2.MinDate = DTPicker1.Value
End Sub