PDA

Click to See Complete Forum and Search --> : LISTBOX and multiple and variables ??


bs51746
May 26th, 2004, 10:36 AM
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

:rolleyes:

RobDog888
May 26th, 2004, 10:54 AM
Here is an example I put together for you to demonstrate the logic.
Option Explicit
'Set the MultiSelect property of the listbox to "2 - Extended" manually.
Private myVariable As String

Private Sub Form_Initialize()

Dim i As Integer

lstDates.AddItem Date
For i = 1 To 10
lstDates.AddItem DateAdd("d", i, Date)
Next

End Sub

Private Sub cmdLoadVar_Click()

Dim x As Integer

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

End Sub

Private Sub cmdReadVar_Click()

Dim arArray() As String
Dim i As Integer

Erase arArray
ReDim arArray(lstDates.ListCount - 1) As String
arArray = Split(myVariable, "|")
For i = 0 To UBound(arArray) - 1
MsgBox arArray(i)
Next

End Sub

bs51746
May 26th, 2004, 02:16 PM
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