For Each Next & ItemsSelected - strange behaviour

dajabo

Registered User.
Local time
Today, 17:18
Joined
Dec 16, 2004
Messages
17
I usually find an answer to problems on this forum in a matter of minutes searching, but this one I can't.

Can anyone shed light on why the For-Next loop in the following code cycles through twice?

Code:
For Each lstitem In ctl.ItemsSelected
Debug.Print ctl.ListIndex
        MsgPresentCheck = MsgPresentCheck & RemMsgParse(ctl.ItemData(lstitem), "MsgCollect")
    Next lstitem

ctl refers to a form ListBox with 1 (yes I've checked) item selected, but the Debug statement produces the same ListIndex number twice when run ... eg
5
5
or
10
10

The function RemMsgParse is definitely being called twice. Is there some peculiar quirk to ItemsSelected where it contains at least of 2 values? Or is there a special way it needs to be treated in a For-Each-Next statement?

Entire function below:

Code:
Public Function SetSelectedMsgsForPreview()

Dim Form As Form, ctl As Control
Dim lstitem, MsgPresentCheck

Call RemMsgClean

Set Form = Forms!MAIN_MANACC_RemittanceMsgs
Set ctl = Form!Clients_lst

lstitem = Null
MsgPresentCheck = Null

For Each lstitem In ctl.ItemsSelected
        MsgPresentCheck = MsgPresentCheck & RemMsgParse(ctl.ItemData(lstitem), "MsgCollect")
Debug.Print ctl.ListIndex
        
Next lstitem
    
If IsNull(MsgPresentCheck) Then
    SetSelectedMsgsForPreview = "none"
End If

End Function
 
Why are you doing this:

Code:
lstitem = Null
?
 
Banana said:
Why are you doing this:
Code:
lstitem = Null
?

While troubleshooting I just wanted to make absolutely sure this variable was null. I use the same variable in other procedures and this procedure is in the middle of a nest (or jungle) of them. I'm not very informed about the life-span of variables and just wanted to make sure while I got the code sorted.

D
 
The lifetime of variable is usually limited to a function or sub unless you specify otherwise, such as this:

Code:
Public lstItem As Variant

or

Code:
Static lstItem As Variant

In your case, lstItem and MessagePresent would be "empty".

Exactly what does the RemMsgParse function does? I see no reason for it to not go through the value twice.
 
Banana said:
Exactly what does the RemMsgParse function does? I see no reason for it to not go through the value twice.

Thanks for the tutorial. Knowing that you can make variables last beyond the function they are in would have been very handy writing this series of code, I'll look into it.

It doesn't change the issue here, though, which is that the FOR EACH NEXT loop above cycles through ctl.ItemsSelected twice and returns two identical values when there is only one item selected. This implies that ItemsSelected contains at least two values (or actually, variables) when there is only one item selected i the list box.

The RemMsgParse function collects records for the specified client from a table of messages to be appended to a Remittance advice, concatenates the actual messages into a single message, and appends a single record containing the single message to the same table, ready for printing in the Remittance advice report or, in this case, for preview by the user.

The described behaviour of the FOR EACH NEXT statement would not be a problem except that when the single message being created consists of only one original message, it has extra line-breaks added on the end, as the parse function is being executed twice - only the second time without any characters in the message, only the line breaks which are inserted for every subsequent message after the first one. The first message doesn't get a line break added but every next message has line-breaks before the message, if you see what I mean, to create a new paragraph for each message.

Sorry about the long explanation, and I know I could hack a way around this, but wondered if there is a reason for this behaviour.
 
So to see if I'm understanding you- you're telling me you stepped through the code and it looped For Each..Next twice?

Have you checked the collection? You can try this:
Code:
Debug.Print Ubound(lstItem)
right after the For Each... line. This will tell you in immediate windows how many there are in the collection. That will give you a clue as to whether lstItem itself is causing problem or not.

I'd also try removing the variable and refering the listbox directly just to see if the behavior persists. Another thing to try is to change from For Each..Next to For i from 1 to lstItem.ItemsSelected.Count.

I don't know why your code is acting like this; I've had used For Each...Next with listbox and never saw it looping through twice for a single selection.
 
Thanks very much Banana for your suggestions ... I'll try what you said in the next couple of days and post back. I think there is an error in the code somewhere too. I ought to tidy this section up.
D
 

Users who are viewing this thread

Back
Top Bottom