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!

Getting "User Name" while in Access db 1

Status
Not open for further replies.

GregGood

Programmer
Jan 24, 2004
16
US
How do I get the Log-in User Name when I am in an Access DB?
 
hi,
Code:
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function UserName() As String
    Dim lLen As Long
    
    lLen = 255
    
    UserName = String$(lLen - 1, 0)
    
    If apiGetUserName(UserName, lLen) > 0 Then
        UserName = Left$(UserName, lLen - 1)
    Else
        UserName = vbNullString
    End If
End Function

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 

You may try (in VBA):

Code:
Environ("Username")

Have fun.

---- Andy
 
Greg ... I had the same issue yesterday. I found this code online that will pull the user name used to log into Windows. I've applied it to my code and it works great.

JT

Option Compare Database
Option Explicit

'UserID variables
Public strSessionUserId As String
Public strSessionComputerId As String

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
'

Public Function fGetUserName()

'Returns a string representing the Windows Username
Dim lpName As String
Dim strTemp As String

'Set up buffer string
lpName = String$(255, 0)

Call GetUserName(lpName, 255)
If lpName = "" Then
strTemp = "NoUserName"
Else
strTemp = lpName
End If
fGetUserName = fStripNull(strTemp)

End Function

Public Function fGetComputerName()

'Returns a string representing the Windows Username
Dim lpName As String
Dim strTemp As String

'Set up buffer string
lpName = String$(255, 0)

Call GetComputerName(lpName, 255)
If lpName = "" Then
strTemp = "NoComputerName"
Else
strTemp = lpName
End If
fGetComputerName = fStripNull(strTemp)

End Function

Public Function fStripNull(StripText As String)

Dim strTemp As String
Dim intNum As Integer
Dim i As Integer

'Returns a string clear of trailing nulls (CHR(0)) and in the
'ASCII range 48-57, 65-90, 95 & 97-122 (0-9, A-Z, _, & a-z)

strTemp = ""
For i = 1 To Len(StripText)
intNum = Asc(Mid(StripText, i, 1))
Select Case intNum
Case 48 To 57, 65 To 90, 95, 97 To 122
strTemp = strTemp + Mid(StripText, i, 1)
Case Else
End Select
Next i

fStripNull = strTemp

End Function
 
NameField=Environ("Username")[like Andrzejek said]
ComputerField=Environ("ComputerName")

works well for network and local users. Not sure why you'd need the massive chunk of code unless you were reformatting it (me.user to User, Me... etc)
 
Also the variables don't exist on Windows 9x if you are supporting running on old OSes (shouldn't be much of an issue now though).

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top