Get Logged-On User Information from Access Automation Object

A

Alan Cantor

Hi, your help is much appreciated.

I'm writing an Access 2002 db that has it's own mdw user/group definitions.
After a user logs on and is using one of the forms, they have the option to
check a box.

Basically, I would like to give the ability to check this special box to
admin users and NOT regular New Data Users. How do I do this?

My thoughts were to use the vb (alt-f11) macro code builder to check the
(Access.Users.CurrentlyLoggedOnUser.Group - pipe dream eh?) access
automation object somehow to extract the group or the username or the
userlevel (anything) from within the CHECKBOX_ONCLICK event. You know..
something like...

sub checkboxname_onclick()
'this ? ...
if user.level < 1 then
msgbox("you're hosed")
end if
'or this ? ...
if user.group <> "adminGroup" then
msgbox("too bad buddy")
end if
end sub

Basically, my question is .... IS this a way to access the
currently-logged-on-user information programmatically from the access
automation objects available in the F2 object browser?

Thanks for your help,
Alan Cantor
 
G

Gez

Try adding this function to a module and call it by adding the code where
required.

if CurrentUserInGroup("Admin") = True then
msgbox("too bad buddy")
End if

Function CurrentUserInGroup(GroupName As String)

Dim MyWorkSpace As Workspace, i As Integer
Dim MyGroup As group, MyUser As user

Set MyWorkSpace = DBEngine.Workspaces(0)

Set MyGroup = MyWorkSpace.Groups(GroupName)
Set MyUser = MyWorkSpace.Users(CurrentUser())
For i = 0 To MyGroup.Users.Count - 1
If MyGroup.Users(i).Name = MyUser.Name Then
CurrentUserInGroup = True
Exit Function
End If
Next i

CurrentUserInGroup = False
MyWorkSpace.Close
Exit Function

End Function
 
G

Gez

Sorry that should read but I'm sure you get the idea.

if CurrentUserInGroup("Admin") = False then
msgbox("too bad buddy")
End if
 
B

Brendan Reynolds \(MVP\)

Not from an Access object, because security in an MDB is not managed by
Access, but by JET. You can do it using DAO ...

Public Function IsUserInGroup(ByVal strUserName As String, ByVal
strGroupName As String) As Boolean

'Requires reference to Microsoft DAO 3.6 Object Library.

Dim grp As DAO.Group
Dim usr As DAO.User

Set grp = DBEngine.Workspaces(0).Groups(strGroupName)
For Each usr In grp.Users
If usr.Name = strUserName Then
IsUserInGroup = True
Exit For
End If
Next usr
Set usr = Nothing
Set grp = Nothing

End Function

.... or using ADOX ...

Public Function IsUserInGroup2(ByVal strUserName As String, ByVal
strGroupName As String) As Boolean

'Requires reference to ADOX object library ("Microsoft ADO Ext. <version
number> for DDL and Security")

Dim cat As ADOX.Catalog
Dim grp As ADOX.Group
Dim usr As ADOX.User

Set cat = New ADOX.Catalog
cat.ActiveConnection = CurrentProject.Connection
Set grp = cat.Groups(strGroupName)
For Each usr In grp.Users
If usr.Name = strUserName Then
IsUserInGroup2 = True
Exit For
End If
Next usr
Set usr = Nothing
Set grp = Nothing
Set cat = Nothing

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