Environ function on vba

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.
 
D

Danny J. Lesandrini

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
 
D

Danny J. Lesandrini

Ooops ... forgot to add the api declare

Declare Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
 
A

AG

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 = ""
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top