Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable Command Buttons depending on user Group

Status
Not open for further replies.

stickers

Technical User
Nov 25, 2002
82
GB
I have had a look at a few threads on this and the Access VBA forum (and the FAQ's), and can't really find an answer. I have a switchboard type form on which I want to disable some buttons (6 of them) for one group of users. Can anyone help with some code? I've tried variations on the following which I got from an old thread: (it doesn't do the trick). Thanks for any help that anyone can give me. I really hope some time soon I can return the favour!!

if checkadmin = true
'then the person is a membor, so run what ever you want
else
'the current user is not a membor of the admins group...
end if




Public Function checkAdmin() As Boolean

Dim grp As Group
Dim i As Integer
Dim Flag As Integer

Set grp = DBEngine.Workspaces(0).Groups("Admins")
Flag = 0

For i = 0 To grp.Users.count - 1
If CurrentUser = grp.Users(i).name Then
Flag = 1
End If
Next i

If Flag = 1 Then
checkAdmin = True
End If


End Function
 
The following is some code that checks to see if a user is part of a specified group. It returns true if the user is part of the group, else returns false. Your code should look something like this (I would put it in the OnCurrent event of the form).

Dim bolVisible as Boolean

If (MemberOfGroup("YourGroupNameToCheck","LoginNameOfUser")) Then
bolVisible = True
else
bolVisible = False
End If

txtBox1.Enabled = bolVisible
txtBox2.Enabled = bolVisible

Here's the function:

Function MemberOfGroup(strGroupName As String, Optional varCurrentUser As Variant) As Boolean

'********************************
'* Declaration Specifications *
'********************************

Dim wsp As Workspace
Dim grp As Group
Dim usr As User

Dim var As Variant
Dim bolAnswer As Boolean

Dim strCurrentUser As String

'****************
'* Initialize *
'****************

On Error GoTo ErrHandler

bolAnswer = False

If (IsMissing(varCurrentUser)) Then
strCurrentUser = CurrentUser
Else
strCurrentUser = CStr(varCurrentUser)
End If

Set wsp = DBEngine.Workspaces(0)
Set grp = wsp.Groups(strGroupName)

On Error Resume Next
Set usr = wsp.Users(strCurrentUser)
If (Err.Number = 3265) Then GoTo ExitProcedure

On Error GoTo ErrHandler
wsp.Close

'******************************************************************
'* Loop to determine if the User is part of the group specified *
'******************************************************************

For Each var In grp.Users
If var.Name = usr.Name Then
bolAnswer = True
GoTo ExitProcedure
End If
Next

'********************
'* Exit Procedure *
'********************

ExitProcedure:

MemberOfGroup = bolAnswer

Exit Function

'****************************
'* Error Recovery Section *
'****************************

ErrHandler:

MsgBox Err.Number & ": " & Err.Description

wsp.Close
Resume ExitProcedure

End Function
 
oops. Hit the submit button too soon. You should enable/disable the controls on the OnOpen event of the form.
 
Hi FancyPrairie,

Thanks for the code - I've tried putting it in the OnOpen event and what I get is an error message telling me that a User-defined type is not defined. The line that is highlighted is:

Dim wsp As Workspace

I'm pretty new at all this - have I put the Function in the wrong place? Should it go in the general bit of the form's module? The code all looks OK to me but I might not have used it correctly????
 
Try this:

Dim wsp as DAO.Workspace

Aslo, set a reference to: Microsoft DAO 3.6 Object Library
 

Many thanks for your reply - I've followed your advice but get a different error message now - 3265:Item not found in this collection. I've been having a look at Help, and the MS website and tried some code from there, but that produced error 91. So I've gone back to your original code - if you have any more ideas I'd be very grateful!!
 
Sorry! It all works perfectly now!! I'm not quite sure just what I've done but somehow it's fine now. In the end I didn't need the DAO. bit. Thank you so much for your help!!
 
Wrote the following answer, but before I submitted it, noticed that you have figure out the problem. However, thought I would post it anyway, in case you don't know how to use debug. Knowing a little about debug, really helps solve these kinds of problems.

Error 3265 may be an error you want to trap (note that I trap it). It could be you misspelled the group name. However, the best way to figure out your problem is to use debug to step thru the code to see where it is erroring out and why.

If you don't know how to use debug, it's pretty simple to get started.

First set a breakpoint on the line that says "bolAnswer = False" or type the word Stop right after this line. Then execute your code like you normally would. When Access encounters the breakpoint or the word Stop, it will pause execution of your program. At this point examine some of your variables. To examine a variable, place the cursor over the variable (a tooltip will be displayed contianing the value of the variable). Or, in Debug's Immediate window type ?variableName (where variable name is the name of the variable you want to examine). Press F8 to move thru your code one line at a time. Press F5 to execute your code until it encounters the next breakpoint (or the word Stop) or the program exits.

Let me know how this works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top