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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Username

Status
Not open for further replies.

AT76

Technical User
Apr 14, 2005
460
0
0
US
Hi,

I've been looking for a simple command to get the username from a user in a computer. Something as easy as retreiving the current date: =date().

Is there such a thing?

Thanks!
 
This is not as simple as what you are describing, however I have used this function that I got from a co-worker. You can basically call the "networkusername" function and either store the results into a variable or populate the field, etc. Hope this helps

Code:
Declare Function WNetGetUser& Lib "Mpr" alias "WNetGetUserA" (lpName As Any, ByVal lpUserName$, lpnLength&)

Public Function networkusername() As String

Dim ret As Long, buffer(512) As Byte, i As Integer
Dim wk101 As WKSTA_INFO_101, pwk101 As Long
Dim wk1 As WKSTA_USER_INFO_1, pwk1 As Long
Dim cbusername As Long
Dim computername As String, langroup As String, logondomain As String

' Clear all of the display values.
    computername = "": langroup = "": networkusername = "": logondomain = ""
    ' Windows 95 or NT - call WNetGetUser to get the name of the user.
        networkusername = Space(256)
        cbusername = Len(networkusername)
        ret = WNetGetUser(ByVal 0&, networkusername, cbusername)
        If ret = 0 Then            ' Success - strip off the null.
            networkusername = Left(networkusername, InStr(networkusername, Chr(0)) - 1)
        Else
            networkusername = ""
        End If

End Function

 
Thanks Paulette33. I will try it out. I thought their would be an easier way to do this. :)
 
Thanks for your feedback earthand fire.

I created a new textbox in Access and added this code to the Control Source of the textbox but I get an error back: #Name?

Any suggestions how to fix this?

Thanks!
 
I've just added a textbox to a form and set its control source as below and it brings back the User Name

=Environ("USERNAME")


However I've also just checked and I'm not working in Sandboxed Mode.

Create the following in a Module

Code:
Public Function GetEnviron(Expression)
  GetEnviron = VBA.Environ(Expression)
End Function

and then use

=Environ("USERNAME")

 
Correction after creating the module and function you need to set the textbox control source to:

=GetEnviron("USERNAME")


ie the name of the function

sorry for any confusion

 
AWESOME!

Thanks earthandfire!!! :)
 
In the Control Source of a text box, you need an '=' sign before the formula. Try:

Code:
=Environ("UserName")

By the way, this is not case sensitive, so 'username', 'UserName' and 'USERNAME' will all work.

Bob Stubbs
 
Thank you to all!! This website is really great! I hope someday I can help out with feedback!
 
BobStubbs in Access 2003 certain functions that Microsoft have deemed "unsafe" are not directly available for normal usage in "Sandbox Mode". To overcome this you have two options:

1: Disable Sandbox Mode or
2: Since these functions are still available in VBA, create a wrapper VBA function and call that.

I have disabled Sandbox Mode on my own machine and therefore =Environ(EnvironmentVariableName) works perfectly, however, when I enable Sandbox mode, the function fails. The wrapper function GetEnviron that I created enables the use of =GetEnviron(EnvironmentVariableName), which will work with or without Sandbox Mode enabled.
 
I used the Environ("username") without problem for XP machines, but it doesn't seem to work on Win 98 machines. How can I achieve the same result on Win 98 machine?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top