Results 1 to 16 of 16

Thread: Adding a colon to a time field if one doesn't exist.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169

    Question Adding a colon to a time field if one doesn't exist.

    I have a gridview control with a date/time column. It's quite old and was free with a magazine but it does the job...almost. It won't let me format the column to just time. It has to be date and time. If the user puts in a time without the colon, e.g. 1730 instead of 17:30, the program crashes.

    That's my scenario. What I need is a function that puts in a colon after the first two digits of a time field if they don't exist.

    This would help me out considerably.

    Thanks.

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    tstr = IIF (2 = instr(tstr, ":"), tstr, Left(tstr, 2) & ":" & right(tstr, len(tstr) - 2))

    something like that

  3. #3
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    just check user input and display a message box:

    Code:
    if not IsDate(myTime) then
         MsgBox "Incorrect Time Format!"
    Else
         do things....
    End If

  4. #4
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Try this and let me know of it works for you:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strTime As String
    3.  
    4.     strTime = "1235"
    5.     Debug.Print FormatTime(strTime)
    6.  
    7. End Sub
    8.  
    9. Public Function FormatTime(strValue As String) As String
    10. '========================================================
    11. Dim pos As Integer
    12.  
    13.     strValue = Trim(strValue)
    14.     pos = InStr(1, strValue, ":")
    15.     If pos = 0 Then
    16.         If Len(strValue) = 4 Then
    17.             FormatTime = Left(strValue, 2) & ":" & Right(strValue, 2)
    18.         ElseIf Len(Trim(strValue)) = 6 Then
    19.             FormatTime = Left(strValue, 2) & ":" & Mid(strValue, 3, 2) & ":" & Right(strValue, 2)
    20.         End If
    21.     End If
    22.  
    23. End Function
    Roy

  5. #5
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    This would be better to use:
    VB Code:
    1. If Len(strValue) = 4 Then
    2.             FormatTime = Left(strValue, 2) & ":" & Right(strValue, 2)
    3.         ElseIf Len(Trim(strValue)) = 6 Then
    4.             FormatTime = Left(strValue, 2) & ":" & Mid(strValue, 3, 2) & ":" & Right(strValue, 2)
    5.         Else
    6.             MsgBox "Incorrect value, please re-enter", vbExclamation, "Format Time"
    7.         End If
    Roy

  6. #6
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    IROY55: How about 7:30 (730). It is a correct time.

  7. #7
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    How about this:
    If Len(strValue) = 3 Then strValue = "0" & strTime

    As you understand this is only an example and NOT the final solution.
    Roy

  8. #8
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    If you're just dealing with a string representing time, how about:
    VB Code:
    1. sTime = Format(Replace(sTime, ":", ""), "00:00")

  9. #9
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    I agree. But still I think we should teach users to put correct values as an input and do not do THEIR work for them.


    Code:
    If len(strValue)<5 then: FormatTime = Left(strValue, Len(strValue)-2) & ":" & Right(strValue, 2)

  10. #10
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    But still I think we should teach users to put correct values as an input and do not do THEIR work for them
    Entirely dissagree. Just like in Retail: customer is always right! Same thing here: you are the one to blame for "incorrect" output - not them. You are the programmer - so build your program as flexble as possible.
    Roy

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    I half agree /w IRoy, and half agree w/ andreys..... We provide extensive training for our product to our users. We teach then how to put the data in correctly. We let them know that if they don't do it right... things could get pretty ugly..... I think the quote is: "Build an idiot proof program and they'll build a better idiot." -- trust me IT'S TRUE!!!! They STILL find ways around things..... This is why we have error handling AND we STILL have tons of code to do validations.... it's a real shame sometimes..... but some of these users are quite creative too....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    Originally posted by andreys
    just check user input and display a message box:

    Code:
    if not IsDate(myTime) then
         MsgBox "Incorrect Time Format!"
    Else
         do things....
    End If
    This wont let user insert incorrect data, and in same time it will prompt to correct an input. This is flexible enought.
    1. User always right.
    2. If user not right, see #1.

  13. #13
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Andrey,
    you must look at the problem much broader: you cannot (and must not) enforce any user to follow your rules as they will refuse to work with your product(s). This is a very common mistake many developers do and result may be catastrofic: you may loose your job or completely re-design your product. Besides, that snippet code you posted isn't that flexible at all. If user types something like "123456" which can be interpreted as "12:34:56" (a valid time BTW) isn't valid DATE, so your code will produce a MsgBox ... Think over and I'm sure you will agree with us.
    Roy

  14. #14
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    I'm not trying to force user follow my rules, but (here is always but...) the element of education should be in place.

  15. #15
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    That's not case, you are missing one main point: as techgnome said "Build an idiot proof program and they'll build a better idiot."
    Roy

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169
    Thanks for the snippets guys. It's helped me lots. I now have a function to validate my time fields. However it's not complete, as it doesn't take all input possibilities into acount. I'll have to add to it once users start to complain

    Anyway, I thought it would be nice to post my results so far:

    Code:
    Public Function TimeCheck(ByVal strValue As String) As String
    Dim FormatTime As String
    Dim pos As Integer
        
        
        ' Check if the colon is already present. If so exit.
        If Len(strValue) = 4 Then
            pos = InStr(1, strValue, ":")
            If pos = 2 Then: TimeCheck = strValue: Exit Function
        Else
            If Len(strValue) = 5 Then
                pos = InStr(1, strValue, ":")
                If pos = 3 Then: TimeCheck = strValue: Exit Function
            End If
        End If
        
    
        
        If Len(Trim(strValue)) = 3 Then ' Three numbers have been entered.
            FormatTime = Left(strValue, 1) & ":" & Right(strValue, 2) ' Put colon after first digit.
            FormatTime = "0" & FormatTime ' Put in a zero before the first digit.
        ElseIf Len(strValue) = 4 Then   ' Four numbers have been entered.
            FormatTime = Left(strValue, 2) & ":" & Right(strValue, 2) ' Put colon after second digit.
        ElseIf Len(Trim(strValue)) = 6 Then  
            FormatTime = Left(strValue, 2) & ":" & Mid(strValue, 3, 2) & ":" & Right(strValue, 2)
        Else
            MsgBox "Incorrect value, please re-enter", vbExclamation, "Format Time"
        End If
    
        TimeCheck = FormatTime
    
    End Function
    Feel free to amend/suggest improvements
    Thanks again.

    Daz

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