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!

Visual Basic to obtain the Windows User Name 1

Status
Not open for further replies.
Jul 6, 2005
52
GB
I am trying to build some security into a database and would like to restrict data viewing according to the user. If I could access the Windows User Name for this, it would be ideal. I believe this can be done using Visual Basic but have absolutely no idea where to start. Can anyone point me in the right direction?
 
Environ("username") seems really easy. are there any limitations to that method?

I have been using the following code (which, to be honest, I don't really understand) to retrieve the user name:

Code:
Option Compare Database
Option Explicit

Global intFrom As Integer

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

Any advantages to using API instead of Environ?
thanks
 
Hi ecannelora
I have a vague memory of something like this coming up before, so I did a search, but could not find the post I was thinking of. However, I did come across this:
Environ("Username") not working for one person
thread181-967767
The above post mentions sandbox mode, which was talked about in a few other posts that were found.
I guess you know that the api method is also the one posted on mvps.org:
 
I have written some FAQ on this that may help some are under the forms area.


Never give up never give in.

There are no short cuts to anything worth doing :)
 
Environ("USERNAME") is just an environment variable which can be changed on accident (not that it ever happens). Also it isn't set in Windows 98/Me systems, which is less of a problem now than it used to be.

I uh, wrote my own FAQ on the subject...
Getting all types of Usernames in Access faq181-3779
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top