Displaying the User Name and in a Text or label box. Environ("username")

B

bbcdancer

Having worked out how to use the Environ("username") method to obtain a
users login ID as a point of tracing records enter in MS Access. I
encountered problems using the Environ("username") method in Access
2000 and 2003.

A friend has cut and paste the below code as a solution, but I'm not
too sure how to display the username login details in a text or label
box in a form, so the user can see their login profile.

Could you provide me some simple sets to using to doing this.

Kind regards and many thanks,

Brenda




Option Compare Database
Option Explicit
Private Declare Function GetComputerNameA Lib "kernel32" (ByVal
lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserName Lib "ADVAPI32.dll" Alias
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetComputerName() As String
On Error GoTo Err_GetComputerName
Dim Username As String * 255
Call GetComputerNameA(Username, 255)
GetComputerName = Left$(Username, InStr(Username, Chr$(0)) - 1)

Exit_GetComputerName:
Exit Function

Err_GetComputerName:
MsgBox Err.Description
Resume Exit_GetComputerName

End Function

Public Function GetCurrentUserName() As String
On Error GoTo Err_GetCurrentUserName
Dim lpBuff As String * 25
Dim ret As Long, Username As String
ret = GetUserName(lpBuff, 25)
Username = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
GetCurrentUserName = Username & ""

Exit_GetCurrentUserName:
Exit Function

Err_GetCurrentUserName:
MsgBox Err.Description
Resume Exit_GetCurrentUserName
End Function
 
G

Guest

Add the code you listed to a new Module in your access database.

Set the Default Value of your form’s textbox to
=GetCurrentUserName()

Basically anywhere you used Environ("username"), use GetCurrentUserName() in
its place.
 

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

Top