473,326 Members | 2,048 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,326 software developers and data experts.

Running function for each item in listbox

Hello,

I'm sure this is such a simple question I didn't even want to bother posting but for the life of me I just can't get it to work : (

I have a button that toggles "Select All" and "Select None". Whenever the user clicks "None" it runs a function that clears a series of data and likewise when they click "All" it inputs a long series of data. The function works as long as the user actually CLICKS each item, however with a listbox of 100+ entries the users will not be clicking each unit individually lol

The "Select All" code that I'm using is very standard:
Expand|Select|Wrap|Line Numbers
  1. For intcounter = 0 To SelectionLst.ListCount
  2.   InputGoodies 
  3.   SelectionLst.Selected(intcounter) = True
  4. Next intcounter
The function "InputGoodies" needs to run the way it would if I ran it OnClick for the listbox. My on-click is just as simple:

Expand|Select|Wrap|Line Numbers
  1. If SelectionLst.Selected(SelectionLst.ListIndex) = "-1" Then 
  2.   InputGoodies
  3. Else
  4. ...
Is there any way to make some kind of loop that will do the above function for each item in the ListCount? I don't understand why I can't get it to work, I feel like I've tried everything. Help is completely appreciated!! : D

-Cathy
Oct 19 '07 #1
15 17574
ADezii
8,834 Expert 8TB
Hello,

I'm sure this is such a simple question I didn't even want to bother posting but for the life of me I just can't get it to work : (

I have a button that toggles "Select All" and "Select None". Whenever the user clicks "None" it runs a function that clears a series of data and likewise when they click "All" it inputs a long series of data. The function works as long as the user actually CLICKS each item, however with a listbox of 100+ entries the users will not be clicking each unit individually lol

The "Select All" code that I'm using is very standard:

For intcounter = 0 To SelectionLst.ListCount
InputGoodies
SelectionLst.Selected(intcounter) = True
Next intcounter

The function "InputGoodies" needs to run the way it would if I ran it OnClick for the listbox. My on-click is just as simple:

If SelectionLst.Selected(SelectionLst.ListIndex) = "-1" Then
InputGoodies
Else
...

Is there any way to make some kind of loop that will do the above function for each item in the ListCount? I don't understand why I can't get it to work, I feel like I've tried everything. Help is completely appreciated!! : D

-Cathy
Cathy, I'm a little hazy on exactly what you are requesting. Assuming your List Box name is List1, the following code will pass each value in the List Box (List1) to the SomeValue() Function, where it will be adjusted or operated on in some manner. ALL entries in the List Box will be passed to the Function regardless of whether they are selected or not. Is this what you are looking for?
Expand|Select|Wrap|Line Numbers
  1. Dim intItemsInList As Integer, intCounter As Integer
  2. Dim lst As ListBox
  3.  
  4. Set lst = Me![List1]
  5.  
  6. intItemsInList = lst.ListCount
  7.  
  8. For intCounter = 0 To intItemsInList - 1
  9.   SomeFunction (lst.ItemData(intCounter))
  10. Next
Oct 20 '07 #2
NeoPa
32,556 Expert Mod 16PB
...
The "Select All" code that I'm using is very standard:
Expand|Select|Wrap|Line Numbers
  1. For intcounter = 0 To SelectionLst.ListCount
  2.   InputGoodies 
  3.   SelectionLst.Selected(intcounter) = True
  4. Next intcounter
...
Your code looks good to me Cathy.
The only thing that occurrs to me is that maybe the function InputGoodies() only processes the item if it's already selected. In that case move line 2 after line 3 for better results.
Oct 21 '07 #3
Well I tried both of the suggestions but I still can't get this to work : (

The "InputGoodies" function that I'm referencing looks like this:

Expand|Select|Wrap|Line Numbers
  1. Public Sub InputGoodies()
  2.  
  3. If Price1.Value = "0" Then
  4. Price1.Value = SelectionLst.Column(5)
  5. SqFt1.Value = SelectionLst.Column(4)
  6. ...
  7.  
..and it goes on for another hundred lines asking questions, running loops, and such. The situation is that when I use a Select All function it DOES run "InputGoodies" X times but it only uses the FIRST SELECTED listbox item as the value (the item that the user may or may not click on). So if they load up the listbox and just click "Select All", it may run "InputGoodies" 30-some times but it wont reference any values from the listbox. IF the user happens to click one of the list items and then clicks Select All, it will run InputGoodies 30-some times and will use that ONE selected value for everything.

So the problem is that my function requires the item to be selected for it to work. If there was some way to simulate a listbox item being selected for each instance in the loop then it should work so it should go something like:

1. Count how many items are in the listbox
2. Select the first item
3. Run my function
4. Repeat X times

I'm so sure this is really easy and I can't believe I haven't figured it out by now but the past weekend I was moving and I didn't get any work done.
Oct 22 '07 #4
NeoPa
32,556 Expert Mod 16PB
In that case it is indeed important to call the function after the selection is set.
Expand|Select|Wrap|Line Numbers
  1. For intCounter = 0 To SelectionLst.ListCount
  2.   SelectionLst.Selected(intCounter) = True 'line needs to be changed
  3.   Call InputGoodies()
  4. Next intcounter
Setting the selection must be done differently as you are not checking the selected items in your InputGoodies() code but only the "active" item.
In the same way that Excel has a Selected list but also has an Active item, so too does an Access ListBox (You must have the MultiSelect property of the ListBox set to Simple or Extended).
Let me see if I can dig up the way to set the Active item rather than simply adding it to the list (as this is what your code uses - it ignores selected items).
Oct 22 '07 #5
NeoPa
32,556 Expert Mod 16PB
Why don't you try :
Expand|Select|Wrap|Line Numbers
  1. For intCounter = 0 To SelectionLst.ListCount
  2.   Call SelectionLst.SetFocus()
  3.   SelectionLst.ListIndex = intCounter
  4.   Call InputGoodies()
  5. Next intcounter
Oct 22 '07 #6
Why don't you try :
Expand|Select|Wrap|Line Numbers
  1. For intCounter = 0 To SelectionLst.ListCount
  2.   Call SelectionLst.SetFocus()
  3.   SelectionLst.ListIndex = intCounter
  4.   Call InputGoodies()
  5. Next intcounter
I think this is the right direction (using the setfocus maybe) but when I run this it tells me "Runtime Error: 7777 You've used the ListIndex Property Incorrectly".

The debug process highlights "SelectionLst.ListIndex = intCounter" as the problem. The ListIndex count was at 19 and the intCounter was at 20 if that will help. I'm really curious now about this. NeoPa you've been a big help so far - you have any suggestions?? : )

It could very well be something I'm doing wrong but everything I'm explaining seems pretty simple on the plane of access development.

Hmmmmm...
Oct 23 '07 #7
NeoPa
32,556 Expert Mod 16PB
Cathy,

I just dug this up from the help file really. I don't have a lot of experience with it.
If it's easy, could you attach a copy of your database to a new post and let me know which version of Access you're using. I can't imagine that the last would make much of a difference, but it won't hurt to know.

If this fails of course, then it might be necessary to recode your other function (InputGoodies()).
If you can't (or don't want to) attach your database then we'll just have to see what we can find out by rebuilding something similar.
Oct 23 '07 #8
Cathy,

I just dug this up from the help file really. I don't have a lot of experience with it.
If it's easy, could you attach a copy of your database to a new post and let me know which version of Access you're using. I can't imagine that the last would make much of a difference, but it won't hurt to know.

If this fails of course, then it might be necessary to recode your other function (InputGoodies()).
If you can't (or don't want to) attach your database then we'll just have to see what we can find out by rebuilding something similar.
Hi NeoPa,

Well I couldn't upload my database because even the front-end is almost 20Mb right now but I was able to at least re-create my problem (I added some comments so you get an idea). It's a very simple re-enactment of what I'm trying to do. I'm hoping that by doing this, it will make this problem resolve itself via becoming self-evident lol

Thanks for taking the time to help me out : )

http://www.rjpinc.net/uploads/sample.zip
Oct 23 '07 #9
NeoPa
32,556 Expert Mod 16PB
I'll have a look at it but I'm out all tomorrow night.
Give this a bump on Thursday if you've not heard from me.
Oct 23 '07 #10
B-B-Bump!!

B-B-Bump!!
Oct 25 '07 #11
NeoPa
32,556 Expert Mod 16PB
I'm sorry Cathy, I've downloaded the db but I haven't had a chance yet to look at it. I'll check it tomorrow or the weekend. Unfortunately I can't do that sort of stuff at work - must be from home (evenings & weekends).
Oct 26 '07 #12
NeoPa
32,556 Expert Mod 16PB
I'm at work at the moment so have no access to the uploaded database, but I've given it some thought and it seems to me that what is called for in this case is surely that the InputGoodies() function is recoded to work with all selected items rather than simply to work with the currently active item.
Not only would this get around your problem, it would also (to my mind) be the most appropriate way to handle it.
When I look at things later I'll look at them with this idea in mind.
Oct 26 '07 #13
NeoPa
32,556 Expert Mod 16PB
Cathy,

Still haven't got to this I'm afraid.
I've had a lot of "homework" questions recently and allowed this one to slip down the list (again) so I'm sorry for that and will remember to look at it tonight (barring only extreme and unforeseen circumstance).

Please feel free to bump any time as this one is long overdue for a serious look at.
Oct 30 '07 #14
Cathy,

Still haven't got to this I'm afraid.
I've had a lot of "homework" questions recently and allowed this one to slip down the list (again) so I'm sorry for that and will remember to look at it tonight (barring only extreme and unforeseen circumstance).

Please feel free to bump any time as this one is long overdue for a serious look at.
I appreciate your help NeoPa : ) I personally have been doing a lot of other access related coding and I've just neglected this item for now. Anyone else who wants to look at this as well is more than welcome to share their input : D
Oct 30 '07 #15
NeoPa
32,556 Expert Mod 16PB
Right Cathy, I couldn't work out why you were doing quite what you were doing but I coded up the InputGoodies() function to process through the list itself in the closest guess I could manage of what your logic was intended to be :S
As I said earlier, you need something to process through the ItemsSelected collection. There's little point in the Command2 code doing it if there's no parameter to the InputGoodies() function to pass it through in.
Anyway, see if this makes sense.
Expand|Select|Wrap|Line Numbers
  1. Public Sub InputGoodies()
  2.     Dim intX As Integer
  3.  
  4.     With Me.List0
  5.         intX = 0
  6.         If .ItemsSelected.Count > intX And X = "" Then
  7.             X = .Column(1, .ItemsSelected(intX))
  8.             intX = intX + 1
  9.         End If
  10.         If .ItemsSelected.Count > intX And Y = "" Then
  11.             Y = .Column(1, .ItemsSelected(intX))
  12.             intX = intX + 1
  13.         End If
  14.         If .ItemsSelected.Count > intX And Z = "" Then
  15.             Z = .Column(1, .ItemsSelected(intX))
  16.             intX = intX + 1
  17.         End If
  18.     End With
  19. End Sub
Oct 30 '07 #16

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

Similar topics

3
by: Mike | last post by:
I have a mulitpage form that I do not record the information into a dbase until the end. I need to pass the field values form on form to the next. I have the code for automatically generating...
2
by: ajay.sonawane | last post by:
Want to ask one thing about RSS ( Really Simple Syndication ) I went through RSS specifications. Read all fields of item and channel. But didn't find any field that could help me to store some of...
5
by: hiroshi ochi | last post by:
Hello, Using MSIE 6.0 and above, with javascript is it possible to display an individual tooltip for each item in a listbox? I need this functionality to show the listitems that are longer...
3
by: vikram | last post by:
How can i add a user define attribute to each checkbox item in a checkbox list control. I want to add a attribute "Tag". Is it possible
14
by: Drew | last post by:
I need to iterate through a submitted form, inserting data on each pass. In the past, I have always used form elements that were named with numbers at the end, like this, name1 relationship1...
0
by: Krishna | last post by:
Hi All, Scenerio: There are many contracts. Each contract has some items for which the contract has been established. So i have to display the Rebate on each item of each contracts. There is...
1
by: Priya | last post by:
I have to display a checboxlist with someitems when each item in a listbox is selected. I have a listbox with a list of categories and when i click each category from the listbox, the checkbox list...
2
by: liz0001 | last post by:
I am trying to run an SQL query for each item in an Array using ASP. I have coded it as such. IDsArray() is an array of integers. ArrayLength=15 sqlGetiIDs = "SELECT * FROM Table1 WHERE...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.