|
SynopsisThis is a singleton class that can be used to return information about the currently logged in user. Class has been successful in 32bit and 64bit versions of Access. PropertiesName: Returns the currently logged on user (the LocalUser). FunctionsValidate(strPassword): Validates a users password. Could be used after a period of inactivity or before a sensitive operation of your app. How to implementCopy the code below to create a text file with the name LocalUser.cls. Then IMPORT the file into your VBA project. CodeCODE VERSION 1.0 CLASS
BEGIN MultiUse = -1 'True END Attribute VB_Name = "LocalUser" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Compare Database Option Explicit Private strName As String Private strDomain As String Private strComputerName As String Private Sub Class_Initialize() If Not Me Is LocalUser Then Err.Raise vbObjectError, "LocalUser", "LocalUser cannot have multiple instances." End If 'Set the properties With CreateObject("wscript.network") strName = .UserName strDomain = .UserDomain strComputerName = .ComputerName End With End Sub Public Property Get Name() Name = strName End Property Public Property Get Domain() Domain = strDomain End Property Public Property Get ComputerName() ComputerName = strComputerName End Property Public Function Validate(strPassword As String) As Boolean 'Validate WindowsUser active directory password Dim oADobject As Object 'IADsOpenDSObject Set oADobject = GetObject("WinNT:") On Error Resume Next oADobject.OpenDSObject "WinNT://" & strDomain, strName, strPassword, &O1 '&O1 = ADS_SECURE_AUTHENTICATION Validate = (Err = 0) End Function Public Function IsMemberOf(strGroupname As String) As Boolean 'Returns a boolean indicating if the current windows user is a member of the passed group name. IsMemberOf = Groups Like "*|" & strGroupname & "|*" End Function Public Function Groups() As String 'Returns a delimited string of all the ad groups the current windows user is a member of Dim sGroupName As String _ , sUsersGroupList As String Dim oTarget As Object Dim vGroup As Variant ' Bind to Computer object Set oTarget = GetObject("WinNT://" & strDomain & "/" & strName & ",user") For Each vGroup In oTarget.Groups sUsersGroupList = sUsersGroupList & "|" & vGroup.Name Next sUsersGroupList = UCase(sUsersGroupList) Groups = sUsersGroupList & "|" End Function Example UsageFrom the immediate window: CODE ? LocalUser.Name
someUserName ? LocalUser.ComputerName someComputerName
|
| This page has been accessed 1,223 times. This page was last modified 16:31, 25 February 2012 by Brent Spaulding. Disclaimers |