1

I need to get code to retrieve the Windows userID for the current session in VB (for Access 2013) on a 64-bit system.

I've tried the solution suggested at How to get logged-in user's name in Access vba?, but apparently this doesn't work on my 64-bit machine. I've also tried to figure out how to integrate the info at http://msdn.microsoft.com/en-us/library/office/gg278832.aspx, but I can't figure it out.

I am a NOVICE VB programmer, so I really need the actual code to do this. (I can [probably] figure out how & why the code does what it does after I see it, but I can't come up with it from scratch at this point.)

I'm hoping this answer will be helpful to others, too.

Thanks so much!

Aloha, -pt

Community
  • 1
  • 1
philiptdotcom
  • 11
  • 1
  • 1
  • 2

2 Answers2

4

This should work, too:

Dim wshNet As Object
Set wshNet = CreateObject("WScript.Network")
MsgBox "Hello, " & wshNet.UserName & "!"
Set wshNet = Nothing
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
3

The answer you linked to works on a 32 bit version of access. For 64 bit versions, you need to use a pointer-safe signature:

Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias
 "GetUserNameA" (ByVal lpBuffer As String, nSize As LongPtr) As Long

(it might work with nSize As Long - I don't have a 64-bit access at hand)

assylias
  • 321,522
  • 82
  • 660
  • 783
  • Should the return value be a LongPtr? I'm not familiar enough with how pointers from 32 bit code running on 64 bit systems are handled. As nSize is a parameter sent to a 32 bit DLL, I'm pretty sure it is best to leave it as a Long. – robartsd Jun 27 '19 at 16:04
  • 2
    `nSize` [is `LPDWORD`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getusernamea). Regardless of bitness, it's `ByRef nSize As Long`. – GSerg Jul 22 '20 at 19:09