Home

Username from Office-Account with VBA (Access2016)?

%3CLINGO-SUB%20id%3D%22lingo-sub-14297%22%20slang%3D%22en-US%22%3EUsername%20from%20Office-Account%20with%20VBA%20(Access2016)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-14297%22%20slang%3D%22en-US%22%3E%3CP%3EI%20want%20to%20read%20the%20username%20of%20the%20current%20logged%20in%20Office%20account%20in%20MS%20Access%202016%20with%20VBA%26nbsp%3B%20(not%20Windows%20or%20network%20username).%20The%20logged%20in%20user%20appears%20indeed%20in%20the%20Office2016%20applications%20respectively%20in%20the%20title%20bar%20at%20the%20top%20right.%20Environ%20()%2C%20etc.%20unfortunately%20does%20not%20help.%3C%2FP%3E%3CP%3EAs%20some%20users%20have%20different%20office%20accounts%20(partly%20for%20business%20and%20%2F%20or%20private)%20and%20change%20between%20these%26nbsp%3B(eg%20due%20to%20access%20to%20your%20own%20OneDrive%20files).%20I%20would%20like%20the%20Office-username%20(account-name)%20for%20the%20user-specific%20representation%20of%20information%20in%20forms%2C%20queries%2C%20etc.%26nbsp%3B%20I%20Have%20already%20found%20that%20the%20previously%20registered%20users%20will%20be%20added%20to%20the%20registry%20and%20are%20thus%20read%2C%20but%20which%20of%20these%20is%20the%20currently%20logged%20on%3F%3C%2FP%3E%3CP%3EThanks%20for%20your%20help%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-14297%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E2016%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAccess%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-23609%22%20slang%3D%22en-US%22%3ERe%3A%20Username%20from%20Office-Account%20with%20VBA%20(Access2016)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-23609%22%20slang%3D%22en-US%22%3E%3CP%3EUserdisplayname%20is%20only%20available%20in%20Macros%20not%20in%20VBA%20%3D%26gt%3B%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Foffice%2Fdn161030.aspx%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EUserDisplayName%20Function%20%3C%2FA%3E%3C%2FP%3E%3CP%3EUserEmailAddress%20is%20only%20available%20in%20Macros%20not%20in%20VBA%20%3D%26gt%3B%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Foffice%2Fdn142191.aspx%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EUserEmailAddress%20Function%20%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20these%20functions%20are%20only%20available%20in%20Macros%20it%20is%20not%20interesting%20for%20me%20...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20if%20I%20use%20these%20functions%20with%20a%20macro%26nbsp%3B...%20and%20I%20switch%20from%20Account%20in%20Office%20it%20will%20show%20the%20correct%20username%2Fuseremail%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-23572%22%20slang%3D%22en-US%22%3ERe%3A%20Username%20from%20Office-Account%20with%20VBA%20(Access2016)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-23572%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20Paul%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20did%20a%20little%20more%20research%20and%20remembered%20that%20Access%20has%20a%20built%20in%20function%20that%20allows%20you%20to%20access%20the%20CurrentUser%20(Name%20and%20email)%20through%20a%20macro%20or%20VBA.%20%26nbsp%3BSo%20it%20is%20not%20necessary%20to%20use%20the%20reference%20inside%20your%20database%20for%20the%20System.DirectoryServices.AccountManagement.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAttached%20is%20a%20screen%20shot%20of%20how%20to%20add%20that%20function%20using%20a%20macro.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20need%20more%20help%20I%20can%20prepare%20a%20step%20by%20step%20instruction%20to%20walk%20you%20through%20your%20exact%20requriements.%20%26nbsp%3BLet%20me%20know.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3EDon%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-23525%22%20slang%3D%22en-US%22%3ERe%3A%20Username%20from%20Office-Account%20with%20VBA%20(Access2016)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-23525%22%20slang%3D%22en-US%22%3E%3CP%3EWhat%20means%20%22Imports%20System.DirectoryServices.AccountManagement%22%20and%20how%20can%20I%20reference%20to%20it%20that%20the%20types%20are%20known%20(MS%20Access%202016)%3F%3C%2FP%3E%3CP%3EThanks%20for%20help%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-21191%22%20slang%3D%22en-US%22%3ERe%3A%20Username%20from%20Office-Account%20with%20VBA%20(Access2016)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-21191%22%20slang%3D%22en-US%22%3E%3CP%3EYes.%3C%2FP%3E%3CP%3ESo%20you%20would%20create%20a%20object%20on%20your%20form%20(or%20other%20type)%20i.e.%20Label%3C%2FP%3E%3CP%3EThen%20you%20would%20create%20some%20code%20(i.e.%20VBA%20or%20Visual%20Basic)%3C%2FP%3E%3CP%3EThen%20you%20would%20reference%20the%20Netframework%20dll%20(using%20Imports)%20i%20think%3C%2FP%3E%3CP%3EThen%20create%20the%20sub%20and%20event%20(onLoad%20maybe)%20or%20click%3C%2FP%3E%3CP%3ESo%20something%20like%20this.%20%26nbsp%3BI%20just%20don't%20know%20the%20exact%20syntax%20in%20Access%3C%2FP%3E%3CP%3EMaybe%20you%20can%20get%20it%20working%20as%20you%20suggest.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EImports%20System.DirectoryServices.AccountManagement%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20Label12_Click()%3CBR%20%2F%3EDim%20User%20As%20System.DirectoryServices.AccountManagement.UserPrincipal%3CBR%20%2F%3EUser%20%3D%20UserPrincipal.Current%3CBR%20%2F%3EDim%20UserContext%20As%20System.DirectoryServices.AccountManagement.PrincipalContext%3CBR%20%2F%3EUserContext%20%3D%20User.Context%3CBR%20%2F%3ELabel12%20%3D%20User.DisplayName%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-21169%22%20slang%3D%22en-US%22%3ERe%3A%20Username%20from%20Office-Account%20with%20VBA%20(Access2016)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-21169%22%20slang%3D%22en-US%22%3E%3CP%3EI%20can%20help%20if%20you%20explain%20how%20to%20integrate%20the%20code%20you%20suggested%20in%20the%20Access%20VBA%20environment%20...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-20757%22%20slang%3D%22en-US%22%3ERe%3A%20Username%20from%20Office-Account%20with%20VBA%20(Access2016)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-20757%22%20slang%3D%22en-US%22%3E%3CP%3EI%20use%20the%26nbsp%3BSystem.DirectoryServices.AccountManagement%20object%20to%20get%20user%20information%20on%20my%20apps.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20VB%20(I%20don't%20use%20VBA%20but%20very%20simliar%20syntax)%20the%20UserContext%20object%20give%20you%20all%20the%20information%20about%20the%20signed%20in%20user%20(Current)%20you%20need.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EImports%20System.DirectoryServices.AccountManagement%3C%2FP%3E%3CP%3EDim%20User%20As%20UserPrincipal%20%3D%20UserPrincipal.Current%3C%2FP%3E%3CP%3EDim%20UserContext%20As%20PrincipalContext%20%3D%20User.Context%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20that%20helps.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-15546%22%20slang%3D%22en-US%22%3ERe%3A%20Username%20from%20Office-Account%20with%20VBA%20(Access2016)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-15546%22%20slang%3D%22en-US%22%3E%3CP%3EGood%20question%20...%20could%20be%20helpfull%20in%20an%20application%20...%3C%2FP%3E%3CP%3EUnfortunately%20I%20have%20not%20solution%20for%20this%20yet%20...%20%3A(%3C%2Fimg%3E%20...%20I'll%20keep%20on%20searching%20...%3C%2FP%3E%3C%2FLINGO-BODY%3E
Schulleitung Attinghausen
Occasional Visitor

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

7 Replies

Good question ... could be helpfull in an application ...

Unfortunately I have not solution for this yet ... :( ... I'll keep on searching ...

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.

I can help if you explain how to integrate the code you suggested in the Access VBA environment ...

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

What means "Imports System.DirectoryServices.AccountManagement" and how can I reference to it that the types are known (MS Access 2016)?

Thanks for help

Dear Paul

 

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.

 

Thanks

Don

 

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?

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies