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. - Private Sub Form_Load()
-
On Error GoTo Error_Handler
-
-
Dim ctl As Control
-
Dim Area8 As clsControl
-
Dim Area9 As clsControl
-
Dim Area10 As clsControl
-
Dim db As DAO.Database
-
Dim rst As DAO.Recordset
-
Dim strCriteria As String
-
-
-
Set Area8 = New clsControl
-
Set Area9 = New clsControl
-
Set Area10 = New clsControl
-
-
Set db = CurrentDb
-
Set rst = db.OpenRecordset("qryUserPermissions")
-
-
For Each ctl In Me.Controls
-
Select Case ctl.ControlType
-
Case acComboBox, acTextBox, acCommandButton
-
If ctl.Properties("Tag") = "Open" Or ctl.Properties("Tag") = "" Then
-
ctl.Properties("Locked") = False
-
ctl.Properties("Visible") = True
-
Else
-
Select Case ctl.Properties("Tag")
-
Case 8
-
Area8.AddControl ctl, ctl.Name
-
-
Case 9
-
Area9.AddControl ctl, ctl.Name
-
-
Case 10
-
Area10.AddControl ctl, ctl.Name
-
-
End Select
-
End If
-
End Select
-
Next ctl
-
-
With rst
-
strCriteria = "AreaID_fk = 8"
-
.FindFirst strCriteria
-
Area8.Permissions !MaxOfPermissionLevel
-
-
strCriteria = "AreaID_fk = 9"
-
.FindFirst strCriteria
-
Area9.Permissions !MaxOfPermissionLevel
-
-
strCriteria = "AreaID_fk = 10"
-
.FindFirst strCriteria
-
Area10.Permissions !MaxOfPermissionLevel
-
End With
-
-
Exit_Procedure:
-
Set Area8 = Nothing
-
Set Area9 = Nothing
-
Set Area10 = Nothing
-
Set db = Nothing
-
Set rst = Nothing
-
-
Exit Sub
-
-
Error_Handler:
-
Call ErrorMessage(Err.Number, Err.Description, "Form_frmAdminForms: Form_Load")
-
Resume Exit_Procedure
-
Resume
-
-
End Sub
Use a collection of clsControl. - ' Setup the collection
-
Dim colClsControl As Collection
-
Set colClsControl = New Collection
-
-
' Loop through all controls to get unique tags and run this
-
' An alternative is to use the contains method to check
-
' the existence of the key before attempting to access it.
-
colClsControl.Add New clsControl, ctl.Properties("Tag")
-
-
' Access the collection to add controls like this
-
colClsControl(ctl.Properties("Tag")).AddControl ctl, ctl.Name
18 10593
Use a collection of clsControl. - ' Setup the collection
-
Dim colClsControl As Collection
-
Set colClsControl = New Collection
-
-
' Loop through all controls to get unique tags and run this
-
' An alternative is to use the contains method to check
-
' the existence of the key before attempting to access it.
-
colClsControl.Add New clsControl, ctl.Properties("Tag")
-
-
' Access the collection to add controls like this
-
colClsControl(ctl.Properties("Tag")).AddControl ctl, ctl.Name
How do I get the unique tag values? Is there a SELECT DISTINCT function available for all of the tag values?
No, you just loop through and check whether or not you've already added that one.
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. - Dim ctl As Control
-
Dim colClsControl as Collection
-
Set colClscontrol = New Collection
-
-
If Not colClsControl.Exists(ctl.Properties("Tag")) Then
-
colClsControl.Add(New clsControl, ctl.Properties("Tag"))
-
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?
That's right. It should be in a loop though.
Okay, I have the following - Dim ctl As Control
-
Dim colClsControl As Collection
-
Dim strTag As String
-
-
Set colClsControl = New Collection
-
-
For Each ctl In Me.Controls
-
Select Case ctl.ControlType
-
Case acComboBox, acTextBox, acCommandButton
-
-
strTag = ctl.Properties("Tag")
-
-
If strTag & "" <> "" And strTag <> "Open" Then
-
If Not colClsControl.Exists(strTag) Then
-
colClsControl.Add(New clsControl, strTag)
-
'Add control to the new collection
-
Else
-
'Add the control to the existing collection
-
End If
-
End If
-
-
End Select
-
Next ctl
-
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).
zmbd 5,501
Expert Mod 4TB - (Post#2)colClsControl(ctl.Properties("Tag")).AddControl ctl, ctl.Name
-
(Post#5)colClsControl.Add(New clsControl, ctl.Properties("Tag"))
-
(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.
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.
The line from post #5 did not work. However, you pulled the wrong line from post #2. - Post #2 Line 8 colClsControl.Add(New clsControl, ctl.Properties("Tag"))
-
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.
@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.
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"?
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. - Dim col As Collection
-
For Each col In colClsControl
-
Debug.Print col.Count
-
-
Next col
What I meant was, am I correct in declaring col as a collection?
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: - Private Function Contains(col As Collection, key As String) As Boolean
-
On Error GoTo NotFound
-
Dim itm As Object
-
Set itm = col(key)
-
Contains = True
-
MyExit:
-
Exit Function
-
NotFound:
-
Contains = False
-
Resume MyExit
-
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.
zmbd 5,501
Expert Mod 4TB
for... each... itteration?
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. - Dim ctl As Control
-
Dim colClsControl As Collection
-
Dim strTag As String
-
Dim col As Collection
-
Dim cls As clsControl
-
-
Set colClsControl = New Collection
-
-
For Each ctl In Me.Controls
-
Select Case ctl.ControlType
-
Case acComboBox, acTextBox, acCommandButton
-
-
strTag = ctl.Properties("Tag")
-
-
If strTag & "" <> "" And strTag <> "Open" Then
-
-
If Not Contains(colClsControl, strTag) Then
-
colClsControl.Add New clsControl, strTag
-
End If
-
-
colClsControl(strTag).AddControl ctl, ctl.Name
-
-
End If
-
-
End Select
-
Next ctl
-
-
Set db = CurrentDb
-
-
For Each cls In colClsControl
-
'Get key value for the current clsControl
-
-
Next cls
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: - If Not Contains(colClsControl, strTag) Then
-
colClsControl.Add New clsControl, strTag
-
colClsControl(strTag).Key = strTag
-
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!
No problem, good luck on the rest of your project.
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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
|
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
...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |