Consulting

Results 1 to 4 of 4

Thread: Compare Dates within Textboxes in Userform

  1. #1
    VBAX Regular
    Joined
    May 2009
    Posts
    76
    Location

    Compare Dates within Textboxes in Userform

    Need help figuring out how to compare dates within textboxes. Having problems when certain textboxes are blank.


    Sub Tdater3()
    If UserForm1.Order1.Value = "" Then
    UserForm1.Days3.Value = ""
    Exit Sub
    End If
     
    If UserForm1.Date2.Value And UserForm1.Order1.Value = True Then
    UserForm1.Days3.Value = CDate(UserForm1.Date2.Value) - CDate(UserForm1.Order1.Value)
    End If
     
    If UserForm1.Date2.Value = False And UserForm1.Order1.Value = True Then
    UserForm1.Days3.Value = CDate(UserForm1.TDate1.Value) - CDate(UserForm1.Order1.Value)
    End If

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Option Explicit
    
    Sub Tdater3()
        
     With UserForm1
        If .Order1 = "" Then 'Order1.Value is False right now. 0 or "" = False
            .Days3 = ""
            Exit Sub
        End If
         
        'Order1 is not 0 or ""
        If .Date2 Then 'Date2 is not 0 or ""
            .Days3 = CDate(.Date2) - CDate(.Order1)
        Else 'Date2 is 0 or ""
            If .Tdate1 Then 'Checking if TDate1 is empty
                .Days3 = CDate(.Tdate1) - CDate(.Order1)
            End If
        End If
      End With
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    May 2009
    Posts
    76
    Location
    I received a run-time error '5' Invalid Procedure call or argument for the line starting at
         If .Date2 Then 'Date2 is not 0 or ""
    The textbox field is currently blank and will sometimes be blank or have a date entered.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Use If .TextBox <> "" Then for all TextBoxes used in IF Functions. You can also use If Len(.TextBox) Then

    Dates are stored and handled internally as Type Double. A Date of "" or 0 = 0.0 0 internally and is displayed as 01/01/1904 00:00:00. Times are the decimal portion of the Date Value. If Date = 0.25 then Time = 6AM. (24 x 0.25) Minutes and seconds requires a bit more math. IMO, the easiest is to use the Format Function: Minutes = CInt(Format(Date, "mm"))

    Boolean False = 0 and True is any other number.

    TextBox values are Strings. (tbxDate2.Text)

    Apparently, VBA has trouble converting Strings to a number for the Boolean IF Function
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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