Discussion Locked

VB Code

0 helpful
12 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I doing this program where by The person selects parts from a list box, then clicks a transfer button to place the selected item in another list box (eg shopping trolley)on the same form.
The problem I have is when I transfer the contents of the shopping trolley to another form like a print preview, listing all items selected from the first form, I can't get it to work.

I was wondering if someone else may have a piece of code that I could use to fix my problem.

Thanks for your help
Claude

12 replies

by meganetcomputers

There are several ways you can do this:
1. even though this may take up memory but you could load the form that you are trying to send this information into without it being visable.
2. You can create several global veriable to store your decisions then just load those variable into the list box when you load the form.

by andrew

Consider using a class (or simply a collection) to house the data for the forms. A single variable for the class instance or collection could be declared with global scope, then you could have one form load it up with user selections, and another form print it or do other processing. The class could also hold other information, like user ID or order ID, etc.

by yatst

A little less complicated then a class: As the person is selecting items build a Global string like this.

Dim LstData as string

Each time an item is selected

lstdata = lstdata & me.lstbox.itemselected

(Add a carriage return at the end if you need it or make the control on the report so that the text will wrap in it).

Good Luck

by shanghai sam

Solution 1.
Private Function GetSelectedListItems() As Variant
Dim I As Integer
Dim iMaxSelected As Integer
Dim sSelectedList() As String

If List1.ListCount > 0 Then

ReDim sSelectedList(List1.ListCount - 1)

For I = 0 To List1.ListCount - 1
If List1.Selected(I) Then
sSelectedList(iMaxSelected) = List1.List(I)
iMaxSelected = iMaxSelected + 1

End If
Next I

End If

If iMaxSelected Then
ReDim Preserve sSelectedList(iMaxSelected - 1)
GetSelectedListItems = sSelectedList
Else
GetSelectedListItems = Empty
End If

End Function

You could then declare a variant and assign it the return value of the function:

Private Sub test()
Dim vSelectedItems As Variant
Dim vItem As Variant

vSelectedItems = GetSelectedListItems()
If IsArray(vSelectedItems) Then
For Each vItem In vSelectedItems
Debug.Print vItem
Next vItem
End If
End Sub

by sl-campbell

--------------------------------
Solution 2.

You could also just make the proceedure a Sub, declare a module level variable, and write it like this:

Public mp_vSelectedList As Variant

Private Sub GetSelectedListItems()
Dim I As Integer
Dim iMaxSelected As Integer

If List1.ListCount > 0 Then

ReDim mp_vSelectedList(List1.ListCount - 1)

For I = 0 To List1.ListCount - 1
If List1.Selected(I) Then mp_vSelectedList(iMaxSelected) = List1.List(I)
iMaxSelected = iMaxSelected + 1

End If
Next I

End If

If iMaxSelected Then
ReDim Preserve mp_vSelectedList(iMaxSelected - 1)
Else
mp_vSelectedList = Empty
End If

End Sub

Private Sub test()

Dim vItem As Variant

Call GetSelectedListItems

If Not IsEmpty(mp_vSelectedList) ThenFor Each vItem In mp_vSelectedList
Debug.Print vItem
Next vItem
End If
End Sub

--------------------------------
Solution 3.
combining Answer #3 with my answer you could have the function return a string, and in the function, if the item was selected, add the item to a string along with a pipe, as opposed to using an array:

If List1.Selected(I) Then
sSelectedList = List1.List(I) & "|"

End If

Then loop through the items like this:
Private Sub test()
Dim vSelectedItems As Variant
Dim vItem As Variant

vSelectedItems = GetSelectedListItems()
if Len(vSelectedItems)>0 then
vSelectedItems=Split(vSelectedItems,"|")
Else
vSelectedItems=Empty
End IF

If IsArray(vSelectedItems) Then
For Each vItem In vSelectedItems
Debug.Print vItem
Next vItem
End If
End Sub

This discussion is closed to new replies.