View Full Version : cycling itemselected on a listbox


bill crawley
04-10-2008, 11:25 PM
Hi All,

I have the following code that Cycles through a listbox that has columns associated to it:

Dim db As DAO.Database

Set db = CurrentDb

lstOrigin.SetFocus
For i = 0 To lstOrigin.ItemsSelected.Count - 1
txtAmount.SetFocus
For x = 1 To CInt(txtAmount.Text)
strsql = "insert into tmpJob select dbo_jtJob.* from dbo_jtJob where jtJobId = " & lstOrigin.Column(0)
db.Execute strsql
Next
Next

This code works fine and so if I multi-select the lstOrigin.Column(0) value changes correctly as expected..

I now have a second listbox also set with columns and set for multiselect:

The code I have for this is :

Dim i As Integer
Dim strCriteria As String

strCriteria = "key in ("
lstDestination.SetFocus
For i = 0 To lstDestination.ItemsSelected.Count - 1
strCriteria = strCriteria & lstDestination.Column(0) & ", "
Next
strCriteria = Left(strCriteria, Len(strCriteria) - 2) & ")"
DoCmd.OpenForm "JobCreateTemplate", , , strCriteria, , acDialog

Now in this one lstDestination.Column(0) always equals the last item in the items selected index. It iterates the loop the correct number of times. I cannot see what I've done different that the process works in the first lot of code and not the second.

This is Access 2003

RuralGuy
04-11-2008, 05:39 AM
As far as I can tell, neither functions should work as you expect. You are referencing Column(0) each time but there is nothing in your code to move to a different row of the ListBox. Bring up VBA Help on ItemsSelected to see an example of how to walk through the ListBox for ItemsSelected only.