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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Member of Admins Group? 1

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
I'm trying to manage several databases. So, I need to know if the currentuser is a member of the Admins group of a given database.

For example, the user is running code in database A. I need to know if the currentuser is a member of the Admins group in database B.

The reason for this is that I have some system tables in a library database (tblPC) which define, for example, all of the computers that run database B. And all of the computers that run database C, and so on. I only want users that belong to the Admins group of database B to be able to edit the records in tblPC that that are assigned to database B. (Note that this user could have developed databases B,C, and E and, therefore, is the Administrator or those database. Consequently, the user can modify the records in tblPC for databases B, C, and E. But, since this user did not develop database D, the user is not a member of D's Admins group and, therefore, can not modify the records in tblPC for database D).

So, again my question is, how can I determine if a user is a member of the Admins group within another database?
 
Code:
Function bUserBelongsToAdminsGroup() As Boolean

Dim bIsIt As Boolean
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strSQL As String 

strSQL = "SELECT MSA1.Name " & _
         "FROM (MSysAccounts As MSA INNER JOIN MSysGroups AS MSG ON " & _ 
              "MSA.SID = MSG.GroupSID) INNER JOIN MSysAccounts AS MSA1 ON 
                   "MSG.UserSID = MSA1_1.SID " & _
         "WHERE MSA.Name= 'Admins' AND " & _
               "MSA.FGroup <> 0 AND " & _
               "MSA1.FGroup = 0 AND " & _
               "MSA1.Name = '"& CurrentUser & "';"

'If you link MSysAccounts & MSysGroups from the mdw file
Set cnn = CurrentProject.Connection 
'else create and open a new appropriate connection to that mdw
Set rst = cnn.Execute (strSQL)

If rst.EOF AND rst.BOF Then
   bIsIt = False
Else 
   bIsIt = True
End If
rst.Close
Set rst = Nothing
'Uncomment the next line of you openned a different connection
'cnn.Close
Set cnn = Nothing
bUserBelongsToAdminsGroup = bIsIt

End Function
 
Your code was very close to what I needed...thanks.

The reason I said "very close" is because database A may have a user "user1" who is a member of the Admins group. Database B may also have a user with the same name "user1" who is also a member of the Admins group. But they are not the same person. To make it work I created 3 parameter queries. Query1 contains your code referencing the workgroup of the current db. Query2 contains your code referencing the workgroup assigned to database B. Query3 joins the 2 based on Name and UserSID.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top