Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.misc
|
|||
|
|||
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. |
#2
Posted to microsoft.public.excel.misc
|
|||
|
|||
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. |
#3
Posted to microsoft.public.excel.misc
|
|||
|
|||
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. |
#4
Posted to microsoft.public.excel.misc
|
|||
|
|||
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. |
#5
Posted to microsoft.public.excel.misc
|
|||
|
|||
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 |
#6
Posted to microsoft.public.excel.misc
|
|||
|
|||
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Calculate Textbox value based on another textbox value.doc | Excel Discussion (Misc queries) | |||
Calculate Textbox value based on another textbox value | Excel Discussion (Misc queries) | |||
show the input message of a data validation list on a textbox and set editable | Excel Discussion (Misc queries) | |||
Textbox to Textbox | Excel Discussion (Misc queries) | |||
TextBox | Excel Discussion (Misc queries) |