473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

multiselect listbox to array

58
Hey all, newbie to vb here. I've created a listbox (called lst_county) that gets populated from a Select * From Table in Oracle on load. I've set the MultiSelect to 2 -Extended. I've got some code that sends out the select rows from lst_country to a msgbox (all working fine). What i need to do now is to actually put those selected rows into an array that I can later parse into another Select * From Table Where < selected lst_country (i,0)> = some value or < selected lst_country (i,0)> = some value , etc until all selected rows have been parsed.
So can anyone at least help me take the selected values and put them into an array.
Here is my msgbox code.

Expand|Select|Wrap|Line Numbers
  1. Public Function lstbox_array()
  2.  
  3.     Dim i As Long
  4.     Dim Msg As String
  5.  
  6.     With frm1.lst_country
  7.         For i = 0 To frm1.lst_country.ListCount - 1
  8.             If .Selected(i) Then
  9.                 Msg = Msg & frm1.lst_country.List(i) & vbCrLf
  10.             End If
  11.         Next i
  12.     End With
  13.  
  14.     If Len(Msg) = 0 Then
  15.         Msg = “No items selected”
  16.     End If
  17.  
  18.     MsgBox Msg
  19.  
  20. End Function
and here is my first attempt

Expand|Select|Wrap|Line Numbers
  1. Public Function lstbox_array()
  2.  
  3. Dim i As Long
  4. Dim Msg As String
  5. 'Dim listArray()
  6. 'Dim j As Long
  7. 'Dim vArray
  8. 'ReDim listArray(1 To frm1.lst_country.ListCount, 1 To 1)
  9.  
  10.     With frm1.lst_country
  11.         For i = 0 To frm1.lst_country.ListCount - 1
  12.             If frm1.lst_country.Selected(i) Then
  13.                 Msg = Msg & Left(frm1.lst_country.List(i), 3) & vbCrLf
  14.                 'j = j + 1: listArray(j, 1) = frm1.lst_country.List(i, 1)
  15.                 'vArray(i) = frm1.lst_country.List(i, 1)
  16.                 'listArray(i, 0) = Left(frm1.lst_country.List(i), 3)
  17.             End If
  18.         Next i
  19.     End With
  20.  
  21.     If Len(Msg) = 0 Then
  22.         Msg = "No items selected"
  23.     End If
  24. 'Debug.Print listArray(i)
  25.     MsgBox Msg
  26.  
  27. End Function
you'll notice alot of commented out code and that just cause I keep trying different things and see them fail..

i figure once i can get the selected items into a new array, it shouldn't be too much of a problem to parse through those and add them to the SQL WHERE statements.
Thank immensely ahead of time for anyone's help and or direction.
Cheers,
Eric
Jul 29 '08 #1
5 5621
janders468
112 Expert 100+
This may not be the best way but the way I would approach this would be:
1. For basic looping through the listbox
Expand|Select|Wrap|Line Numbers
  1. dim var as variant
  2. for each var in listbox.ItemsSelected
  3.      msgbox listbox.ItemData(var)
  4. next var
  5.  
It wouldn't be too much of a stretch to convert that to an array if you wanted to:
Expand|Select|Wrap|Line Numbers
  1. dim var as variant
  2. dim n as integer
  3. redim varArr(listbox.listcount-1)
  4. for each var in listbox.ItemsSelected
  5.    varArr(n) = listbox.ItemData(var)
  6.     n = n + 1
  7. next var
  8.  
This is one way to do it any how
Jul 29 '08 #2
erbrose
58
I guess I need to learn more about arrays before continuing here.
When I try your first bit of code (after changing .ItemsSelected to just .Selected As I was getting a Compile error: Method or data member not found)

Expand|Select|Wrap|Line Numbers
  1. Dim var As Variant
  2.     For Each var In frm1.lst_country.Selected
  3.         MsgBox frm1.lst_country.ItemData(var)
  4.     Next var
  5.  
i get a Compile error: Argument not optional and it highlights line 2.

same with the second bit of code you posted if i changed it to this
Expand|Select|Wrap|Line Numbers
  1. Dim var As Variant
  2. Dim n As Integer
  3. ReDim varArr(frm1.lst_country.ListCount - 1)
  4. For Each var In frm1.lst_country.Selected
  5.    varArr(n) = frm1.lst_country.ItemData(var)
  6.     n = n + 1
  7. Next var
  8.  
again it highlights line 4 and gives me the same error, so there is something with the Selected argument that is not working and confusing me.
Jul 30 '08 #3
janders468
112 Expert 100+
I should have maybe asked some preliminary questions, sorry about that. What version of vb are you using?
Jul 30 '08 #4
erbrose
58
I managed to get it sorted out... after reading more about arrays.
here is what I did

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim MyArray() As String
  3. Dim n As Integer
  4.  
  5. ReDim MyArray(frm1.lst_country.ListCount)
  6.  
  7.     If frm1.lst_country.ListIndex = -1 Then
  8.         MsgBox "You didn't select any Countries"
  9.     End If
  10.     For i = 0 To frm1.lst_country.ListCount - 1
  11.         If frm1.lst_country.Selected(i) = True Then
  12.             MyArray(n) = Left(frm1.lst_country.List(i), 3)
  13.             n = n + 1
  14.         End If
  15.     Next i
  16.  
  17. ReDim Preserve MyArray(n - 1)
  18.  
I really appreciated your replies and code snippets!
Thanks
Eric
ps I will be sure to note next time I am working in VB6
Jul 30 '08 #5
janders468
112 Expert 100+
Glad you got it figured out, and glad to help. For whatever it's worth the code I first supplied was for Access vba, which uses a different listbox object than does forms 2.0 (the library containing the listbox object in vb6 and vba for the other office products). I believe that is why the method was missing.
Jul 30 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sally | last post by:
I have a simple multiselect listbox with a rowsorce of MemberID, MemberName, SendLetter. SendLetter is a Yes/No field. What is the code to set SendLetter to Yes when the user selects MemberName? I...
2
by: Cassie Pennington | last post by:
I am trying to write various items from a multiselect list box to an SQL statement to update a report, without success. SQL only appears to accept hard-coded data or control values from a form, not...
2
by: Alan Lane | last post by:
Hello world: I'm using Access 2003. I have 2 listboxes. One is a single column. The other has two columns. I can use Dev Ashish's code (thanks Dev!) from the Access MVP Website to accumulate...
6
by: ¿ Mahesh Kumar | last post by:
Hi groups, Control name : ListboxID (lstCertification), selection mode=mutliselect. On Pageload i'm assinging string lstSplit="1/3/6/8" of the previously selected listindex id's. Now on the...
2
by: ttime | last post by:
I've got a form that uses a multiselect listbox. When a user is selected from a combo box, values are populated into this listbox associated with that user. The problem is, if one person has say...
3
by: kaosyeti via AccessMonster.com | last post by:
hey... i have an unbound multiselect listbox on a form that i want to use to populate text boxes on that form. so if a user selects the 3rd item in a list of 20, how can i have that item show up...
5
by: kimiraikkonen | last post by:
Hello, I have openfiledialog control named "openfileplaylist" and multi- selectpropert is TRUE. But although i select more than one files using "shift+arrows", i only get one file listed in my...
5
by: martin DH | last post by:
Hello, The details are below, but I have a simple form (Form1) with two objects and a "search" command button. When the two objects are cascading combo boxes (the form creates the parameters for a...
1
by: asharma0001 | last post by:
Hi all, I was wondering whether somebody might be able to help me with a question I have on a MS Access Database I'm building. I have created a search form with a few multiselect listboxes....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.