Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get user login name 8

Status
Not open for further replies.

Darrylles

Programmer
Feb 7, 2002
1,758
GB
Hi all,

I've seen this solution in Tek-Tips, but I just cannot find it via 'search', I've saved the solution somewhere but can't find that either - lol.

What do I need to get the Windows user login name from within MS Access (via VB)?

This is for Win 2k and 98, but either will be appreciated (one will lead to the other).

Kind regards,

Darrylle





"Never argue with an idiot, he'll bring you down to his level - then beat you with experience." darrylles@totalise.co.uk
 
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

' Use Windows API to retrieve Userid from system
Dim strUserName As String, LogonID As String, Name As Variant
strUserName = String(8, Chr$(0))
GetUserName strUserName, 8
LogonID = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
 
Same general approach, just a little different code:

Put this in a module:
Option Compare Database
Option Explicit

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

Function get_username() As String
Dim lpBuff As String * 25
Dim ret As Long
Dim Username As String

ret = GetUserName(lpBuff, 25)
Username = VBA.Left(lpBuff, VBA.InStr(lpBuff, VBA.Chr(0)) - 1)

get_username = VBA.UCase(Username)
End Function

The function get_username() will supply the username. I.E. in a
text box, "=get_username()" will display the username on the
form or report.


----
A program is a spell cast over a computer, turning input into error messages.
 
On Windows NT, 2000 and XP you can also use =Environ("Computername") to get the workstation name, but unfortuately this doesn't work on 95/98. :-(

John
 
Darylles/DCurtis

I have to apologise: I have just tried my method on my dad's Win98 and sister's Win95 boxes and it doesn't work unfortunately on either (both with Access 97, but the Environ function reads the environment variable) ;-(
There is no USERNAME variable set on either, despite a network logon.
However it works fine on my Win2K box, so isn't really a suitable cross platform solution for Darylles.

John

 
Hi all,

Many thanks - stars all round.

John - in my question, I did say: both Win2k and Win98, so 'Environ' is relevant.

Thanks again,

Darrylle

"Never argue with an idiot, he'll bring you down to his level - then beat you with experience." darrylles@totalise.co.uk
 
Darrylle,

I can only apologise again for leading you up the garden path that works only for part of the time.
However, in my defence you do say "but either will be appreciated".

You could of course use a SET USERNAME= or SET COMPUTERNAME= line in AUTOEXEC.BAT on the Windows 98 boxes to get around this, but I think the API code provided by WMcGregor and dcurtis is more reliable.

John
 
No prob JRB

thnx

"Never argue with an idiot, he'll bring you down to his level - then beat you with experience." darrylles@totalise.co.uk
 
Just a note, that if you use Username a lot, then the environ call is a lot faster than the API call. However, as John stated, it is not necessarily a reliable function.

An alternative, that isn't quite right for an app that I have used the API in, but my future designs will use it, is create a global variable that stores the username from just one call to the API, and then reference that variable (through a function call would be neater)
 
I've written a FAQ that covers getting usernames from Access/JET, local logon, network logon, and the computername. Part of the material was grabbed from this thread. Suggestions welcome.

faq181-3779
 
I don't know if this will help anyone out but some of the other objects you can get from using environ()

I don't have a 95 or 98 to test this on, but I know it works with NT, 2000, XP

Go to start, run, type "cmd" (without the "" marks) and press enter

Type "set" (without the "" marks) and press enter

I have used this to get the username, computername etc..

I also agree with what was stated before about it not being as accurate. like with Environ("OS") on a 2000 machine will come up as NT.

Hope this helps

Ritec
 
Please correct me if I'm wrong, but I think that both of these methods will return the term "Owner" instead of the username when used with Windows XP and the logged-in user happens to be the Owner of the machine. If anyone has a way to get around this, please advise. I've been hoping that someone like Dan Appleman would write something revealing the nuances of the XP API, but I have not seen anything yet. Perhaps someone else has.
 
RITec,

The "Environ" code doesn't work on Windows 95 or 98 (not tested on Me) because there is no such environment variable.

A far more reliable method is to use the API calls also mentioned in this thread, for this very reason.

John
 
Hi,

I've put a textbox on a form and set the default value to =Environ("Username")and when i go into form view all i get #Name?....what am i doing wrong

CJB
 
apkohn,

The problem with using global variables is, well, they're global. I.E., the variable will be loaded with the name of the latest user of the database.

For the possible benefit of other novices, I worked around this by storing the user's name in an invisible field on a form that remains open while the database is in use. Since the form exists locally, each active user of the database had their own value in the control.

Now I'm smarter, so I'll be reviewing posts here for the professional approach.


HTH,
Bob
Your mileage may vary, but following the guidelines in faq181-2886 will help you reach your goal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top