VBA Code pulling listbox data

  • Thread starter Thread starter Secret Squirrel
  • Start date Start date
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
 
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)
 
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)
 
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
 
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
 
Back
Top