Results 1 to 9 of 9

Thread: [RESOLVED] timevalue additon with array error

  1. #1

    Thread Starter
    Addicted Member scottlafoy's Avatar
    Join Date
    Feb 2009
    Location
    Calgary Alberta, Canada
    Posts
    148

    Resolved [RESOLVED] timevalue additon with array error

    Hi,
    I am trying to add the time values from part of an array. the array is 0 to 27 but I only want to add parts of it and have it displayed in lanaweek1. I am getting an error on line 1 of the code below, saying type mismatch. The lanatotal(i).text is in "hh:mm" format

    Code:
    Private Sub lanatotal_Change(Index As Integer)
      timetoadd = TimeValue(lanatotal(0).Text) + TimeValue(lanatotal(1).Text) + TimeValue(lanatotal(2).Text)
      lanaweek1 = Format(timetoadd, "hh:mm")
    End Sub
    thanks in advance for any suggestions

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: timevalue additon with array error

    You will have to use the same logic that I gave you last time in combination with "On Error Resume Next"

    The reason: You are using textbox change event... so even if you type say 3 in that textbox, the event will fire and you will get the type mismatch error....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: timevalue additon with array error

    1. The format is wrong. Should be hh:nn (mins) vs hh:mm (months), no?
    2. So the textboxes contain hours & minutes each? And you want to total 3 of them? For example if (0) = 01:05, (1) = 02:20 & (3) = 03:30, the result would be 06:55 ?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Addicted Member scottlafoy's Avatar
    Join Date
    Feb 2009
    Location
    Calgary Alberta, Canada
    Posts
    148

    Re: timevalue additon with array error

    ok, I will try that koolsid. LaVolpe that is exactly what I am trying to do, I want (0) and (1) and (2) to add together even if one of them is missing. The format they are in while in the textbox is H:mm eg. 3:15 or 10:45

    Thanks,

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: timevalue additon with array error

    If I am interpreting correctly, this can tally all the times in the 27 textboxes:
    Code:
    Private Sub lanatotal_Change(Index As Integer)
        Dim subTime As Date, totalHrs As Long, totalMins As Long
        Dim I As Long
        For I = lanatotal.LBound To lanatotal.UBound
            If IsDate(lanatotal(I)) Then
                subTime = CDate(lanatotal(I))
                totalHrs = totalHrs + Hour(subTime)
                totalMins = totalMins + Minute(subTime)
            End If
        Next
        lanaweek1.Text = "Hrs:" & totalHrs + totalMins \ 60 & " Mins: " & totalMins - ((totalMins \ 60) * 60)
    End Sub
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Addicted Member scottlafoy's Avatar
    Join Date
    Feb 2009
    Location
    Calgary Alberta, Canada
    Posts
    148

    Re: timevalue additon with array error

    Thank you, this works, but is there a way to only add up parts of the array, eg (0) to (6) to view in textbox1 and (7) to (13) to view in textbox2?

    thank you,
    C:\DOS
    C:\DOS\RUN
    RUN\DOS\RUN

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: timevalue additon with array error

    Sure, create a function and call it from the change event
    Code:
    Private Sub TallyTimes(StartIndex As INteger, StopIndex As Integer, destTextBox As VB.TextBox)
       Dim subTime As Date, totalHrs As Long, totalMins As Long
        Dim I As Long
        For I = StartIndex To StopIndex
            If IsDate(lanatotal(I)) Then
                subTime = CDate(lanatotal(I))
                totalHrs = totalHrs + Hour(subTime)
                totalMins = totalMins + Minute(subTime)
            End If
        Next
        destTextBox.Text = "Hrs:" & totalHrs + totalMins \ 60 & " Mins: " & totalMins - ((totalMins \ 60) * 60)
    End Sub
    
    Private Sub lanatotal_Change(Index As Integer)
      Select Case Index
      Case 0 To 6
         TallyTimes 0, 6, lanaweek
      Case 7 to 13
         TallyTimes 7, 13, textbox2
      End Select
    End Sub
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: timevalue additon with array error

    @LaVolpe, This should be shorter and quicker:
    Code:
    Private Sub TallyTimes(StartIndex As Integer, StopIndex As Integer, destTextBox As VB.TextBox)
        Dim totalTime As Date
        Dim I As Long
        
        For I = StartIndex To StopIndex
            If IsDate(lanatotal(I)) Then
                totalTime = totalTime + TimeValue(CDate(lanatotal(I)))
            End If
        Next
        destTextBox.Text = Int(totalTime) * 24 + Hour(totalTime) & ":" & Minute(totalTime)
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  9. #9

    Thread Starter
    Addicted Member scottlafoy's Avatar
    Join Date
    Feb 2009
    Location
    Calgary Alberta, Canada
    Posts
    148

    Re: timevalue additon with array error

    I got this working. Thank you!
    C:\DOS
    C:\DOS\RUN
    RUN\DOS\RUN

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