473,499 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop through all list boxes to pull all selections- Passing variable control name

68 New Member
I have my field names stored in a 2D array:(arrControls)

I want to loop through my list box controls and pull the selections for each.
arrControls(0,1) = [lst_Status]
arrControls(1,1) = "StatusID"

I need to pass this to a function to read the selections:
fItemSel("StatusID", [lst_Status])

Which would return (if the user selected id 2 and 3):
"StatusID In(2,3)"

When I don't put "" around the field name: [lst_Status]
It tries to pull the selection instead of the field name.

When I put quotes around it: "[lst_Status]"
It can't pass the text string to the function reading the list field selection (which is expecting a control or variant)

I have tried absolutely everything I can think of and don't know how to get past this.
How can you set a control = contents of an array field?
Jun 5 '11 #1
12 3164
ADezii
8,834 Recognized Expert Expert
How about posting all the relevant Code, so that we can see exactly what is going on?
Jun 5 '11 #2
dk4300
68 New Member
Sorry, I was hoping that would make sense, so I only posted the concept that I'm stuck on. I'll put together a longer example. Thanks
Jun 6 '11 #3
dk4300
68 New Member
OK, here is more detail. I'm trying to loop through all my list boxes (using a variable name that would pull from an array) and get a statment (strListBoxSelection) of all the selections. I'm stuck on getting the field name stored in the array to pass to the fItemSel function.
Thank you!

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Dim arrControls(0 To 1, 0 To 4)
  4. Dim intColumn As Integer
  5. Dim strListBoxSelection As String
  6.  
  7. Function PullListBoxSelections()
  8.  
  9.     '0-ControlName
  10.     arrControls(0, 0) = [lst_Division]
  11.     arrControls(0, 1) = [lst_SubDivision]
  12.     arrControls(0, 2) = [lst_Department]
  13.     arrControls(0, 3) = [lst_Category]
  14.     arrControls(0, 4) = [lst_Item]
  15.  
  16.     '1-FieldName
  17.     arrControls(1, 0) = "DivisionID"
  18.     arrControls(1, 1) = "SubDivisionID"
  19.     arrControls(1, 2) = "DepartmentID"
  20.     arrControls(1, 3) = "Category"
  21.     arrControls(1, 4) = "Item"
  22.  
  23.     For intColumn = 0 To 4
  24.         strListBoxSelection = ""
  25.         PullListBoxSelections = ""
  26.  
  27.         strListBoxSelection = Application.Run("fItemSel", arrControls(1, intColumn), arrControls(0, intColumn))
  28.         'strListBoxSelection = Application.Run("fItemSel", "DivisionID", [lst_Division])
  29.  
  30.         If strListBoxSelection <> "" Then
  31.             If PullListBoxSelections = "" Then
  32.                 PullListBoxSelections = "HAVING " & PullListBoxSelections
  33.             Else
  34.                 PullListBoxSelections = PullListBoxSelections & " AND " & strListBoxSelection
  35.             End If
  36.         End If
  37.     Next
  38.  
  39. End Function
  40.  
  41. Public Function fItemSel(FieldName As String, ctrl As Control) As String
  42.     strSELECT = ""
  43.     For Each varItem In ctrl.ItemsSelected
  44.         If strSELECT = "" Then
  45.             strSELECT = ctrl.ItemData(varItem)
  46.  
  47.         Else
  48.             strSELECT = strSELECT & "," & ctrl.ItemData(varItem)
  49.  
  50.         End If
  51.     Next varItem
  52.  
  53.     If strSELECT = "" Then
  54.         fItemSelAnd = ""
  55.     Else
  56.         fItemSelAnd = FieldName & " In(" & strSELECT & ")"
  57.     End If
  58. End Function 
Jun 6 '11 #4
ADezii
8,834 Recognized Expert Expert
Here is a little food for thought assuming Status_ID is the Bound Column for lstStatus:
  1. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Function fItemSel(strField As String, lst As ListBox) As Variant
    2. Dim bytColCtr As Byte
    3. Dim varItem As Variant
    4. Dim strBuild As String
    5.  
    6. 'Is there at least 1 Item Selected for the given Listbox? If
    7. 'not, then return a Null Value, then Exit
    8. If lst.ItemsSelected.Count = 0 Then
    9.   fItemSel = Null
    10.     Exit Function
    11. End If
    12.  
    13. 'Assuming strField is the 'Bound' Column
    14. For Each varItm In lst.ItemsSelected
    15.   strBuild = strBuild & lst.ItemData(varItm) & ","
    16. Next varItm
    17.  
    18. fItemSel = """" & strField & " In(" & Left$(strBuild, Len(strBuild) - 1) & ")" & """"
    19. End Function
  2. Sample Call to Function:
    Expand|Select|Wrap|Line Numbers
    1. Dim varRetVal As Variant
    2.  
    3. varRetVal = fItemSel("Status_ID", Me![lst_Status])
    4.  
    5. Debug.Print varRetVal
  3. Results/Output (with 6 Selections):
    Expand|Select|Wrap|Line Numbers
    1. "Status_ID In(10,9,8,7,6,5)"
  4. Simply feed to the Function each Bound Field Name as well as the List Box.
Jun 6 '11 #5
dk4300
68 New Member
That's my back up plan, but I'm still interested in trying to pass that field name as a variable. In my super simple example ([lst_Status]) I only gave one list box, in my next example, I gave 5 list boxes, in the real database, I actually am shooting for 7 list boxes (and 4 combo boxes). Some call for IDs, some dates, some text fields (I only included the function code for the IDs but I have the others).

I'm wondering about storing in my array the control name, field name, select statement, group by statement, and order by statment.
When the user selects from a list box, it would pull the "where" and "having" statments from the previous list boxes, and update the rowsource on the future list boxes. So after the first selection, I would loop through the next 7-10 controls and update the rowsource. On the 5th list box (for example), I would pull the "where" and "having" statements from the first 5 and update the rowsource on the next 5. It's probably not efficient to pull the 1st list boxes "where" statement, after the 1st-5th etc additional list box selections, but I'm not sure where to store it otherwise.

So I'm stuck on passing my form field name stored in a array to my listbox reading function. I was wondering if there is something super easy that I'm missing. Like a way to change the data type or something?
Jun 6 '11 #6
dk4300
68 New Member
Found it:
Expand|Select|Wrap|Line Numbers
  1. Set Ctrl = Me.Controls(arrControls(0, intColumn))
Me.Controls was the part I was missing. Brutal.

Expand|Select|Wrap|Line Numbers
  1. Set Ctrl = Me.Controls(arrControls(0, intColumn))
  2. strListBoxSelection = Application.Run("fItemSel", arrControls(1, intColumn), Ctrl) 
Jun 6 '11 #7
ADezii
8,834 Recognized Expert Expert
How about storing the Control/Field Combinations in Pairings within a Comma Delimited List, and eliminating the 2-Dimensional Array?
Expand|Select|Wrap|Line Numbers
  1. Dim strDelim As String
  2. Dim varTest As Variant
  3. Dim intCtr As Integer
  4.  
  5. strDelim = "[lst_Division],DivisionID,[lst_SubDivision],SubDivisionID,[lst_Department]," & _
  6.            "DepartmentID,[lst_Category],Category,[lst_Item],Item"
  7.  
  8. varTest = Split(strDelim, ",")
  9.  
  10. For intCtr = LBound(varTest) To UBound(varTest) Step 2
  11.   Debug.Print varTest(intCtr), varTest(intCtr + 1)
  12. Next
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. [lst_Division]              DivisionID
  2. [lst_SubDivision]           SubDivisionID
  3. [lst_Department]            DepartmentID
  4. [lst_Category]              Category
  5. [lst_Item]                  Item
Jun 6 '11 #8
dk4300
68 New Member
I did something very similar using the split function, but mine was 2D still because I have 70 strings. I combined the split function with a loop function to fill each spot in the array.
I used an array because I wanted to store not only lst_Division, and DivisionID, but also the SELECT, GROUP, and ORDER statements (and a couple other things) for each. About 7 columns total for 10 controls.
The other thing is that I wanted to loop through the controls using an id. (Which your example above would do in a 1D array, but mine would have 70 columns which is why I wouldn't do that, though it's a good idea and I'll use it in other places). Instead of having to list 10 functions after each "after update" of 10 controls, I ultimately wanted one procedure where I passed the column number of the control to read (start to end) and the column numbers to write (start to end). I did this:
Expand|Select|Wrap|Line Numbers
  1. Call UpdateControls(intReadStart, intReadEnd, intWriteStart, intWriteEnd)
So on the 4th (for example) control it would be:
Expand|Select|Wrap|Line Numbers
  1. Call UpdateControls(0,4,5,10)
This reads the selected criteria for controls 0-4 in my array (by passing the variable field name to my list box reading function), pulls the SQL statements for fields 5-10, inserts the WHERE statement from 0-4 and updates the rowsource. It's sweet.
My only complaint now (once I had the Me.Controls breakthrough) is that I like it so much I'm likely to use this type of form/functions on other projects so I would still like to perfect it. I have the criteria re-reading all the control selections with every selection from a new control which seems inefficient since it ultimately it should just save one ongoing "WHERE" string and add to it or remove from it for every new choice "after update". My original intention was "after update" to update the array with the selected criteria and store it there, but I'm embarressed to say I don't know how long those variables would be saved?
Jun 8 '11 #9
ADezii
8,834 Recognized Expert Expert
I don't know how long those variables would be saved?
That depends entirely on the Scope, namely the Context within which they are Declared:
  1. Since it is Declared in a Local Procedure, it would have a very narrow Scope, namely varSomeVariable would exist only within the AfterUpdate() Event of SomeControl:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub SomeControl_AfterUpdate()
    2.   Dim varSomeVariable as Variant
    3. End Sub
  2. Since it is Declared Privately within the General Declarstion of a Form's Module, varSomeVariable would exist for the lifetime of the Form:
    Expand|Select|Wrap|Line Numbers
    1. 'General Declarations Section of a Form
    2. Private varSomeVariable as Variant
  3. Since it is Declared Publically within the General Declarstion of a Standard Code Module, varSomeVariable would exist for the lifetime of Application:
    Expand|Select|Wrap|Line Numbers
    1. 'General Declarations Section of a Standard Code Module
    2. Public varSomeVariable as Variant
  4. Hope this sheds some light on the Subject.
Jun 8 '11 #10
dk4300
68 New Member
Thank you so much for the response, I think I sort of knew your #1 and #2, so I declared it in the general form rather than the procedure, but I didn't know #3 and in general wasn't confident in #2 still being there while other procedures were run (which as you explain, is the whole point). Thanks a ton, I will try adding a column to the array to store the controls selections (after update), then cycle through them to string together the WHERE statment and not have to keep reading the selections for the same conrols over and over.

In your expert opinion, do you think this sounds like a good idea to make the form work this way? I not only want it to work (which it does right now), but going forward, I would like to keep increasing the efficiency and professionalism of my code.
Jun 8 '11 #11
ADezii
8,834 Recognized Expert Expert
I not only want it to work (which it does right now), but going forward, I would like to keep increasing the efficiency and professionalism of my code.
Sounds like a desirable and professional approach to me, one which I'm sure no one here will argue with. Good Luck.
Jun 9 '11 #12
dk4300
68 New Member
Thanks, and thanks again for the explanation/assurances sbout declaring my variables. That knocked out the last remaining hesitations I had. I just upated my form to read the array "on load", then it's stored going forward (before I was reading it too many times which didn't seem right), I also updated the "after update" for each control to write to a field in my array for that associated control (then loop through them to piece together the "where" statment, instead of reading each controls selection repeatedly on the click of every new control). Now I'm just looping though the 10 fields to pull the "where" statment, instead of looping through every line of every list box. Much better. Much faster! (I also added a sort on every field of every listbox). Thanks a ton! On to the next...
Jun 9 '11 #13

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

Similar topics

1
1470
by: sleon | last post by:
I have two drop down list boxes, category, and subcategory. The subcategory list does not get populated until a selection has been made from the "category" list. When a selection is made from...
1
2148
by: Hohn Upshew | last post by:
I want to build a function having the name of the list box in brackets like Check(ListBoxInvoices).May i have some help as to how to do it ? In my form i have several list boxes.I have command...
8
2542
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and...
3
3835
by: B-Dog | last post by:
I'm capturing the checked radio button to XML file using the name of the radio button. I want to read my xml file to find which button was checked on close and the check the appropriate button...
6
1334
by: Col | last post by:
Hi - I've never worked with list boxes before. Here's what I'd like to do - have a list box (or some other control) that allows multiple selection and stores all the values in one field (can be...
1
4370
by: Christina | last post by:
Hi, I've been looking at some code for dependent list boxes to adapt to a State and City list. There will only be 2 states for the first list box, and 3 cities in the second list box. When the...
4
64525
Rabbit
by: Rabbit | last post by:
Cascading Combo/List Boxes This tutorial is to guide you in the creation of Cascading combo/list boxes. That is when you have multiple combo/list boxes where the selection of an option in one...
3
1847
by: ZaphodBBB | last post by:
Hi (Windows XP Pro, MS Access 2003) I have a form that has quite a number of list boxes on it. The list boxes only have 3 or 4 selections possible...Pass, Fail, Average, Not...
5
7125
by: paragpdoke | last post by:
Hello Everyone. I'm new to XSL and couldn't get something to work. Sample XML excerpt: <root> <ObjectTemplate> <NodeType1></NodeType1> <NodeType2></NodeType2> </ObjectTemplate> <Object>
0
7134
marktang
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,...
0
7012
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
7225
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...
1
6901
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...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4605
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...
0
3105
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...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
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...

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.