ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Textbox validation (https://www.excelbanter.com/excel-discussion-misc-queries/230199-textbox-validation.html)

TUNGANA KURMA RAJU

Textbox validation
 
how to Validate a TextBox entry with 3 conditions in Userform?
Textbox2 entry must be a date,that date must be of current month & current
year and it should be greater than TextBox1 date.

JLatham

Textbox validation
 
I just built a simple form with 2 text boxes and a command button. The dates
are validated when the button is clicked. But the code inside of that could
be moved into any other area that is appropriate.

Private Sub CommandButton1_Click()
Dim date1 As Date
Dim date2 As Date

If Not IsDate(TextBox1) Then
MsgBox "TextBox1 does not have a date in it"
TextBox1.SetFocus
Exit Sub
End If
If Not IsDate(TextBox2) Then
MsgBox "TextBox2 does not have a date in it"
TextBox2.SetFocus
Exit Sub
End If
date1 = DateSerial(Year(TextBox1), _
Month(TextBox1), Day(TextBox1))
date2 = DateSerial(Year(TextBox2), _
Month(TextBox2), Day(TextBox2))
If date2 < date1 Then
MsgBox "Dates are in error, " & _
"2nd date must be after 1st date"
TextBox2.SetFocus
Exit Sub
End If
If Month(date2) < Month(Now()) Then
MsgBox "Incorrect Month in TextBox2"
TextBox2.SetFocus
Exit Sub
End If
If Year(date2) < Year(Now()) Then
MsgBox "Incorrect Year in TextBox2"
TextBox2.SetFocus
Exit Sub
End If
End Sub


"TUNGANA KURMA RAJU" wrote:

how to Validate a TextBox entry with 3 conditions in Userform?
Textbox2 entry must be a date,that date must be of current month & current
year and it should be greater than TextBox1 date.


Jacob Skaria

Textbox validation
 
Assuming the textboxes are named txtStartDate and txtEndDate and you need
this validation to happen at CommandButton1 click

Private Sub CommandButton1_Click()

If IsDate(txtStartDate) = False Or IsDate(txtEndDate) = False Then
MsgBox "Invalid Date range"
txtStartDate.SetFocus: Exit Sub
ElseIf CDate(txtEndDate) <= CDate(txtStartDate) Then
MsgBox "End Date should be greater then Start date"
txtEndDate.SetFocus: Exit Sub
ElseIf Format(CDate(txtEndDate), "mmyyyy") < _
Format(CDate(txtStartDate), "mmyyyy") Then
MsgBox "Both dates should be of the same month"
txtEndDate.SetFocus: Exit Sub
End If

End Sub

If this post helps click Yes
---------------
Jacob Skaria


"TUNGANA KURMA RAJU" wrote:

how to Validate a TextBox entry with 3 conditions in Userform?
Textbox2 entry must be a date,that date must be of current month & current
year and it should be greater than TextBox1 date.


Jacob Skaria

Textbox validation
 
'With validtion for current month

Assuming the text boxes are named txtStartDate and txtEndDate..

Private Sub CommandButton1_Click()

If IsDate(txtStartDate) = False Or IsDate(txtEndDate) = False Then
MsgBox "Invalid Date range"
txtStartDate.SetFocus: Exit Sub
ElseIf CDate(txtEndDate) <= CDate(txtStartDate) Then
MsgBox "End Date should be greater then Start date"
txtEndDate.SetFocus: Exit Sub
ElseIf Format(CDate(txtStartDate), "mmyyyy") < _
Format(Date, "mmyyyy") Then
MsgBox "Date should be of current month"
txtStartDate.SetFocus: Exit Sub
ElseIf Format(CDate(txtEndDate), "mmyyyy") < _
Format(CDate(txtStartDate), "mmyyyy") Then
MsgBox "Both dates should be of the same month"
txtEndDate.SetFocus: Exit Sub
End If

End Sub

--
If this post helps click Yes
---------------
Jacob Skaria


"Jacob Skaria" wrote:

Assuming the textboxes are named txtStartDate and txtEndDate and you need
this validation to happen at CommandButton1 click

Private Sub CommandButton1_Click()

If IsDate(txtStartDate) = False Or IsDate(txtEndDate) = False Then
MsgBox "Invalid Date range"
txtStartDate.SetFocus: Exit Sub
ElseIf CDate(txtEndDate) <= CDate(txtStartDate) Then
MsgBox "End Date should be greater then Start date"
txtEndDate.SetFocus: Exit Sub
ElseIf Format(CDate(txtEndDate), "mmyyyy") < _
Format(CDate(txtStartDate), "mmyyyy") Then
MsgBox "Both dates should be of the same month"
txtEndDate.SetFocus: Exit Sub
End If

End Sub

If this post helps click Yes
---------------
Jacob Skaria


"TUNGANA KURMA RAJU" wrote:

how to Validate a TextBox entry with 3 conditions in Userform?
Textbox2 entry must be a date,that date must be of current month & current
year and it should be greater than TextBox1 date.


Dave Peterson

Textbox validation
 
I don't think I would use a textbox to get a date from a user.

I'd have no idea what this string actually means:
01/02/03

You may want to consider using a calendar control.

Ron de Bruin has some notes:
http://www.rondebruin.nl/calendar.htm

TUNGANA KURMA RAJU wrote:

how to Validate a TextBox entry with 3 conditions in Userform?
Textbox2 entry must be a date,that date must be of current month & current
year and it should be greater than TextBox1 date.


--

Dave Peterson

JLatham

Textbox validation
 
Aha! The very basic problem surrounding any textbox which someone tries to
use for non-text entries such as dates or numeric information of some type.

But at least we can assume it'll make a good guess on the 9th of September
this year :).

"Dave Peterson" wrote:

I don't think I would use a textbox to get a date from a user.

I'd have no idea what this string actually means:
01/02/03

You may want to consider using a calendar control.

Ron de Bruin has some notes:
http://www.rondebruin.nl/calendar.htm

TUNGANA KURMA RAJU wrote:

how to Validate a TextBox entry with 3 conditions in Userform?
Textbox2 entry must be a date,that date must be of current month & current
year and it should be greater than TextBox1 date.


--

Dave Peterson



All times are GMT +1. The time now is 09:17 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com