473,882 Members | 1,695 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
18 10654
zmbd
5,501 Recognized Expert Moderator Expert
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 Recognized Expert Specialist
@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 Recognized Expert Moderator MVP
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 Recognized Expert Specialist
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 Recognized Expert Moderator MVP
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 Recognized Expert Moderator Expert
for... each... itteration?
Jul 1 '13 #16
Seth Schrock
2,965 Recognized Expert Specialist
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 Recognized Expert Specialist
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 Recognized Expert Moderator MVP
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
7248
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
1795
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
4008
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
5157
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
4582
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
9777
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,...
0
11109
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...
1
10833
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,...
0
10405
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7957
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
5782
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
5979
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3226
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.