Results 1 to 25 of 25

Thread: [RESOLVED] how do i add to an array comboxes strings?

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] how do i add to an array comboxes strings?

    hey,
    i have like 10 comboboxes that i need to add the same values
    now instead of adding for each combobox
    i want to add it to all the combos at once
    this is how i am using right now
    Code:
        DtDay(1).AddItem "06:00"
        DtDay(1).AddItem "06:30"
        DtDay(1).AddItem "07:00"
        DtDay(1).AddItem "07:30"
        DtDay(1).AddItem "08:00"
        DtDay(1).AddItem "08:30"
        DtDay(1).AddItem "09:00"
        DtDay(1).AddItem "09:30"
        DtDay(1).AddItem "10:00"
        DtDay(1).AddItem "10:30"
        DtDay(1).AddItem "11:00"
        DtDay(1).AddItem "11:30"
        DtDay(1).AddItem "12:00"
        DtDay(1).AddItem "12:30"
        DtDay(1).AddItem "13:00"
        DtDay(1).AddItem "13:30"
        DtDay(1).AddItem "14:00"
        DtDay(1).AddItem "14:30"
        DtDay(1).AddItem "15:00"
        DtDay(1).AddItem "15:30"
        DtDay(1).AddItem "16:00"
        DtDay(1).AddItem "16:30"
        DtDay(1).AddItem "17:00"
        DtDay(1).AddItem "17:30"
        DtDay(1).AddItem "18:00"
        DtDay(1).AddItem "18:30"
        DtDay(1).AddItem "19:00"
        DtDay(1).AddItem "19:30"
        DtDay(1).AddItem "20:00"
        DtDay(1).AddItem "20:30"
        DtDay(1).AddItem "21:00"
        DtDay(1).AddItem "21:30"
        DtDay(1).AddItem "22:00"
        DtDay(1).AddItem "22:30"
        DtDay(1).AddItem "23:00"
        DtDay(1).AddItem "23:30"
        DtDay(1).ListIndex = 0
    Code:
        DtDay(2).AddItem "06:00"
        DtDay(2).AddItem "06:30"
        DtDay(2).AddItem "07:00"
        DtDay(2).AddItem "07:30"
        DtDay(2).AddItem "08:00"
        DtDay(2).AddItem "08:30"
        DtDay(2).AddItem "09:00"
        DtDay(2).AddItem "09:30"
        DtDay(2).AddItem "10:00"
        DtDay(2).AddItem "10:30"
        DtDay(2).AddItem "11:00"
        DtDay(2).AddItem "11:30"
        DtDay(2).AddItem "12:00"
        DtDay(2).AddItem "12:30"
        DtDay(2).AddItem "13:00"
        DtDay(2).AddItem "13:30"
        DtDay(2).AddItem "14:00"
        DtDay(2).AddItem "14:30"
        DtDay(2).AddItem "15:00"
        DtDay(2).AddItem "15:30"
        DtDay(2).AddItem "16:00"
        DtDay(2).AddItem "16:30"
        DtDay(2).AddItem "17:00"
        DtDay(2).AddItem "17:30"
        DtDay(2).AddItem "18:00"
        DtDay(2).AddItem "18:30"
        DtDay(2).AddItem "19:00"
        DtDay(2).AddItem "19:30"
        DtDay(2).AddItem "20:00"
        DtDay(2).AddItem "20:30"
        DtDay(2).AddItem "21:00"
        DtDay(2).AddItem "21:30"
        DtDay(2).AddItem "22:00"
        DtDay(2).AddItem "22:30"
        DtDay(2).AddItem "23:00"
        DtDay(2).AddItem "23:30"
        DtDay(2).ListIndex = 0

    BTW
    i need it to enter by 15 minutes each
    ex.
    06:00
    06:15
    06:30
    until 23:45
    and so on....
    tnx in advanced
    salsa31

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: how do i add to an array comboxes strings?

    You can use some some basic loops for this.
    Sample for the hours only
    Code:
    Dim lHour As Long
    
    For lHour = 0 To 23
      dtDay(0).AddItem = Format(lHour, "00") & ":00"
    Next lHour
    See if you can use this sample to create a slightly more advanced version to add half hours or quarter hours.

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: how do i add to an array comboxes strings?

    Code:
    Private Sub Command1_Click()
      AddTimeSlots Combo1, 60
    End Sub
    
    Private Sub Command2_Click()
      AddTimeSlots Combo1, 30
    End Sub
    
    Private Sub Command3_Click()
      AddTimeSlots Combo1, 15
    End Sub
    
    Private Sub AddTimeSlots(oCMB As ComboBox, ByVal lMinuteStep As Long)
      Dim lHour As Long, lMin As Long
      
      oCMB.Clear
      For lHour = 0 To 23
        For lMin = 0 To 59 Step lMinuteStep
          oCMB.AddItem Format(lHour, "00") & ":" & Format(lMin, "00")
        Next lMin
      Next lHour
      oCMB.ListIndex = 0
    End Sub

  4. #4

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i add to an array comboxes strings?

    Quote Originally Posted by Arnoutdv View Post
    Code:
    Private Sub Command1_Click()
      AddTimeSlots Combo1, 60
    End Sub
    
    Private Sub Command2_Click()
      AddTimeSlots Combo1, 30
    End Sub
    
    Private Sub Command3_Click()
      AddTimeSlots Combo1, 15
    End Sub
    
    Private Sub AddTimeSlots(oCMB As ComboBox, ByVal lMinuteStep As Long)
      Dim lHour As Long, lMin As Long
      
      oCMB.Clear
      For lHour = 0 To 23
        For lMin = 0 To 59 Step lMinuteStep
          oCMB.AddItem Format(lHour, "00") & ":" & Format(lMin, "00")
        Next lMin
      Next lHour
      oCMB.ListIndex = 0
    End Sub
    tnk you my friend
    the combo is array
    DtDay(1).AddItem
    DtDay(2).AddItem

  5. #5

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i add to an array comboxes strings?

    Quote Originally Posted by Arnoutdv View Post
    You can use some some basic loops for this.
    Sample for the hours only
    Code:
    Dim lHour As Long
    
    For lHour = 0 To 23
      dtDay(0).AddItem = Format(lHour, "00") & ":00"
    Next lHour
    See if you can use this sample to create a slightly more advanced version to add half hours or quarter hours.
    this shows each hour
    9:00
    10:00
    i need the combo to display
    9:00
    9:15
    9:30
    9:45
    and so on until 23:45

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

    Re: how do i add to an array comboxes strings?

    Or this brute force method with Strings:
    Code:
    Private Sub Form_Load()
    Dim i As Integer
    Dim j As Integer
    Dim t1 As String
    Dim t2 As Integer
    t1 = "05"
    t2 = "00"
    For j = 0 To 23
    If CInt(t1) < 23 Then
        t1 = CStr(CInt(t1) + 1)
        dtDay(1).AddItem t1 & ":00"
        For i = 1 To 3
            t2 = CStr(CInt(t2) + 15)
            dtDay(1).AddItem t1 & ":" & t2
        Next i
        t2 = "00"
                End If
    Next j
    dtDay(1).ListIndex = 0
    End Sub

  7. #7

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i add to an array comboxes strings?

    Quote Originally Posted by SamOscarBrown View Post
    Or this brute force method with Strings:
    Code:
    Private Sub Form_Load()
    Dim i As Integer
    Dim j As Integer
    Dim t1 As String
    Dim t2 As Integer
    t1 = "05"
    t2 = "00"
    For j = 0 To 23
    If CInt(t1) < 23 Then
        t1 = CStr(CInt(t1) + 1)
        dtDay(1).AddItem t1 & ":00"
        For i = 1 To 3
            t2 = CStr(CInt(t2) + 15)
            dtDay(1).AddItem t1 & ":" & t2
        Next i
        t2 = "00"
                End If
    Next j
    dtDay(1).ListIndex = 0
    End Sub
    hey sami tnx amigo
    that did the trick

  8. #8

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i add to an array comboxes strings?

    hey sami
    it dosnt show the full hour
    it skipps it
    09:15
    09:30
    09:45 >> here dosnt show after 10:00
    10:15

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: how do i add to an array comboxes strings?

    Have you tried post #3?
    Code:
    AddTimeSlots dtDay(1), 60
    AddTimeSlots dtDay(2), 30
    AddTimeSlots dtDay(3), 15

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

    Re: how do i add to an array comboxes strings?

    Yes it does....did you copy-paste?

  11. #11

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i add to an array comboxes strings?

    Quote Originally Posted by Arnoutdv View Post
    Have you tried post #3?
    Code:
    AddTimeSlots dtDay(1), 60
    AddTimeSlots dtDay(2), 30
    AddTimeSlots dtDay(3), 15
    i get type mismatch

  12. #12
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: how do i add to an array comboxes strings?

    Is dtDay() an array of ComboBoxes?

  13. #13
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: how do i add to an array comboxes strings?

    Salsa

    Sorry, but I've gotta go with Sam on this one.

    My combox is named Combo1.

    Code:
            Dim i As Integer
            Dim j As Integer
            Dim t1 As String
            Dim t2 As Integer
            t1 = "05"
            t2 = "00"
            With Combo1
                For j = 9 To 13
                    If CInt(t1) < 23 Then
                        t1 = CStr(CInt(t1) + 1)
                        .AddItem t1 & ":00"
                        For i = 1 To 3
                            t2 = CStr(CInt(t2) + 15)
                            txt = txt & vbCrLf
                            .AddItem t1 & ":" & t2
                        Next i
                        t2 = "00"
                    End If
                Next j
            End With
    Not as many as Sam's, but no "skipped" interval ..

    Name:  combo1.png
Views: 173
Size:  3.2 KB

    .. and scrolling down produces

    Name:  combo2.png
Views: 172
Size:  3.3 KB

    Spoo

  14. #14
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: how do i add to an array comboxes strings?

    Salsa

    BTW, here is similar for ComboBox array

    Code:
            For a = 0 To 1
                t1 = "05"
                t2 = "00"
                With Combo2(a)
                    For jj = 9 To 13
                        If CInt(t1) < 23 Then
                            t1 = CStr(CInt(t1) + 1)
                            .AddItem t1 & ":00"
                            For i = 1 To 3
                                t2 = CStr(CInt(t2) + 15)
                                txt = txt & vbCrLf
                                .AddItem t1 & ":" & t2
                            Next i
                            t2 = "00"
                        End If
                    Next jj
                End With
            Next a
    Name:  combo3.png
Views: 182
Size:  3.7 KBName:  combo4.png
Views: 228
Size:  3.8 KB

    Spoo

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

    Re: how do i add to an array comboxes strings?

    just because...
    Code:
        Dim x As Single
        For x = 6 To 23.75 Step 0.25
            Debug.Print Int(x) & ":" & Format((x - Int(x)) * 60, "00")
        Next
    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}

  16. #16

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i add to an array comboxes strings?

    Quote Originally Posted by LaVolpe View Post
    just because...
    Code:
        Dim x As Single
        For x = 6 To 23.75 Step 0.25
            Debug.Print Int(x) & ":" & Format((x - Int(x)) * 60, "00")
        Next
    DtDay(1).AddItem???
    hello sir long time good to see you tnk you

  17. #17

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i add to an array comboxes strings?

    Quote Originally Posted by Arnoutdv View Post
    Is dtDay() an array of ComboBoxes?
    yes it is

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

    Re: how do i add to an array comboxes strings?

    Quote Originally Posted by salsa31 View Post
    DtDay(1).AddItem???
    hello sir long time good to see you tnk you
    Think you might have been able to answer that yourself:
    Code:
        Dim x As Single
        For x = 6 To 23.75 Step 0.25
            DtDAy(1).AddItem Int(x) & ":" & Format((x - Int(x)) * 60, "00")
        Next
    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}

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

    Re: how do i add to an array comboxes strings?

    BOY, Lavolpe!, that was simple! (No, I am not calling you 'boy'!!!)

    Sure would be nice to have a grasp of the language like you do....

  20. #20
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: how do i add to an array comboxes strings?

    Also if it is an array and you want to populate multiple combos with the same data then do not use
    DtDAy(1)

    Instead use a loop
    Code:
    Dim x As Single, Y as Integer
    For Y=1 to 10
        For x = 6 To 23.75 Step 0.25
            DtDAy(Y).AddItem Int(x) & ":" & Format((x - Int(x)) * 60, "00")
        Next
    Next
    That would populate 10 combo boxes with the same data assuming that 10 or more exist and have the indexes of 1 through 10

  21. #21
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: how do i add to an array comboxes strings?

    Be careful with those floating point loop indexes though. You can get some funny stuff if you're not careful. The fraction 0.25 is going to go to binary fairly cleanly. However, what if we wanted thirds?

    Take a look at the following two examples:

    Code:
    
    Private Sub Command1_Click()
        Dim n As Single
        For n = 1! To 8!/3! Step 1!/3!
            Debug.Print n
        Next n
    End Sub
    
    ... versus ...

    Code:
    
    Private Sub Command1_Click()
        Dim n As Single
        For n = 1! To 7!/3! Step 1!/3!
            Debug.Print n
        Next n
    End Sub
    
    Now, in the first one, 8/3 = 2.666... and everything runs as expected.

    However, in the second one, 7/3 = 2.333... but we never get to that number. The loop stops executing when n = 2.

    I'll let y'all ponder why that is. It's all about equality (and inequality) in binary.

    Best Regards,
    Elroy

    EDIT1: Just for the purists, the loop stops reporting when n = 2. As is the case with all loops, n will be incremented by the step one more time once the code falls out of the loop.
    Last edited by Elroy; Jun 19th, 2017 at 09:20 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

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

    Re: how do i add to an array comboxes strings?

    Elroy, granted. However, since the request for quarter hour increments, knew that wouldn't be a problem which is why the loop is an easy choice.
    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}

  23. #23
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: how do i add to an array comboxes strings?



    ---
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

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

    Re: how do i add to an array comboxes strings?

    I'd do it with Date-Variables (adding them up with the Interval in question)...

    Quite flexible compared to the other routines - and the Parameters can be passed-in as Strings,
    which are a bit more readable then (basically the same format as the later expected output).

    Code:
    Option Explicit
     
    Private Sub Form_Load()
      FillTimeSlots List1, "23:45", "0:15", "0:05" 'to test "crossing the midnight point"
    
      FillTimeSlots Combo1, "6:45", "23:45", "0:15"
    End Sub
    
    Private Sub FillTimeSlots(Lst, ByVal tStart As Date, ByVal tEnd As Date, ByVal tDelta As Date, Optional ByVal sFmt As String = "hh:mm")
        Lst.Clear
        If tEnd < tStart Then tEnd = DateAdd("d", 1, tEnd) 'just in case we "go over midnight"
        Do:  Lst.AddItem Format$(tStart, sFmt)
             tStart = tStart + tDelta
        Loop Until tStart > DateAdd("s", 1, tEnd)
    End Sub
    Olaf

  25. #25

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i add to an array comboxes strings?

    thank you very much all of you

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