473,836 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping through groups of controls on a form

Seth Schrock
2,965 Recognized Expert Specialist
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
18 10647
Rabbit
12,516 Recognized Expert Moderator MVP
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 Recognized Expert Specialist
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 Recognized Expert Moderator MVP
No, you just loop through and check whether or not you've already added that one.
Jun 28 '13 #4
Seth Schrock
2,965 Recognized Expert Specialist
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 Recognized Expert Moderator MVP
That's right. It should be in a loop though.
Jun 29 '13 #6
Seth Schrock
2,965 Recognized Expert Specialist
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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator MVP
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 Recognized Expert Specialist
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

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

Similar topics

16
7244
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
1074
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 all of the controls on the page and if they are Checkboxes and then see if the they are checked then do something ...
4
1792
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 If I placed this code in a module or class how would the code know which page I was refering to.
2
1486
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 which boxes are checked/unchecked. Any way to do this kind of thing in a loop instead of writing a separate line for each box? -- http://www.soundclick.com/chrisdevol
5
4005
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 attempt was to put the controls in a Panel, and then loop through the controls collection of the panel. What happened is it read the controls from the bottom of my form to the top! So I tried a more manual process. I named each textbox like this:
7
5156
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 If TypeOf ctrl Is CheckBox Then 'do something here.....
1
4581
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
2039
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 For Each myControl In frm.Controls 'I also tried myControl in frm myControl.FormatConditions.Delete
4
4578
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 to set the colors each time a user selects a different color. Thanks.
0
10834
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10541
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9367
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7782
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6976
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();...
0
5645
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...
0
5817
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3108
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 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...

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.