Results 1 to 3 of 3

Thread: LISTBOX and multiple and variables ??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    31

    LISTBOX and multiple and variables ??

    I have a form in XLS (2002) that has a listbox. In form_intialize () event 10 dates fill the list box (today +1 through 10).

    User will select 4 dates, i am trying to store all 4 values selected in variables.
    I know that it recognizes only the last selected but was hoping for a work around.
    I have the selected being shown in Msgbox and then stored in myVariable, but when loops, overwrites last variable stored.

    this is what i have:

    Dim x As Integer

    For x = 0 To lstDates.ListCount - 1
    If lstDates.Selected(x) = True Then
    MsgBox lstDates.List(x)
    myVariable = lstDates.List(x)
    End If
    Next x


  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Here is an example I put together for you to demonstrate the logic.
    VB Code:
    1. Option Explicit
    2. 'Set the MultiSelect property of the listbox to "2 - Extended" manually.
    3. Private myVariable As String
    4.  
    5. Private Sub Form_Initialize()
    6.  
    7.     Dim i As Integer
    8.    
    9.     lstDates.AddItem Date
    10.     For i = 1 To 10
    11.         lstDates.AddItem DateAdd("d", i, Date)
    12.     Next
    13.    
    14. End Sub
    15.  
    16. Private Sub cmdLoadVar_Click()
    17.  
    18.     Dim x As Integer
    19.    
    20.     For x = 0 To lstDates.ListCount - 1
    21.         If lstDates.Selected(x) = True Then
    22.             MsgBox lstDates.List(x)
    23.             myVariable = myVariable & lstDates.List(x) & "|"
    24.         End If
    25.     Next x
    26.    
    27. End Sub
    28.    
    29. Private Sub cmdReadVar_Click()
    30.  
    31.     Dim arArray() As String
    32.     Dim i As Integer
    33.    
    34.     Erase arArray
    35.     ReDim arArray(lstDates.ListCount - 1) As String
    36.     arArray = Split(myVariable, "|")
    37.     For i = 0 To UBound(arArray) - 1
    38.         MsgBox arArray(i)
    39.     Next
    40.    
    41. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    31
    Thanks for your reply.
    I put in a msgbox after the line "myVariable = myVariable & lstDates.list(x) to see both dates selected returned.
    (I deactivated the other msgbox in the line above).
    I get 2 msgboxes, 1 with the first date i select, and 2ns with both dates selected. What is the code doing ?
    not sure what the code in cmdreadVar () is soppose to do, does not do anything when clicked.

    thanks again for your help

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