IpbWiki Core is not installed yet. ipbwiki class not installed, sorry auto-authentication is not possible... LocalUser - Access wiki - Access Help and How-to - Microsoft Office
  Main Page | Recent changes | Edit this page | Page history

Printable version | Disclaimers | Privacy policy | Current revision

Not logged in
Log in | Help


Revision as of 06:10, 25 February 2012; view current revision
←Older revision | Newer revision→



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.


UserName: 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

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
   sUsersGroupList = UCase(sUsersGroupList)
   Groups = sUsersGroupList & "|"

End Function[code]

Example Usage

From the debug window:

[code]? LocalUser.UserName someUserName

? LocalUser.ComputerName someComputerName[/code]

[Main Page]

Edit this page
Discuss this page
Page history
What links here
Related changes

Special pages
Bug reports