Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default TextBox validation code

In my userform I have 2 TextBoxes.
I nedd code for
validation of TextBox2 is entry : it should be Date,should be current month
and current year date,entry date should be greater than or equal to textbox1.
Thank You.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200905/1

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default TextBox validation code

Try the following. Let me know if not working as you want it to but let me
know what locale date format you are using.

Private Sub TextBox2_AfterUpdate()

Dim bolValid As Boolean
Dim dateValid As Date

On Error Resume Next
dateValid = DateValue(Me.TextBox2)
On Error GoTo 0

If dateValid 0 Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid date format in TextBox2" _
& vbCrLf & "Must be d/m/y format"
Exit Sub
End If

If Year(dateValid) = Year(Date) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid Year in TextBox2" & _
vbCrLf & "Must be " & Year(Date)
Exit Sub
End If

If Month(dateValid) = Month(Date) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid Month in TextBox2" & _
vbCrLf & "Must be " & Month(Date)
Exit Sub
End If

If dateValid = DateValue(Me.TextBox1) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid date in TextBox2" & _
vbCrLf & "Must be = to TextBox1"
Exit Sub
Exit Sub
End If

'You should not need following code
'Only there for testing
If bolValid Then
MsgBox "Date in TextBox2 is valid"
End If

End Sub

--
Regards,

OssieMac


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default TextBox validation code

I have forgotten to mention,If validation does not meet any condition cursor
should go TextBox2 prmpt for correct entry.
I am using dd/mm/yy date format

OssieMac wrote:
Try the following. Let me know if not working as you want it to but let me
know what locale date format you are using.

Private Sub TextBox2_AfterUpdate()

Dim bolValid As Boolean
Dim dateValid As Date

On Error Resume Next
dateValid = DateValue(Me.TextBox2)
On Error GoTo 0

If dateValid 0 Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid date format in TextBox2" _
& vbCrLf & "Must be d/m/y format"
Exit Sub
End If

If Year(dateValid) = Year(Date) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid Year in TextBox2" & _
vbCrLf & "Must be " & Year(Date)
Exit Sub
End If

If Month(dateValid) = Month(Date) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid Month in TextBox2" & _
vbCrLf & "Must be " & Month(Date)
Exit Sub
End If

If dateValid = DateValue(Me.TextBox1) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid date in TextBox2" & _
vbCrLf & "Must be = to TextBox1"
Exit Sub
Exit Sub
End If

'You should not need following code
'Only there for testing
If bolValid Then
MsgBox "Date in TextBox2 is valid"
End If

End Sub


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200905/1

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default TextBox validation code

Can only get the Cursor to set the focus back to TextBox2 if done in the Exit
Event. You will need all of the following code. Note the comments in the code.

I have now included code to allow the user to skip the textbox and leave
blank without invoking the validation. You will get problems even on closing
the form if the user for any reason needs to leave the field blank.

'Following Dim statement must be in Declarations _
area before any Subs or code.
Dim bolValid As Boolean

Private Sub TextBox2_AfterUpdate()

Dim dateValid As Date

'The following If/End If aborts validation _
if TextBox2 is empty. (Delete if not required)
If Me.TextBox2 = "" Then
Exit Sub
End If

On Error Resume Next
dateValid = DateValue(Me.TextBox2)
On Error GoTo 0

If dateValid 0 Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid date format in TextBox2" _
& vbCrLf & "Must be dd/mm/yy format"
Exit Sub
End If

If Year(dateValid) = Year(Date) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid Year in TextBox2" & _
vbCrLf & "Must be " & Year(Date)
Exit Sub
End If

If Month(dateValid) = Month(Date) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid Month in TextBox2" & _
vbCrLf & "Must be " & Month(Date)
Exit Sub
End If

If dateValid = DateValue(Me.TextBox1) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid date in TextBox2" & _
vbCrLf & "Must be = to TextBox1"
Exit Sub
End If

End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)

'Following line allows user to skip if leaving blank
'Remove [And Me.TextBox2 < ""] if you don't want this
If bolValid = False And Me.TextBox2 < "" Then
Cancel = True
Me.TextBox2.SetFocus
End If
End Sub
--
Regards,

OssieMac


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default TextBox validation code

You have an active thread elsewhere.

"tkraju via OfficeKB.com" wrote:

In my userform I have 2 TextBoxes.
I nedd code for
validation of TextBox2 is entry : it should be Date,should be current month
and current year date,entry date should be greater than or equal to textbox1.
Thank You.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200905/1


--

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
Textbox validation [email protected] Excel Programming 9 May 29th 08 09:59 AM
textbox validation TC[_6_] Excel Programming 2 October 13th 04 03:19 AM
textbox value validation girapas[_2_] Excel Programming 1 July 19th 04 12:16 PM
Textbox validation phreud[_17_] Excel Programming 6 June 27th 04 07:49 PM
textbox validation Beginner[_2_] Excel Programming 1 April 7th 04 07:46 PM


All times are GMT +1. The time now is 08:42 PM.

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"