VBA Code pulling listbox data

S

Secret Squirrel

I'm using the following code to pull the values from my listbox and enter
them into a textbox. How do I get rid of the "OR" after the last item is
selected?

This is an example of what is showing up in my textbox:

1 OR 2 OR 3 OR 4 OR

Here is the code I'm using:

Dim i As Integer
Dim strMultiValue
For i = 0 To SkillList.ListCount - 1
If SkillList.Selected(i) Then
strMultiValue = strMultiValue & SkillList.Column(0, i) & " OR "
End If
Next i
If Len(strMultiValue) > 2 Then
strMultiValue = Left(strMultiValue, Len(strMultiValue) - 1)
Me.Text494.Value = strMultiValue
Else
Me.Text494.Value = Null
End If
 
D

Dale Fye

Here is the way I would do it.

1. Instead of looping through all of the items, just loop through the
ItemsSelected collection.

Dim strMultiValue as string
Dim varItem as variant

For each varItem in me.SkillList.ItemsSelected
strMultiValue = strMultiValue & " OR " & me.SkillList.column(0, varItem)
Next

'Strip the first ' OR ' off the list
strMultiValue = Mid(strMultiValue, 5)
 
S

Secret Squirrel

Perfect! Works like a charm. Thanks Dale!



Dale Fye said:
Here is the way I would do it.

1. Instead of looping through all of the items, just loop through the
ItemsSelected collection.

Dim strMultiValue as string
Dim varItem as variant

For each varItem in me.SkillList.ItemsSelected
strMultiValue = strMultiValue & " OR " & me.SkillList.column(0, varItem)
Next

'Strip the first ' OR ' off the list
strMultiValue = Mid(strMultiValue, 5)
 
S

stevenaramos@outlook

Secret Squirrel said:
I'm using the following code to pull the values from my listbox and enter
them into a textbox. How do I get rid of the "OR" after the last item is
selected?

This is an example of what is showing up in my textbox:

1 OR 2 OR 3 OR 4 OR

Here is the code I'm using:

Dim i As Integer
Dim strMultiValue
For i = 0 To SkillList.ListCount - 1
If SkillList.Selected(i) Then
strMultiValue = strMultiValue & SkillList.Column(0, i) & " OR "
End If
Next i
If Len(strMultiValue) > 2 Then
strMultiValue = Left(strMultiValue, Len(strMultiValue) - 1)
Me.Text494.Value = strMultiValue
Else
Me.Text494.Value = Null
End If




Is it ok if i join Your Group
 
D

Dale Fye

What do you mean, "Join our group"?

This is a Microsoft Access newsgroup. Anyone who needs assistance with
Access is welcome as long as they are polite. You will generally find the
answers to most of your Access database and application development issues
within 24 hours.

Welcome!

Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top