Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating an array from Value List

Status
Not open for further replies.

AT76

Technical User
Apr 14, 2005
460
US
Hi,

I'm working on an app in which the user enters multiple items (s3txt2) into a value list (s3lst3):

Private Sub s3btn2_Click()
s3txt2.SetFocus
If Trim(s3txt2.Text) <> "" Then
s3lst3.AddItem (s3txt2.Text)
End If
s3txt2.Text = ""
End Sub

However, when the code is trying to loop through all items selected it only reads 1 item. ( s3lst3.ItemsSelected.Count = 1)

Is there another way of creating an array so I'm able to capture the correct COunt of Items entered?

Thank you!

 
Some more feedback:

's3lst3.ListCount' gives the correct # of Items selected. But I'm unable to use it as follows:

For Each s In s3lst3.ListCount

Next s

 
How is the user entering the list? I infer that "s3txt2" is a text box. Is that correct?

If its being entered as "A;B;C;D" with ";" serving as a delimiter then
Code:
Dim myArray() As String
myArray = Split(s3text2.Text, ";")
MsgBox "Items in Array = " & Ubound(myArray) + 1

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
I get "Object required" after line:

myArray = Split(s3text2.Text, ";")
 
Users are entering the items thru a textbox. Then they click an 'ADD' button which invokes the code I attached earlier.

Ok code is going thru now.

Each time the user enter an item it saves it into myArray, but when it leaves the code to enter the next item the myArray goes back to 0.

Private Sub s3btn2_Click()
s3txt2.SetFocus

If Trim(s3txt2.Text) <> "" Then

s3txt2.SetFocus
myArray = Split(Me.s3txt2.Text, ";")
MsgBox "Items in Array = " & UBound(myArray) + 1

s3lst3.AddItem (s3txt2.Text)
End If

s3txt2.Text = ""
End Sub
 
I have been able to capture the total # of items entered by:

s3lst3.ListCount

However when I try to run line:

For Each s In s3lst3.ListCount

I get the following error message: For each may only iterate over a collection object or an array.

Any ideas how to fix this?
 
Hi!

To iterate using the For Each you need to use the ItemsSelected Collection:

Dim s As Variable

For Each s In s3lst3.ItemsSelected
Do your stuff here
Next s

This assumes that the list box is multiselect and that there are items selected.

If you want to go through all of the items in the list box then you can do:

Dim i As Integer

For i = 0 To s3lst3.ListCount - 1
do your stuff here using s3lst3.Column(0, i)
Next i

It may be .Columns instead of Column, I don't remember for sure.

hth


Jeff Bridgham
Purdue University
Graduate School
Data Analyst
 
The items in a ListBox are not held in a collection ... thus For Each s In s3lst3.ListCount can't be used to access them. You need
Code:
Dim n As Integer
For n = 0 To s3lst3.ListCount - 1
   s = s3lst3.List(n)
   [COLOR=green]' Do something with 's'[/color]
Next n

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Thank you Jebri and Golom... I appreciate your help!

- AT
 
Actually Golom, both jebry's methods are viable.

you CAN iterate thru the ItemSelected COLLECTION,
of a list box.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top