Copy the code below into a module and name it GetUserName
You can use it to return the user name in Queries, Text boxes, Etc by calling it. Type the value of any expression as GetUserName() and it will return the strin
Option Compare Databas
Option Explici
' Declare for call to mpr.dll
Public Declare Function WNetGetUser Lib "mpr.dll"
Alias "WNetGetUserA" (ByVal lpName As String,
ByVal lpUserName As String, lpnLength As Long) As Lon
Const NoError = 0 'The Function call was successfu
Public Function GetUserName() As Strin
' Buffer size for the return string
Const lpnLength As Integer = 25
' Get return buffer space
Dim Status As Intege
' For getting user information
Dim lpName As Strin
' Assign the buffer size constant to GetUserName
GetUserName = Space$(lpnLength + 1
' Get the log-on name of the person using product
Status = WNetGetUser(lpName, GetUserName, lpnLength
' See whether error occurred
If Status = NoError The
' This line removes the null character. Strings in C are null
' terminated. Strings in Visual Basic are not null-terminated
' The null character must be removed from the C strings to be use
' cleanly in Visual Basic
GetUserName = Left$(GetUserName, InStr(GetUserName, Chr(0)) - 1
Els
' An error occurred
MsgBox "Unable to retrieve user name.
En
End I
End Functio