Forum Discussion
Schulleitung Attinghausen
Sep 20, 2016Copper Contributor
Username from Office-Account with VBA (Access2016)?
I want to read the username of the current logged in Office account in MS Access 2016 with VBA (not Windows or network username). The logged in user appears indeed in the Office2016 applications respectively in the title bar at the top right. Environ (), etc. unfortunately does not help.
As some users have different office accounts (partly for business and / or private) and change between these (eg due to access to your own OneDrive files). I would like the Office-username (account-name) for the user-specific representation of information in forms, queries, etc. I Have already found that the previously registered users will be added to the registry and are thus read, but which of these is the currently logged on?
Thanks for your help
- Phil_LinellCopper Contributor
Hi I was looking for something similar and came across this excellent explanation:
https://www.youtube.com/watch?v=pJL1CCfvf7s
Public Function GetUserName() As String
GetUserName = Environ("Username")
End FunctionHope it helps
Phil
- You should never use Environ values as they can easily be spoofed!
https://www.devhut.net/2018/04/12/vba-recognize-user-get-username/
- Don DeCarloBrass Contributor
I use the System.DirectoryServices.AccountManagement object to get user information on my apps.
In VB (I don't use VBA but very simliar syntax) the UserContext object give you all the information about the signed in user (Current) you need.
Imports System.DirectoryServices.AccountManagement
Dim User As UserPrincipal = UserPrincipal.Current
Dim UserContext As PrincipalContext = User.Context
Hope that helps.
- Luc DeboisIron Contributor
I can help if you explain how to integrate the code you suggested in the Access VBA environment ...
- Don DeCarloBrass Contributor
Yes.
So you would create a object on your form (or other type) i.e. Label
Then you would create some code (i.e. VBA or Visual Basic)
Then you would reference the Netframework dll (using Imports) i think
Then create the sub and event (onLoad maybe) or click
So something like this. I just don't know the exact syntax in Access
Maybe you can get it working as you suggest.
Imports System.DirectoryServices.AccountManagement
Private Sub Label12_Click()
Dim User As System.DirectoryServices.AccountManagement.UserPrincipal
User = UserPrincipal.Current
Dim UserContext As System.DirectoryServices.AccountManagement.PrincipalContext
UserContext = User.Context
Label12 = User.DisplayName
End Sub
- Luc DeboisIron Contributor
Good question ... could be helpfull in an application ...
Unfortunately I have not solution for this yet ... :-( ... I'll keep on searching ...