Textbox validation

  • Thread starter TUNGANA KURMA RAJU
  • Start date
T

TUNGANA KURMA RAJU

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.
 
J

JLatham

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
 
J

Jacob Skaria

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
 
J

Jacob Skaria

'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
 
J

JLatham

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 :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top