Environ function on vba

  • Thread starter Thread starter gvaughn
  • Start date Start date
G

gvaughn

I am looking for information about the evniron function in vba for access. I
would like to retrieve the username of the current user on windows and use it
for log-in/log-out purposes in an interface. Any information, examples, or
suggestions on how to retrieve the user name would be appreciated. Thank you.
 
Something like this ...

Public Function GetLoginName() As String
On Error Resume Next

Dim lngen As Long
Dim lngX As Long
Dim strUserName As String

strUserName = String$(254, 0)
lngen = 255
lngX = apiGetUserName(strUserName, lngen)
If Environ$("UserID") <> "" Then
GetLoginName = Environ$("UserID")
ElseIf lngX <> 0 Then
GetLoginName = Left$(strUserName, lngen - 1)
Else
GetLoginName = "Guest"
End If

End Function
 
Ooops ... forgot to add the api declare

Declare Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
 
I don't remember where I found this, but it will give you all of the environ
values.

Dim EnvString, Indx, Msg, PathLen
Indx = 1
Do
EnvString = Environ(Indx) ' Get environment
Debug.Print EnvString
Indx = Indx + 1 Loop Until EnvString = ""
Loop Until EnvString = ""
 
Back
Top