#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 171
Default 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   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default 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   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default 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   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default 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   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default 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   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate Textbox value based on another textbox value.doc Tdungate Excel Discussion (Misc queries) 1 February 12th 09 07:11 PM
Calculate Textbox value based on another textbox value Tdungate Excel Discussion (Misc queries) 0 February 12th 09 07:03 PM
show the input message of a data validation list on a textbox and set editable HCS7 Excel Discussion (Misc queries) 3 November 20th 08 11:43 AM
Textbox to Textbox Troubled User Excel Discussion (Misc queries) 1 April 13th 07 03:44 AM
TextBox Jimbo1 Excel Discussion (Misc queries) 2 April 20th 06 02:47 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"