• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 953
  • Last Modified:

VBA - Get UserName

I read the following question

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_24604365.html

I was just wondering if there is a specific library or reference that I need to include, because I cannot get the function ENVIRON("UserName") to work?

0
VTKegan
Asked:
VTKegan
1 Solution
 
Rey Obrero (Capricorn1)Commented:
try this codes

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

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If ( lngX > 0 ) Then
        fOSUserName = Left$(strUserName, lngLen - 1)
    Else
        fOSUserName = vbNullString
    End If
End Function


from   http://www.mvps.org/access/api/api0008.htm

0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
You might also want to look at this
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Access_Forms/Q_23071163.html
, which returns the user name or station name.  It is an API call like cap posted.
JimD.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
"because I cannot get the function ENVIRON("UserName") to work?"
Are you in A2007?

mx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
VTKeganAuthor Commented:
yes
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
It appears to be problematic in A2007. Is your database in a Trusted Location?  If that doesn't fix it, then the solutions  posted will work.

mx
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<It appears to be problematic in A2007. Is your database in a Trusted Location?  If that doesn't fix it, then the solutions  posted will work.>>
  I'd still use the API call.  You can't fool the API call, but you can easily work around the environment setting.
JimD.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Yes, you can ... but that's not going to happen in my environment.  However. rather than track down the problem in A2007, I will end up using the API call anyway.

mx
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now