Results 1 to 7 of 7

Thread: Data validation for text box in user form

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    42

    Data validation for text box in user form

    I have 2 text boxes in my user form.
    1- txtpresencehours
    2- txtproductionhours
    I want production hours value does not exceed presence hour (users can not enter)and if it happened a warning message window pops up
    Code:
    Private Sub txtPresenceHours_Afterupdate()
    Dim tString As String
     With txtPresenceHours
    
    'Check if user put in a colon or not
     If InStr(1, .Value, ":", vbTextCompare) = 0 And Len(.Value) > 1 Then
    
    'If not, make string 4 digits and insert colon
     tString = Format(.Value, "0000")
     tString = Left(tString, 2) & ":" & Right(tString, 2)
     txtPresenceHours.Value = Format(TimeValue(tString), "hh:mm")
    Else
    
    'Otherwise, take value as given
     .Value = Format(.Value, "hh:mm")
    End If
    End With
    
    
    
    End Sub
    Private Sub txtProductionHours_AfterUpdate()
    
    Dim tString As String
     With txtProductionHours
    
    'Check if user put in a colon or not
     If InStr(1, .Value, ":", vbTextCompare) = 0 And Len(.Value) > 1 Then
    
    'If not, make string 4 digits and insert colon
     tString = Format(.Value, "0000")
     tString = Left(tString, 2) & ":" & Right(tString, 2)
     txtProductionHours.Value = Format(TimeValue(tString), "hh:mm")
    Else
    
    'Otherwise, take value as given
     .Value = Format(.Value, "hh:mm")
    End If
    End With
    
    If txtProductionHours.Value > txtPresenceHours.Value Then
    MsgBox "Productuin hours must not exceed Presence hours", vbExclamation, "Userform1"
            Me.txtProductionHours.SetFocus
            Exit Sub
        End If
    End Sub
    1-What if i have for example 50 textboxes in my form, is there a a code just to put the boxes name in it instead of writing this code for each box
    Code:
    Private Sub txtPresenceHours_Afterupdate()
    Dim tString As String
     With txtPresenceHours
    
    'Check if user put in a colon or not
     If InStr(1, .Value, ":", vbTextCompare) = 0 And Len(.Value) > 1 Then
    
    'If not, make string 4 digits and insert colon
     tString = Format(.Value, "0000")
     tString = Left(tString, 2) & ":" & Right(tString, 2)
     txtPresenceHours.Value = Format(TimeValue(tString), "hh:mm")
    Else
    
    'Otherwise, take value as given
     .Value = Format(.Value, "hh:mm")
    End If
    End With
    2- Above code gives error for value higher than 23:59 ,also I don't want to round to days and hours i want them to be shown as they are. I changed the format to [h]:mm but didn't work
    Last edited by mortezataheri; Feb 21st, 2017 at 09:33 AM. Reason: Adding Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width