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. - Public Function lstbox_array()
-
-
Dim i As Long
-
Dim Msg As String
-
-
With frm1.lst_country
-
For i = 0 To frm1.lst_country.ListCount - 1
-
If .Selected(i) Then
-
Msg = Msg & frm1.lst_country.List(i) & vbCrLf
-
End If
-
Next i
-
End With
-
-
If Len(Msg) = 0 Then
-
Msg = “No items selected”
-
End If
-
-
MsgBox Msg
-
-
End Function
and here is my first attempt - Public Function lstbox_array()
-
-
Dim i As Long
-
Dim Msg As String
-
'Dim listArray()
-
'Dim j As Long
-
'Dim vArray
-
'ReDim listArray(1 To frm1.lst_country.ListCount, 1 To 1)
-
-
With frm1.lst_country
-
For i = 0 To frm1.lst_country.ListCount - 1
-
If frm1.lst_country.Selected(i) Then
-
Msg = Msg & Left(frm1.lst_country.List(i), 3) & vbCrLf
-
'j = j + 1: listArray(j, 1) = frm1.lst_country.List(i, 1)
-
'vArray(i) = frm1.lst_country.List(i, 1)
-
'listArray(i, 0) = Left(frm1.lst_country.List(i), 3)
-
End If
-
Next i
-
End With
-
-
If Len(Msg) = 0 Then
-
Msg = "No items selected"
-
End If
-
'Debug.Print listArray(i)
-
MsgBox Msg
-
-
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
5 5685
This may not be the best way but the way I would approach this would be:
1. For basic looping through the listbox -
dim var as variant
-
for each var in listbox.ItemsSelected
-
msgbox listbox.ItemData(var)
-
next var
-
It wouldn't be too much of a stretch to convert that to an array if you wanted to: -
dim var as variant
-
dim n as integer
-
redim varArr(listbox.listcount-1)
-
for each var in listbox.ItemsSelected
-
varArr(n) = listbox.ItemData(var)
-
n = n + 1
-
next var
-
This is one way to do it any how
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) -
Dim var As Variant
-
For Each var In frm1.lst_country.Selected
-
MsgBox frm1.lst_country.ItemData(var)
-
Next var
-
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 -
Dim var As Variant
-
Dim n As Integer
-
ReDim varArr(frm1.lst_country.ListCount - 1)
-
For Each var In frm1.lst_country.Selected
-
varArr(n) = frm1.lst_country.ItemData(var)
-
n = n + 1
-
Next var
-
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.
I should have maybe asked some preliminary questions, sorry about that. What version of vb are you using?
I managed to get it sorted out... after reading more about arrays.
here is what I did -
Dim i As Integer
-
Dim MyArray() As String
-
Dim n As Integer
-
-
ReDim MyArray(frm1.lst_country.ListCount)
-
-
If frm1.lst_country.ListIndex = -1 Then
-
MsgBox "You didn't select any Countries"
-
End If
-
For i = 0 To frm1.lst_country.ListCount - 1
-
If frm1.lst_country.Selected(i) = True Then
-
MyArray(n) = Left(frm1.lst_country.List(i), 3)
-
n = n + 1
-
End If
-
Next i
-
-
ReDim Preserve MyArray(n - 1)
-
I really appreciated your replies and code snippets!
Thanks
Eric
ps I will be sure to note next time I am working in VB6
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 want to do this as the selections are
being made not after-the-fact after all selections are made.
Thanks!
Sally
|
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 variable
data. Any clues as to how I can write several items to an SQL
statement from a multiselect listbox to update a report?
Thanks in anticipation
Cassie
|
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 the values from the bound column of a
MultiSelect listbox, so that I can include them in a SQL query that will
then be the recordsource for a report. This gives me 2 of the 3 values I
need to put into the SQL query. However, I can't get the non...
|
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 page load for updation, i have to reload the selected items again with the same string "1/3/6/8" to be selected in my multiselect list box.
Its asking me to convert object to int... for list selection. but how to achieve this..?...
|
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 10 entries in the listbox, and selects the last 3-4, and the next user only has 2 entries, those last 3-4 rows are still highlighted in the listbox. This introduces null values into my code which is definitely not good.
I've tried using...
| |
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 in a text box? then, how can i
have a second selected item show up in a different text box, without
affecting the 1st one? (lboxOptions, txtboxOptions0, txtboxOptions1, etc..)
--
Greg
Message posted via AccessMonster.com
|
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 listbox.
What's wrong?
Code:
If openfileplaylist.ShowDialog() = Windows.Forms.DialogResult.OK Then
|
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 query - Query1), the query returns my results proper. But when the two objects are cascading combo-then-multiselect listbox (the perferred format in this case), the query always returns zero records.
Tables:
COMPILE (contains the records to be...
|
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. What I'd ultimately like is for the selection(s) in one listbox to filter the second listbox, but am struggling to find a way to do this. I have looked at some of the other solutions on this forum but not been able to find one that works on my database....
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |