Results 1 to 11 of 11

Thread: [RESOLVED] MSFlexGrid & Times

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Resolved [RESOLVED] MSFlexGrid & Times

    Hey Guys, I'm using MSFlexGrid as the title suggests.

    This one has had be stumped for some time, and not real sure on how to approach it as MSFlexGrid has querks of its own.

    I have times that need to be entered, they go in the format of

    mm:ss not really using hours or any type of dates. Hours can be optional, would like to add that in so finally it would be hh:mm:ss but would like the sequence as 0:00 which is the default.

    I have two "time" collumns. Then finally a totals column which adds the two together. My question is how to do this error free. I keep comming up with wierd numbers, and don't know if I'm exactly calling the thing right.

    Second Q: I'm using the mod that puts a floating textbox above the flex-grid. Question is, can I make it change on just those 3 columns (I have other stringed columns) to make it do a insert type thing, so pressing the number 1 will from the right update it to 0:01 then typing in #0 will make 0:10

    Thanks fellow bretherans for the help with the code. THe software I'm developing is coming along very fine! Gotta love VB.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: MSFlexGrid & Times

    For your first question, show us the code you currently have as we can amend it for you to remove the problems (far easier than writing it from scratch!).

    For the second question I would suggest using a different textbox for those columns, which does the formatting in the change event. Bear in mind tho that people may want to input the : characters themselves.

    Here is some possible code:
    VB Code:
    1. 'dont let the event fire when we are changing the value (infinite loop!)
    2. Static bSelfChanging as Boolean
    3.   If bSelfChanging Then Exit Sub
    4.   bSelfChanging = True
    5.  
    6. Dim sTime as String
    7. Dim iCursorpos as Integer
    8. 'get current value (& cursor pos)
    9.   iCursorpos = txtTimeInput.SelStart
    10.   sTime = Replace(txtTimeInput.Text,":","")
    11.  
    12. 'format
    13.   sTime = Format$(sTime,"#####:#0:00")
    14.   If Left$(sTime,1) = ":" Then sTime = Mid$(sTime,2)
    15.  
    16. 'set value (& return cursor pos)
    17.   txtTimeInput.Text = sTime
    18.   txtTimeInput.SelStart = iCursorpos
    19.  
    20.   bSelfChanging = False

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: MSFlexGrid & Times

    Honestly I removed the complete time code because of that very reason, was too buggy and was going to wait for an another possible approach.

    Oh, and further looking into the code used, I am using a non-floating textbox for writing to the flexgrid. I'm using this
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     MSFlexGrid1.HighLight = flexHighlightWithFocus
    4.  
    5. End Sub
    6.  
    7. Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
    8.  
    9.     With MSFlexGrid1
    10.         Select Case KeyAscii
    11.             Case 8
    12.                 If Not .Text = "" Then
    13.                     .Text = Left(.Text, Len(.Text) - 1)
    14.                 End If
    15.             Case 9 ' Tab
    16.                 If .Col + 1 = .Cols Then
    17.                     .Col = 0
    18.                     If .Row + 1 = .Rows Then
    19.                         .Row = 0
    20.                     Else
    21.                         .Row = .Row + 1
    22.                     End If
    23.                 Else
    24.                     .Col = .Col + 1
    25.                 End If
    26.             Case Else
    27.                 .Text = .Text & Chr(KeyAscii)
    28.         End Select
    29.     End With
    30. End Sub

    I don't know how I can still incorporate a control into the columns i wish to use. Si, where would I put the code at.... that is something I've been looking at for quite a while.
    Last edited by DJHotIce; Nov 6th, 2005 at 06:05 PM. Reason: did some more typing

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: MSFlexGrid & Times

    That code isn't using a textbox at all That means you can use a simpler version of the code I posted, eg:
    VB Code:
    1. Dim sTime as String
    2. 'get current value
    3.   sTime = Replace(.Text,":","")
    4.  
    5. 'format
    6.   sTime = Format$(sTime,"#####:#0:00")
    7.   If Left$(sTime,1) = ":" Then sTime = Mid$(sTime,2)
    8.  
    9. 'set value
    10.   .Text = sTime
    This should go just before the 'End With' line.

    As for the adding of time values, I think you can do this (where r is the row number):
    VB Code:
    1. With MSFlexGrid1
    2.   .TextMatrix(r, [i]totalcol[/i]) = CDate(.TextMatrix(r, [i]firstcol[/i])) + CDate(.TextMatrix(r, [i]secondcol[/i]))
    3. End With

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: MSFlexGrid & Times

    thanks si_the_geek I think that code will work! what I ment in the earlier post 'non-floating' textbox... I'm not using a textbox.... sorry for the confusion.

    THe code does look very straight foward though.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: MSFlexGrid & Times

    Si, the code works, to an extent...

    I am not real familiar with date/times and of the such with VB just yet so bear with me

    1) Time represented is not right.. 0:25 + 0:00 yields 2:25:00 AM
    2) No AM/PM and no hour representation unless needed. it should be :25 which is pronounced 25 Seconds.

    But the code does seem to add properly... In the meantime I will be searching my VB books for info.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: MSFlexGrid & Times

    You probably just need to format the values (before and after the CDate's) appropriately, eg:
    VB Code:
    1. Dim sTime as String
    2. With MSFlexGrid1
    3.   sTime = Format$(CDate(Format$(.TextMatrix(r, firstcol),"####0:00:00")) + CDate(Format$(.TextMatrix(r, secondcol),"####0:00:00"))"#####:##:00")
    4.   If Left$(sTime,2) = "::" Then sTime = Mid$(sTime,2)
    5.   .TextMatrix(r, totalcol) = sTime
    6. End With

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: MSFlexGrid & Times

    Si, here's what I did for the revised code you did above. I had this earlier, but then yours had an error, so I decided to revert to the code that was working... Only diff is I'm treating the Hours like min and mins like seconds... Hopefully there is an easier way... Marking thread resolved.

    VB Code:
    1. With MSFlexGrid1
    2.         With MSFlexGrid1
    3.           .TextMatrix(.Row, 10) = Format(CDate(.TextMatrix(.Row, 8)) + CDate(.TextMatrix(.Row, 9)), "h:mm")
    4.         End With
    5.     End With

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: [RESOLVED] MSFlexGrid & Times

    You should also format the values inside the CDate (as I did), to ensure that the values are converted to times accurately.
    but then yours had an error

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: [RESOLVED] MSFlexGrid & Times

    I recieved a type mismatch error, if memory serves correct.

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: [RESOLVED] MSFlexGrid & Times

    I've just checked my code and can't see anything obvious, so I would assume that the values could not be converted into times.

    Therefore I would recommend using a bit of validation, possibly like this:
    VB Code:
    1. Dim sTime as String
    2. Dim sTempTime as String
    3. With MSFlexGrid1
    4.   sTime = "0:00:00"
    5.   sTempTime = Format$(.TextMatrix(r, firstcol),"####0:00:00")
    6.   If IsDate(sTempTime) Then    sTime = sTime + CDate(sTempTime)
    7.   sTempTime = Format$(.TextMatrix(r, secondcol),"####0:00:00")
    8.   If IsDate(sTempTime) Then    sTime = sTime + CDate(sTempTime)
    9.   If Left$(sTime,2) = "::" Then sTime = Mid$(sTime,2)
    10.   .TextMatrix(r, totalcol) = sTime
    11. End With
    If you still get an error, you should let me know which line it occurs on, and what the text values are.

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