UserID (1 Viewer)

Jmsteph

Registered User.
Local time
Today, 09:48
Joined
Dec 2, 2008
Messages
28
I have a database that checks the userid that the person used to log into the PC to get into the database. It have worked for many people for many years, but all of the sudden it is failing on a new associate. Does anyone know where VBA actually pulls the users ID as I'm thinking IT set her up incorrectly.
Thanks,

Jeremy
 

boblarson

Smeghead
Local time
Today, 07:48
Joined
Jan 12, 2001
Messages
32,059
It depends on how your database is set up. The database could be using the VBA.Environ("username") syntax or it could be using the Windows API. Or it could be using Active Directory for this. Hard to say without seeing the database.
 

derekburleigh

New member
Local time
Today, 16:48
Joined
May 16, 2011
Messages
14
I don´t mean to hijack your thread, but I also have a question on UserID, the answer to which may also help you! :D

My database runs on Win XP machines and uses the following code to find the UserID:-

Dim stUser As String
Dim wsh
Set wsh = CreateObject("WScript.Shell")
stUser = wsh.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Logon User Name")

This was fine until now when we have a new PC running Win 7. This code doesn´t work on Win 7. Is there a better way of finding the UserId of the signed on person or does anyone know the Win 7 equivalent code.

I would also need the code to identify the Win version running on the PC of course!!
 

boblarson

Smeghead
Local time
Today, 07:48
Joined
Jan 12, 2001
Messages
32,059
You should not be accessing the registry directly to get user login information. You can use the

VBA.Environ("username")

code but if you want to ensure that nobody can spoof someone else (because a knowledgeable user can change the environment variable easily enough if they know what to do) then you can use this Windows API here:
http://access.mvps.org/access/api/api0008.htm
 

Jmsteph

Registered User.
Local time
Today, 09:48
Joined
Dec 2, 2008
Messages
28
Hi Bob,

I use the the modual below.

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long
Declare Function clt_OpenClipboard Lib "User32" Alias "OpenClipboard" (ByVal hwnd As Long) As Long
Declare Function clt_EmptyClipBoard Lib "User32" Alias "EmptyClipboard" () As Long
Declare Function clt_CloseClipboard Lib "User32" Alias "CloseClipboard" () As Long

Then I use the below code so that I can call it in VBA.

Public Property Get UserName() As Variant
Dim sBuffer As String
Dim lSize As Long
sBuffer = Space$(255)
lSize = Len(sBuffer)
Call GetUserName(sBuffer, lSize)
UserName = Left$(sBuffer, lSize - 1)
End Property

This allowes me to pull a users ID by calling UserName() in the VBA code. This has always worked for me, but I now have a user that is not matching what I have listed for her network ID, do the database is denying her access as I block anyone who's ID is not in the auth table.
 

Users who are viewing this thread

Top Bottom