Getting Active Directory Group Membership in Access
Okay, it’s time for something a little geeky that I’m rather proud of.
Here’s the deal: At work, we have a Microsoft SQL database with an Access front end. At the moment, there’s no real permissions model or anything, but my boss wants me to start fine-tuning that a little. I want something that both I can look at and that she can look at and have it make sense, while also making sure it’s secure enough where any halfway savvy user can’t just break it. I thought about a number of different approaches, including…
- Creating a set of tables and queries in the database itself that handles permissions via Access VBA functions. Finding a way to restrict access to the tables to just the people I want access to would have been problematic, in that it would require a second step (Access Workgroup Security or MS SQL security), which defeats the whole point.
- Access Workgroup Security - This was a no-go because of how the Access front-end is deployed. Plus, it seemed overkill for something we want to throw in gradually. That said, we may end up adopting this later, or just changing the front-end so it’s web-based instead of Access based, that way it’s a little easier to manage or abstract away some of the
- MS SQL security - Good on a table-by-table basis, but not particularly useful or easy to manage on a column-by-column basis, which is basically what we’re looking for.
So, I thought to myself, what if we managed group membership in Active Directory (editable by only domain administrators here) and created a function in Access that could query that to determine whether or not a user is a member of an appropriate group? Sounds simple, right? Well, it took a bit, but, after playing with Scriptomatic and finding a WMI call that Access would actually let me pull off, this ended up becoming the final result:
Function GetGroupFromAD(GroupName As String) As Boolean ' Checks to see if the current user is in the group name ' passed to the function. Useful for managing permissions. GetGroupFromAD = False Dim strGroupComponent As String Dim strPartComponent As String Dim strDomain As String ' These strings build the WQL query that's passed via WMI. strDomain = """Your Domain Name"""strGroupComponent = "GroupComponent='Win32_Group.Domain=" & _ strDomain & ",Name=""" & GroupName & """'" strPartComponent = "PartComponent='Win32_UserAccount.Domain=" & _ strDomain & ",Name=""" & Environ("UserName") & """'"Set objWMIService = GetObject("winmgmts:\\" & _ Environ("ComputerName") & "\root\CIMV2") Set colltems = objWMIService.ExecQuery("SELECT * FROM " & _ "Win32_GroupUser WHERE (" & strGroupComponent & _ " AND " & strPartComponent & ")", "WQL") ' If the query returns a record, that means the ' user is a member of the group. Otherwise, the ' query will return no records. If colltems.Count > 0 Then GetGroupFromAD = True End If End Function
Then, you could do things like…
Private Sub Form_Current()
Me.SomeField.Locked = Not GetGroupFromAD "Group Name"
End Sub
Granted, someone could still get into the table itself and munge things if that was their pleasure, but, to be fair, if you’re using an Access front-end, you’re probably not looking for Fort Knox-style security anyways.
Enjoy!

