UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> LocalUser    
(Difference between revisions)
Revision as of 14:19, 25 February 2012
DatAdrenaline (Talk | contribs)
(Properties - -- Changed to match the code)
← Previous diff
Revision as of 16:05, 25 February 2012
DatAdrenaline (Talk | contribs)
(Added CLFooter tag)
Next diff →
Line 109: Line 109:
? LocalUser.ComputerName ? LocalUser.ComputerName
someComputerName[/code] someComputerName[/code]

Revision as of 16:05, 25 February 2012



This 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.


Name: Returns the currently logged on user (the LocalUser).
ComputerName: Returns the computer name the LocalUser is using.
Domain: The domain name the LocalUser resides in.


Validate(strPassword): Validates a users password. Could be used after a period of inactivity or before a sensitive operation of your app.
IsMemberOf(strGroupname): Returns a boolean indicating if LocalUser is a member of the passed in Active Directory group.
Groups(): Returns a list of all the groups the LocalUser is a member of.

How to implement

Copy the code below to create a text file with the name LocalUser.cls. Then IMPORT the file into your VBA project.


 MultiUse = -1  'True
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
   sUsersGroupList = UCase(sUsersGroupList)
   Groups = sUsersGroupList & "|"
End Function

Example Usage

From the immediate window:

? LocalUser.UserName

? LocalUser.ComputerName

Creative Commons License
LocalUser by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search