Sep 20 2016 05:20 AM
Sep 20 2016 05:20 AM
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
Sep 26 2016 07:31 AM
Good question ... could be helpfull in an application ...
Unfortunately I have not solution for this yet ... :( ... I'll keep on searching ...
Oct 09 2016 01:40 PM
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.
Dim User As UserPrincipal = UserPrincipal.Current
Dim UserContext As PrincipalContext = User.Context
Hope that helps.
Oct 11 2016 02:51 AM
I can help if you explain how to integrate the code you suggested in the Access VBA environment ...
Oct 11 2016 05:18 AM
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.
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
Oct 20 2016 12:29 AM
What means "Imports System.DirectoryServices.AccountManagement" and how can I reference to it that the types are known (MS Access 2016)?
Thanks for help
Oct 20 2016 04:19 AM
I did a little more research and remembered that Access has a built in function that allows you to access the CurrentUser (Name and email) through a macro or VBA. So it is not necessary to use the reference inside your database for the System.DirectoryServices.AccountManagement.
Attached is a screen shot of how to add that function using a macro.
If you need more help I can prepare a step by step instruction to walk you through your exact requriements. Let me know.
Oct 20 2016 05:51 AM
Userdisplayname is only available in Macros not in VBA => UserDisplayName Function
UserEmailAddress is only available in Macros not in VBA => UserEmailAddress Function
If these functions are only available in Macros it is not interesting for me ...
But if I use these functions with a macro ... and I switch from Account in Office it will show the correct username/useremail?
Aug 26 2020 02:25 AM
Aug 26 2020 03:23 AM