Futures_Bright
Registered User.
- Local time
- Today, 19:31
- Joined
- Feb 4, 2013
- Messages
- 69
[Solved] Collection - For Each loop doesn't appear to work
Hi all,
Another day, another problem with my database design it seems. I decided to test the User Permissions module in my database today and unfortunately it didn't work as I expected. The code is below with the problem area highlighted.
When UserAccess = "Admin", the code works fine. The reason that Set ctl = Nothing is set as comments is because that appeared to be the problem at this point (ctl = nothing), ctl.Name is "<Object variable or With Block not set>". I based this code on the one found through this link, except I'm adding controls to multiple groups and then setting the permission based upon the user's Access level.
When stepping through the process, ctl doesn't change from Nothing once it is judging the users access level and so does not loop each of the For statements (presumably because there 'is no next').
Can anyone tell me what I've done wrong here please? have i referenced the collections incorrectly? Are there problems when having 'For Each' loops in series?
Hi all,
Another day, another problem with my database design it seems. I decided to test the User Permissions module in my database today and unfortunately it didn't work as I expected. The code is below with the problem area highlighted.
Code:
Dim ctl As Control
Dim frmCurrentForm As Form
Dim frmName As String
Dim colUser As Collection
Dim colReviewVi As Collection
Dim colReviewEd As Collection
Dim colAdmin As Collection
On Error GoTo err_Permissions
Set frmCurrentForm = Screen.ActiveForm
frmName = frmCurrentForm.Name
For Each ctl In Forms(frmName).Controls
'Make all controls invisible and uneditable
ctl.Visible = False
'if label, Enabled doesn't exist and so goes to error
ctl.Enabled = False
Next ctl
Set ctl = Nothing
'Group all controls
For Each ctl In Forms(frmName).Controls
Select Case ctl.Tag
Case "User"
colUser.Add ctl, ctl.Name
Case "ReviewVi"
colReviewVi.Add ctl, ctl.Name
Case "ReviewEd"
colReviewEd.Add ctl, ctl.Name
Case "Admin"
colAdmin.Add ctl, ctl.Name
Case Else
On Error Resume Next
colUser.Add ctl, ctl.Name
End Select
Next ctl
' Set ctl = Nothing
'enable and make visible relevant controls
[COLOR=red]Select Case UserAccess[/COLOR]
Case "Admin"
For Each ctl In Forms(frmName).Controls
ctl.Visible = True
ctl.Enabled = True
Next ctl
' Set ctl = Nothing
[COLOR=red]Case "Reviewer"[/COLOR]
[COLOR=red] For Each ctl In colUser[/COLOR]
[COLOR=red] ctl.Visible = True[/COLOR]
[COLOR=red] Next ctl[/COLOR]
[COLOR=red]' Set ctl = Nothing[/COLOR]
[COLOR=red] For Each ctl In colReviewVi[/COLOR]
[COLOR=red] ctl.Visible = True[/COLOR]
[COLOR=red] Next ctl[/COLOR]
[COLOR=red]' Set ctl = Nothing[/COLOR]
[COLOR=red] For Each ctl In colReviewEd[/COLOR]
[COLOR=red] ctl.Visible = True[/COLOR]
[COLOR=red] ctl.Enabled = True[/COLOR]
[COLOR=red] Next ctl[/COLOR]
[COLOR=red]' Set ctl = Nothing[/COLOR]
Case "User"
For Each ctl In colUser
ctl.Visible = True
Next ctl
' Set ctl = Nothing
Case Else
MsgBox "You have not been assigned permissions for this library. Please contact the administrators immediately", vbCritical, "Error - User Permissions"
End Select
When UserAccess = "Admin", the code works fine. The reason that Set ctl = Nothing is set as comments is because that appeared to be the problem at this point (ctl = nothing), ctl.Name is "<Object variable or With Block not set>". I based this code on the one found through this link, except I'm adding controls to multiple groups and then setting the permission based upon the user's Access level.
When stepping through the process, ctl doesn't change from Nothing once it is judging the users access level and so does not loop each of the For statements (presumably because there 'is no next').
Can anyone tell me what I've done wrong here please? have i referenced the collections incorrectly? Are there problems when having 'For Each' loops in series?
Last edited: