Logged in user

  • Thread starter Thread starter Tezza
  • Start date Start date
T

Tezza

Is there a simple way to return the logged in user name
(the username logged onto the PC not the Access user)?

The currentuser() function is not returning what I want -
it returns the default "Admin" Access user.

Win2k
Access2002

Thanks in advance,

Tezza
 
Tezza,

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

Public Function GetLoginName() As String
Dim nSize As Long
Dim lReturn As Long
Dim sUserName As String

nSize = 255
sUserName = String(nSize - 1, Chr(0))

If GetUserName(sUserName, nSize) <> 0 Then
GetLoginName = Left(sUserName, nSize - 1)
Else
GetLoginName = ""
End If
End Function
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
 
Graham,

A little more than I hoped but looks like it will do what
is desired.

Thanks very muchly

Tezza
 
Tezza,

Try:

Environ("UserName")

Works in code as well as in object design (queries, forms..)

HTH,
Nikos
 
Sorry, but I recommend very strongly against this.

Environment variables are trivial to change. Using the API approach shown by
Graham is far better.
 
Douglas

Any reason why the GetUSerName function would fail with error 122 (LastDllError) on some W2K computer. 3 of 6 W2K computers that I use the API on fail
 
Sorry, no idea.

Check the code sample at http://www.mvps.org/access/api/api0008.htm at "The
Access Web", and make sure your code is identical.

--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)


David said:
Douglas,

Any reason why the GetUSerName function would fail with error 122
(LastDllError) on some W2K computer. 3 of 6 W2K computers that I use the API
on fail.
 
Private Declare Function GetUserName Lib "Advapi32.dll" Alias "GetUserNameA"
(ByVal sBuffer As String, nSize As Long) As Long
=============================
Private Function GetUser() As String
Dim sUserName As String
Dim lgSize As Long
Dim lgLength As Long
sUserName = String(15, " ")
lgSize = Len(sUserName)
lgLength = GetUserName(sUserName, lgSize)
GetUser = Left(sUserName, lgSize - 1)
End Function
=======================
Then on form: Create a label: lblUser
Then on the form's On-Load or On-Open event:
Me![lblUser].caption = GetUser()
---Phil Szlyk
 
Works like a charm.

Thanks again!

Tezza
-----Original Message-----
Graham,

A little more than I hoped but looks like it will do what
is desired.

Thanks very muchly

Tezza


.
 

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

Back
Top