473,425 Members | 1,974 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,425 software developers and data experts.

Looping through groups of controls on a form

Seth Schrock
2,965 Expert 2GB
I'm using Access 2010 and I'm trying to group controls so that I can edit each control's properties in a group as one. I am currently using the control's tag property to specify which group it is apart of and then I loop through call the controls and add it to a collection (through a class module) based on its tag property. However, this means that if I want to add a group to the form, I have to edit my code to reference another instance of my class module and then test for the new group. Is there a way to make it so that my code could be more universal instead of having to duplicate my code for each form and then edit the code to fit the number of groups on that form? I'm thinking it would need a loop, but I don't know what to loop through. Here is what I'm using now.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. On Error GoTo Error_Handler
  3.  
  4. Dim ctl As Control
  5. Dim Area8 As clsControl
  6. Dim Area9 As clsControl
  7. Dim Area10 As clsControl
  8. Dim db As DAO.Database
  9. Dim rst As DAO.Recordset
  10. Dim strCriteria As String
  11.  
  12.  
  13. Set Area8 = New clsControl
  14. Set Area9 = New clsControl
  15. Set Area10 = New clsControl
  16.  
  17. Set db = CurrentDb
  18. Set rst = db.OpenRecordset("qryUserPermissions")
  19.  
  20. For Each ctl In Me.Controls
  21.     Select Case ctl.ControlType
  22.         Case acComboBox, acTextBox, acCommandButton
  23.             If ctl.Properties("Tag") = "Open" Or ctl.Properties("Tag") = "" Then
  24.                 ctl.Properties("Locked") = False
  25.                 ctl.Properties("Visible") = True
  26.             Else
  27.                 Select Case ctl.Properties("Tag")
  28.                     Case 8
  29.                         Area8.AddControl ctl, ctl.Name
  30.  
  31.                     Case 9
  32.                         Area9.AddControl ctl, ctl.Name
  33.  
  34.                     Case 10
  35.                         Area10.AddControl ctl, ctl.Name
  36.  
  37.                 End Select
  38.             End If
  39.     End Select
  40. Next ctl
  41.  
  42. With rst
  43.     strCriteria = "AreaID_fk = 8"
  44.     .FindFirst strCriteria
  45.     Area8.Permissions !MaxOfPermissionLevel
  46.  
  47.     strCriteria = "AreaID_fk = 9"
  48.     .FindFirst strCriteria
  49.     Area9.Permissions !MaxOfPermissionLevel
  50.  
  51.     strCriteria = "AreaID_fk = 10"
  52.     .FindFirst strCriteria
  53.     Area10.Permissions !MaxOfPermissionLevel
  54. End With
  55.  
  56. Exit_Procedure:
  57.     Set Area8 = Nothing
  58.     Set Area9 = Nothing
  59.     Set Area10 = Nothing
  60.     Set db = Nothing
  61.     Set rst = Nothing
  62.  
  63.     Exit Sub
  64.  
  65. Error_Handler:
  66.     Call ErrorMessage(Err.Number, Err.Description, "Form_frmAdminForms: Form_Load")
  67.     Resume Exit_Procedure
  68.     Resume
  69.  
  70. End Sub
Jun 28 '13 #1

✓ answered by Rabbit

Use a collection of clsControl.
Expand|Select|Wrap|Line Numbers
  1. ' Setup the collection
  2. Dim colClsControl As Collection
  3. Set colClsControl = New Collection
  4.  
  5. ' Loop through all controls to get unique tags and run this
  6. ' An alternative is to use the contains method to check
  7. ' the existence of the key before attempting to access it.
  8. colClsControl.Add New clsControl, ctl.Properties("Tag")
  9.  
  10. ' Access the collection to add controls like this
  11. colClsControl(ctl.Properties("Tag")).AddControl ctl, ctl.Name

18 10593
Rabbit
12,516 Expert Mod 8TB
Use a collection of clsControl.
Expand|Select|Wrap|Line Numbers
  1. ' Setup the collection
  2. Dim colClsControl As Collection
  3. Set colClsControl = New Collection
  4.  
  5. ' Loop through all controls to get unique tags and run this
  6. ' An alternative is to use the contains method to check
  7. ' the existence of the key before attempting to access it.
  8. colClsControl.Add New clsControl, ctl.Properties("Tag")
  9.  
  10. ' Access the collection to add controls like this
  11. colClsControl(ctl.Properties("Tag")).AddControl ctl, ctl.Name
Jun 28 '13 #2
Seth Schrock
2,965 Expert 2GB
How do I get the unique tag values? Is there a SELECT DISTINCT function available for all of the tag values?
Jun 28 '13 #3
Rabbit
12,516 Expert Mod 8TB
No, you just loop through and check whether or not you've already added that one.
Jun 28 '13 #4
Seth Schrock
2,965 Expert 2GB
This is the first time I have worked with custom collections, so I would like to make sure that I'm understanding what I need to do.

Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Dim colClsControl as Collection
  3. Set colClscontrol = New Collection
  4.  
  5. If Not colClsControl.Exists(ctl.Properties("Tag")) Then
  6.     colClsControl.Add(New clsControl, ctl.Properties("Tag"))
  7. End If
This would then become a collection of collections which I could loop through to control the properties of the controls within the sub-collections. Is that correct?
Jun 28 '13 #5
Rabbit
12,516 Expert Mod 8TB
That's right. It should be in a loop though.
Jun 29 '13 #6
Seth Schrock
2,965 Expert 2GB
Okay, I have the following
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Dim colClsControl As Collection
  3. Dim strTag As String
  4.  
  5. Set colClsControl = New Collection
  6.  
  7. For Each ctl In Me.Controls
  8.     Select Case ctl.ControlType
  9.         Case acComboBox, acTextBox, acCommandButton
  10.  
  11.             strTag = ctl.Properties("Tag")
  12.  
  13.             If strTag & "" <> "" And strTag <> "Open" Then
  14.                 If Not colClsControl.Exists(strTag) Then
  15.                     colClsControl.Add(New clsControl, strTag)
  16.                     'Add control to the new collection
  17.                 Else
  18.                     'Add the control to the existing collection
  19.                 End If
  20.             End If
  21.  
  22.     End Select
  23. Next ctl
  24.  
However, I'm getting a syntax error on line 15 when I try to compile it.

Also, since the name of the new clsControl (or the existing one) is stored as a variable (or the equivalent of a variable if I just used ctl.Properties("Tag"), I'm not sure how to reference it to add a control to the collection (where I have the comments).
Jul 1 '13 #7
zmbd
5,501 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. (Post#2)colClsControl(ctl.Properties("Tag")).AddControl ctl, ctl.Name 
  2. (Post#5)colClsControl.Add(New clsControl, ctl.Properties("Tag")) 
  3. (Post#7)colClsControl.Add(New clsControl, strTag)
Did the line from post #5 work? If so, then I suspect your strTag isn't resolving correctly.
However, neither line from post 5 or 7 match Rabbit's in #2.

Now I'm not really sure what you are trying to do here; thus, I really can't provide much insight - just hoping that I might jog the ole braincells here.
Jul 1 '13 #8
Rabbit
12,516 Expert Mod 8TB
Sorry, there is a slight error in my sample code. When calling a sub by itself, you have to leave off the parentheses. I will modify the original post.

As for how to add a control to the collection, that's in the last line of code in my original post.
Jul 1 '13 #9
Seth Schrock
2,965 Expert 2GB
The line from post #5 did not work. However, you pulled the wrong line from post #2.
Expand|Select|Wrap|Line Numbers
  1. Post #2  Line 8 colClsControl.Add(New clsControl, ctl.Properties("Tag"))
  2. Post #7 Line 15 colClsControl.Add(New clsControl, strTag)
I actually ran the test with the ctl.Properties("Tag") instead of the variable and it still didn't work.

But you did point out the answer to my other question.

Edit: I will give that a try Rabbit. I have been pulled away to another project at the moment, but I will test that and let you know. And thanks to Z, I found that last line of code.
Jul 1 '13 #10
zmbd
5,501 Expert Mod 4TB
You migh take a look at the following too for some insight on accessing the class module:
Class Module to Handle Opening Forms Hierarchically
...(there are a couple of other class module articles)
Simple Tutorial on Class Modules
Jul 1 '13 #11
Seth Schrock
2,965 Expert 2GB
@Z I think that those links will be helpful in the future for my understanding of class modules, but I think that my problem in this case is more with collections than class modules.

@Rabbit I'm trying to loop through the colClsControl collection, but I'm not sure what each item would be. For example, looping through controls is For Each control in Me.Controls... What is the equivalent to "control"?

Also, when I try to run the code, I'm getting an error on line 14 of post #7 saying Object doesn't support this property or method. I'm assuming the .Exists since it wasn't in the intellisense. Not sure what to try now.
Jul 1 '13 #12
Rabbit
12,516 Expert Mod 8TB
I thought you were using .Exists because that's what Intellisense was showing you was available. Collection objects have a Contains method, use that instead.

I don't know what you mean by this:
What is the equivalent to "control"?
Jul 1 '13 #13
Seth Schrock
2,965 Expert 2GB
Same error using the Contains method. Intellisense only gives Add, Count, Item, and Remove options. I used the .Exists because the Help on collections had it in its example.

Expand|Select|Wrap|Line Numbers
  1. Dim col As Collection
  2. For Each col In colClsControl
  3.     Debug.Print col.Count
  4.  
  5. Next col
What I meant was, am I correct in declaring col as a collection?
Jul 1 '13 #14
Rabbit
12,516 Expert Mod 8TB
After further research, it looks like the VBA implementation of the collection class doesn't have a method to check whether or not a key exists within the collection. Instead, you will have to trap for the error that gets thrown when attempting to access a nonexistent key. For example, with a function such as this:
Expand|Select|Wrap|Line Numbers
  1. Private Function Contains(col As Collection, key As String) As Boolean
  2.     On Error GoTo NotFound
  3.     Dim itm As Object
  4.     Set itm = col(key)
  5.     Contains = True
  6. MyExit:
  7.     Exit Function
  8. NotFound:
  9.     Contains = False
  10.     Resume MyExit
  11. End Function
No, you wouldn't declare the objects stored within the collection as a collection. Unless you are indeed storing a collection of collections. In your case, that is not what you're doing. You're storing a collection of your class clsControl so that's what you would declare it as.
Jul 1 '13 #15
zmbd
5,501 Expert Mod 4TB
for... each... itteration?
Jul 1 '13 #16
Seth Schrock
2,965 Expert 2GB
Okay. I've got everything working so far. I'm now working on lines 42 - 54 of my OP. I've got the loop to loop through each clsControl in colClsControl. I just need to be able to get the key of the clsControl so that I can use it in the criteria for the recordset so that I can determine what the enabled, visible, and locked properties need to be set to. If I'm understanding the code correctly, each new instance of clsControl is named as the value of the control's tag property. However, since I'm outside of the loop that goes through the controls, I don't have access to the control's tag property (at least in a simple list of unique values). Is there a way to pull the key value for the current clsControl? I'm looking online right now for that, but haven't found anything yet.

Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Dim colClsControl As Collection
  3. Dim strTag As String
  4. Dim col As Collection
  5. Dim cls As clsControl
  6.  
  7. Set colClsControl = New Collection
  8.  
  9. For Each ctl In Me.Controls
  10.     Select Case ctl.ControlType
  11.         Case acComboBox, acTextBox, acCommandButton
  12.  
  13.             strTag = ctl.Properties("Tag")
  14.  
  15.             If strTag & "" <> "" And strTag <> "Open" Then
  16.  
  17.                 If Not Contains(colClsControl, strTag) Then
  18.                     colClsControl.Add New clsControl, strTag
  19.                 End If
  20.  
  21.                 colClsControl(strTag).AddControl ctl, ctl.Name
  22.  
  23.             End If
  24.  
  25.     End Select
  26. Next ctl
  27.  
  28. Set db = CurrentDb
  29.  
  30. For Each cls In colClsControl
  31.     'Get key value for the current clsControl
  32.  
  33. Next cls
Jul 5 '13 #17
Seth Schrock
2,965 Expert 2GB
I found a way! I added a Key property to my class module and I set it right after line 18 using the following code:
Expand|Select|Wrap|Line Numbers
  1. If Not Contains(colClsControl, strTag) Then
  2.     colClsControl.Add New clsControl, strTag
  3.     colClsControl(strTag).Key = strTag
  4. End If
Then in my loop at the end, I can pull this value to be part of the criteria in my recordset.

If you think that there is a cleaner/better way of doing this, please let me know, but since I have it working I will move forward with my project.

Thanks Rabbit for all of your help!
Jul 5 '13 #18
Rabbit
12,516 Expert Mod 8TB
No problem, good luck on the rest of your project.
Jul 5 '13 #19

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

Similar topics

16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
4
by: Annie | last post by:
hello guys, I am getting the following error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. What i am trying to do is to loop through...
4
by: Poppy | last post by:
How can I loop through controls on a form and find out what type they are. I want to loop through controls on a webform and if they are visible textboxes change there value if NULL to "na". Also...
2
by: Chris Devol | last post by:
VB.NET 2002. I have a large group of CheckBoxes. I want to check/uncheck all the boxes whenever the "Check All" box is checked/unchecked. I also want to be able to fill an array of flags based on...
5
by: johnb41 | last post by:
I need to loop through a bunch of textbox controls on my form. The order of the loop is very important. For example, the top one must be read first, then the one below it, etc. My first...
7
by: astro | last post by:
I am not farmilar with the object model for webforms. I want to loop through the web form controls - pulling out the checkboxes on the form like the following: For Each ctrl In Me.Controls ...
1
by: Mel | last post by:
I need to loop through all form elements such as text, radios, check boxes and the like and to save their state in a file. does anyone have JS code to do this ? It can help a bunch thanks
1
by: Michael R | last post by:
Hi All. I'm trying to delete conditions from all the controls in my form via VBA command. This code doesn't work though: Dim frm As Form Dim myControl As Control Set frm = Forms!Customers ...
4
by: RamanS | last post by:
Hi, I have a form with 10 labels named (label1 to Label10). Is there a way where i can loop through these controls and set their background colors using vba? It very lengthy to write 10 lines...
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
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
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.