How can I recognize groups in VBA?

H

hanski

Hi.

I asked before how to get the group name via VBA and I got the
answer: "Workspaces(0).Users(CurrentUser).Groups(0).Name ", but my
machine says that: "Compile error. Invalid use of property", and it
clicking the Name. What wrong with that and how do I get the group
name? Why it is not as simple as getting the user name;
Currentuser()????

hanski
 
S

Sandra Daigle

Try this instead: Dbengine.Workspaces(0).Users(CurrentUser).Groups(0).Name

*However*, you need to realize that a user can be in multiple groups so this
is only going to give you the first group name (which is why it is not so
simple) If you want to check membership in a particular group you will need
to loop through the groups collection. Here is some sample code:

if UserInGroup(currentUser(),"MyGroup ") then
msgbox "User is in the group"
end if

The UserInGroup function can be done a couple of ways the first involves 2
functions - one to ensure that the group exists, then if it does exist, the
second to determine whether the user is in the group. This method is a bit
more reliable than the second I'll show later:

Public Function UserInGroup(strUser As String, _
strGroup As String) As Boolean
Dim ws As dao.Workspace
Dim strUserName As String
Dim usr As dao.User
Dim inti As Integer
Dim fUserInGroup As Boolean
Set ws = DBEngine.Workspaces(0)
If GroupExists(strGroup) Then
For inti = 0 To ws.Groups(strGroup).Users.count - 1
If ws.Groups(strGroup).Users(inti).Name = strUser Then
fIsUserInGroup = True
End If
Next inti
End If
UserInGroup = fUserInGroup
set ws=nothing
End Function

Public Function GroupExists(strGroup) As Boolean
Dim ws As dao.Workspace
Dim strUserName As String
Dim usr As dao.User
Dim inti As Integer
Dim fGroupExists As Boolean
Set ws = DBEngine.Workspaces(0)
For inti = 0 To ws.Groups.count - 1
If ws.Groups(inti).Name = strGroup Then
fGroupExists = True
End If
Next inti
GroupExists = fGroupExists
set ws=nothing
End Function


Here's the one function method - note that it does not differentiate the
error that occurs if marked line fails.

Function UserInGroup(strUser As String, _
strGroup As String) As Boolean
Dim ws As Workspace
Dim strUserName As String
Set ws = DBEngine.Workspaces(0)
On Error Resume Next
' if this normally line fails then the user is not in the group
' but it could fail due to some other error condition so
' this method might be considered a bit sloppy
strUserName = ws.Groups(strGroup).Users(strUser).Name
UserInGroup = (Err = 0)
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top