Results 1 to 24 of 24

Thread: [RESOLVED] Vb6: elapsed time + time in label1.caption (new problem)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Resolved [RESOLVED] Vb6: elapsed time + time in label1.caption (new problem)

    hey guys i have a problem with this project which SamOscarBrown wrote for me, the minutes function is working fine, but the hours is messed up, it keeps returning to same hour over and hour, same thing for days function it has same problem too
    i tried to fix but i failed, plz help fix this code
    here is code:
    --------------
    'Option Explicit

    Dim inFile As String
    Dim outFile As String
    Dim savedMinutes As Long
    Dim iDays As Integer
    Dim iHours As Integer
    Dim iMinutes As Integer

    Private Sub convertMinutesToHoursAndDays(savedMinutes)
    Dim dayString As String
    Dim hourString As String
    Dim minuteString As String
    Dim tempHours As Integer
    tempHours = savedMinutes Mod 1440
    iHours = tempHours / 60
    iMinutes = tempHours Mod 60
    iDays = Int(savedMinutes / 1440)
    If iDays = 1 Or iDays = 0 Then
    dayString = "Day"
    Else
    dayString = "Days"
    End If
    If iHours = 1 Or iHours = 0 Then
    hourString = "Hour"
    Else
    hourString = "Hours"
    End If
    If iMinutes = 1 Or iMinutes = 0 Then
    minuteString = "Min"
    Else
    minuteString = "Mins"
    End If
    Label1.Caption = "Started: " & CStr(iDays) & " " & dayString & " | " & CStr(iHours) & " " & hourString & " | " & CStr(iMinutes) & " " & minuteString
    End Sub

    Private Sub cmdExit_Click()
    Unload Me
    End Sub

    Private Sub Form_Load()
    Dim line1 As String, line2 As String, line3 As String, line4 As String, line5 As String, line6 As String, line7 As String, line8 As String
    tmrDelta.Interval = 1000
    inFile = FreeFile
    Open App.Path & "\time.txt" For Input As #inFile
    Line Input #inFile, line1
    Line Input #inFile, line2
    Text1.Text = line1
    savedMinutes = CLng(line2)
    convertMinutesToHoursAndDays (savedMinutes)
    tmrDelta.Enabled = True
    Close #inFile
    End Sub
    '
    Private Sub Form_Unload(Cancel As Integer)
    outFile = FreeFile
    Open App.Path & "\time.txt" For Output As #outFile
    Print #outFile, Text1.Text
    Print #outFile, CStr(savedMinutes)
    Close #outFile
    End Sub
    '
    Private Sub tmrDelta_Timer()
    Static l As Long
    l = l + 1
    If l Mod 60 = 0 Then savedMinutes = savedMinutes + 1
    convertMinutesToHoursAndDays (savedMinutes)
    End Sub

    ----------------------
    Last edited by geekmaro; Jan 14th, 2021 at 08:16 AM.

  2. #2
    Junior Member
    Join Date
    Jan 2017
    Posts
    30

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    does it return on startup, or when the hour is say meant to change to 14:00 from 13:00 and instead stays at 13:00?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by rjsnipe View Post
    does it return on startup, or when the hour is say meant to change to 14:00 from 13:00 and instead stays at 13:00?
    let me give you example:
    it shows 5 hours, then it must go to 6 hours, but instead it shows 5 hours, and once half hour passed it shows 6 hours and 30 min
    so after 30 min that is only when it goes to correct hour

  4. #4
    Junior Member
    Join Date
    Jan 2017
    Posts
    30

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    I'd use ticks instead, storing your own value will 'drift'

    I've not plumbed in the saving to file, but all you need to do is save the starting ticks, which you can do on Startup rather than shutdown, if you want. I've set the resolution on the timer to 100ms, just so you can see it counting seconds, if you want to test that it counts up the increments correctly, where I divide the diff / 1000 to get seconds, either remove it or shrink it to get time moving fast (by up to 1000x).

    VB6Timer.zip

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by rjsnipe View Post
    I'd use ticks instead, storing your own value will 'drift'

    I've not plumbed in the saving to file, but all you need to do is save the starting ticks, which you can do on Startup rather than shutdown, if you want. I've set the resolution on the timer to 100ms, just so you can see it counting seconds, if you want to test that it counts up the increments correctly, where I divide the diff / 1000 to get seconds, either remove it or shrink it to get time moving fast (by up to 1000x).

    VB6Timer.zip
    i have checked your code, after 30 seconds it turns in to 1 minute, it is not working
    and i also need to save label1 current time with text1.text
    and to be able to load it into form on form start

  6. #6
    Junior Member
    Join Date
    Jan 2017
    Posts
    30

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Change the Timer Function to

    Code:
    Private Function LongToTimeSpan(Diff As Long) As TimeSpan
        Dim Time As New TimeSpan
        Time.Days = Int(Diff / DayInSeconds)
        Diff = Diff Mod DayInSeconds
        Time.Hours = Int(Diff / HourInSeconds)
        Diff = Diff Mod HourInSeconds
        Time.Minutes = Int(Diff / MinuteInSeconds)
        Time.Seconds = Diff Mod MinuteInSeconds
        Set LongToTimeSpan = Time
    End Function
    I don't know the original spec, so not sure what you want to save and how, but you should be able to pick out the bits you need from both projects

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by rjsnipe View Post
    Change the Timer Function to

    Code:
    Private Function LongToTimeSpan(Diff As Long) As TimeSpan
        Dim Time As New TimeSpan
        Time.Days = Int(Diff / DayInSeconds)
        Diff = Diff Mod DayInSeconds
        Time.Hours = Int(Diff / HourInSeconds)
        Diff = Diff Mod HourInSeconds
        Time.Minutes = Int(Diff / MinuteInSeconds)
        Time.Seconds = Diff Mod MinuteInSeconds
        Set LongToTimeSpan = Time
    End Function
    I don't know the original spec, so not sure what you want to save and how, but you should be able to pick out the bits you need from both projects
    i appreciate your help but you made things harder for me
    i just needed someone to fix "SamOscarBrown" code
    but you give me your own code
    could you plz fix the code i gave you
    thanks in advance

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by geekmaro View Post
    could you plz fix the code i gave you
    The code you got from Sam was not optimally written -
    (and it changed, what we already had established as a reliable "time-delta-visualization" in a prior thread).

    Here is the code again...
    (enhanced about storing the delta-time at App-Shutdown - and re-initializing properly from the stored value):
    Code:
    Option Explicit
    
    Private mStartTime As Date, WithEvents tmrDelta As VB.Timer
    
    Private Sub Form_Load()
      mStartTime = Now - Val(GetSetting(App.EXEName, "Timing", "LastDelta", "0"))
      Set tmrDelta = Controls.Add("VB.Timer", "tmrDelta")
          tmrDelta.Interval = 500
    End Sub
    
    Private Sub tmrDelta_Timer()
      Me.Caption = GetElapsedTimeString(Now - mStartTime)
    End Sub
    
    Function GetElapsedTimeString(ByVal dT As Double) As String
      Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n | s \s\e\c"
      GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
    End Function
    
    Private Sub Form_Unload(Cancel As Integer)
      SaveSetting App.EXEName, "Timing", "LastDelta", Str(Now - mStartTime)
    End Sub
    HTH

    Olaf

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Never did say I was good at this...:-)
    Sam I am (as well as Confused at times).

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by SamOscarBrown View Post
    Never did say I was good at this...:-)
    well you are way better than me, that is for sure

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by Schmidt View Post
    The code you got from Sam was not optimally written -
    (and it changed, what we already had established as a reliable "time-delta-visualization" in a prior thread).

    Here is the code again...
    (enhanced about storing the delta-time at App-Shutdown - and re-initializing properly from the stored value):
    Code:
    Option Explicit
    
    Private mStartTime As Date, WithEvents tmrDelta As VB.Timer
    
    Private Sub Form_Load()
      mStartTime = Now - Val(GetSetting(App.EXEName, "Timing", "LastDelta", "0"))
      Set tmrDelta = Controls.Add("VB.Timer", "tmrDelta")
          tmrDelta.Interval = 500
    End Sub
    
    Private Sub tmrDelta_Timer()
      Me.Caption = GetElapsedTimeString(Now - mStartTime)
    End Sub
    
    Function GetElapsedTimeString(ByVal dT As Double) As String
      Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n | s \s\e\c"
      GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
    End Function
    
    Private Sub Form_Unload(Cancel As Integer)
      SaveSetting App.EXEName, "Timing", "LastDelta", Str(Now - mStartTime)
    End Sub
    HTH

    Olaf
    it works so far, but i dont understand where is it saving settings ? in this code i cant see where it saves last time
    SaveSetting App.EXEName

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    i found the path here:
    Call SaveSetting(App.EXEName, "Textboxes\frmMain", "Text1", frmMain.Text1.Text)

    will create a key under "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\YourAppName\Textboxes\frmMain

    but i need to save it in text file better, so i can delete textfile, that is way faster for me
    anyone know how to make code to save it into textfile ?

  13. #13
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by geekmaro View Post
    i found the path here:
    Call SaveSetting(App.EXEName, "Textboxes\frmMain", "Text1", frmMain.Text1.Text)

    will create a key under "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\YourAppName\Textboxes\frmMain

    but i need to save it in text file better, so i can delete textfile, that is way faster for me
    anyone know how to make code to save it into textfile ?
    Why not leave it in the registry as it is?
    (the registry-branch starts with "HKEY_CURRENT_USER\..." - and thus will ensure User-specific App-Settings - per "Logged on User")

    And in case you want to reset the saved Time-Delta-Value, you just have to call this line:
    SaveSetting App.EXEName, "Timing", "LastDelta", Str(0)

    HTH

    Olaf

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by Schmidt View Post
    Why not leave it in the registry as it is?
    (the registry-branch starts with "HKEY_CURRENT_USER\..." - and thus will ensure User-specific App-Settings - per "Logged on User")

    And in case you want to reset the saved Time-Delta-Value, you just have to call this line:
    SaveSetting App.EXEName, "Timing", "LastDelta", Str(0)

    HTH

    Olaf
    beause im already saving other settings in textfile that i load also, so it is better to keep all in one text file
    plz help me solve this one left problem
    thanks alot

  15. #15
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by geekmaro View Post
    beause im already saving other settings in textfile that i load also, so it is better to keep all in one text file
    plz help me solve this one left problem
    thanks alot
    Then you already have working code that saves and retrieves data to/from a text file that you can use as a guide to make it work however you would like it to.

  16. #16
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by geekmaro View Post
    beause im already saving other settings in textfile that i load also, so it is better to keep all in one text file
    plz help me solve this one left problem
    What's there to help with, when you are already able, to save and load "other values" to a textfile?

    The Value you need to save into your TextFile (as a String... in Form_Unload) is:
    sTimeDelta = Str(Now - mStartTime)

    And after you have loaded this Value (from your TextFile) again in Form_Load:
    sTimeDelta = LoadMyStringValueFromMyTextFile(...)

    ...you will have to subtract it from Now(), when you initialize the mStartTime (using the Val()-Function for Double-conversion is important):
    mStartTime = Now - Val(sTimeDelta)

    Edit: Also note, that in case there is no Value to be found in your TextFile currently (to fill sTimeDelta with) -
    ... sTimeDelta then remaining an empty, uninitialized String - a call to:
    Val(sTimeDelta) will return a nice "Zero-DoubleValue" from this uninitialized String-Variable nevertheless (meaning, that it will work also for this initial case)...

    HTH

    Olaf
    Last edited by Schmidt; Jan 14th, 2021 at 06:16 PM.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by Schmidt View Post
    What's there to help with, when you are already able, to save and load "other values" to a textfile?

    The Value you need to save into your TextFile (as a String... in Form_Unload) is:
    sTimeDelta = Str(Now - mStartTime)

    And after you have loaded this Value (from your TextFile) again in Form_Load:
    sTimeDelta = LoadMyStringValueFromMyTextFile(...)

    After that, you will have to subtract it from Now(), when you initialize the mStartTime (using the Val()-Function for Double-conversion is important):
    mStartTime = Now - Val(sTimeDelta)

    HTH

    Olaf
    sorry it didnt work i tried my best but i dont get it, this is my code to save all into textfile:
    ----
    Private Sub Command3_Click()
    outFile = FreeFile
    Open App.Path & "\1.txt" For Output As #outFile
    Print #outFile, Text1.Text
    Print #outFile, Text2.Text
    Print #outFile, Text3.Text
    Close #outFile
    End Sub
    -----


    and here my code to load all from textfile:
    ----
    Dim line1 As String, line2 As String, line3 As String
    Open App.Path & "\1.txt" For Input As #inFile
    Line Input #inFile, line1
    Line Input #inFile, line2
    Line Input #inFile, line3

    Text1.Text = line1
    Text2.Text = line2
    Text3.Text = line3
    Close #inFile
    ----
    could you please include timer in this code?
    thank you so much

  18. #18
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by geekmaro View Post
    sorry it didnt work i tried my best but i dont get it, this is my code to save all into textfile:
    ----
    Private Sub Command3_Click()
    outFile = FreeFile
    Open App.Path & "\1.txt" For Output As #outFile
    Print #outFile, Text1.Text
    Print #outFile, Text2.Text
    Print #outFile, Text3.Text
    Close #outFile
    End Sub
    -----


    and here my code to load all from textfile:
    ----
    Dim line1 As String, line2 As String, line3 As String
    Open App.Path & "\1.txt" For Input As #inFile
    Line Input #inFile, line1
    Line Input #inFile, line2
    Line Input #inFile, line3

    Text1.Text = line1
    Text2.Text = line2
    Text3.Text = line3
    Close #inFile
    ----
    could you please include timer in this code?
    No, because adding an additional single code-line for your new to introduce String-Variable sTimeDelta
    (into each of the two code-blocks) - should be utterly trivial - even for a VB-beginner.

    Olaf

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by Schmidt View Post
    No, because adding an additional single code-line for your new to introduce String-Variable sTimeDelta
    (into each of the two code-blocks) - should be utterly trivial - even for a VB-beginner.

    Olaf
    im real newbie
    i tried:
    Private Sub Command3_Click()
    sTimeDelta As String
    outFile = FreeFile
    Open App.Path & "\1.txt" For Output As #outFile
    Print #outFile, Text1.Text
    Print #outFile, Text2.Text
    Print #outFile, Text3.Text
    Close #outFile
    End Sub

    error: stament invalid = sTimeDelta As String
    i really need you to code it for me just this and wont bother you anymore

  20. #20
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by geekmaro View Post
    im real newbie
    i tried:
    Private Sub Command3_Click()
    sTimeDelta As String
    outFile = FreeFile
    Open App.Path & "\1.txt" For Output As #outFile
    Print #outFile, Text1.Text
    Print #outFile, Text2.Text
    Print #outFile, Text3.Text
    Close #outFile
    End Sub

    error: stament invalid = sTimeDelta As String
    i really need you to code it for me just this and wont bother you anymore
    As I wrote in #16...
    In "file-write-direction", the Value of the String-Variable sTimeDelta is equivalent to:
    sTimeDelta = Str(Now - mStartTime)

    So, what about using a single additional line in your above code,
    which makes use of the right-hand-side of the equivalence-assignment above
    (via an additional Print-Statement - instead of your incomplete Variable definition-line)?

    Olaf

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by Schmidt View Post
    As I wrote in #16...
    In "file-write-direction", the Value of the String-Variable sTimeDelta is equivalent to:
    sTimeDelta = Str(Now - mStartTime)

    So, what about using a single additional line in your above code,
    which makes use of the right-hand-side of the equivalence-assignment above
    (via an additional Print-Statement - instead of your incomplete Variable definition-line)?

    Olaf
    saving:
    Print #outFile, Str(Now - mStartTime)
    loading:
    mStartTime = Now - Val(line4)

    works like a charm
    thanks a lot for your help

  22. #22
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by geekmaro View Post
    saving:
    Print #outFile, Str(Now - mStartTime)
    loading:
    mStartTime = Now - Val(line4)
    Well, guess there's still hope ...

    Olaf

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6: elapsed time + time in label1.caption (new problem)

    Quote Originally Posted by Schmidt View Post
    Well, guess there's still hope ...

    Olaf
    im having another problem now
    when i deleted 1.txt
    and i run form, it shows started: 4400 days something like that
    how can i make time run from 0 in case there is no 1.txt file ?

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: [RESOLVED] Vb6: elapsed time + time in label1.caption (new problem)

    i got it
    mStartTime = Now
    that simple

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