Microsoft Access 2010 Bible

(Rick Simeone) #1

Part IV: Professional Database Development


956


GetUserNameA
The GetUserNameA function retrieves the name of the user currently logged on to the system and
places it in a string buffer.

Declare Function apiGetUserName _
Lib “Advapi32” _
Alias “GetUserNameA”( _
ByVal Buffer As String, _
BufferSize As Long) As Long

If the function is successful, it returns TRUE, and the length of the returned string is placed in the
BufferSize variable.

The user name returned by the GetUserName API function has nothing to do with the user
logged in to Access — Windows doesn’t know anything about the Access users or groups and
doesn’t monitor who’s logged in to an Access database. The following wrapper function tells you
exactly who logged on to the computer:

Function GetUserName() As String
Dim UserName As String * 255
Dim NameSize As Long
Dim RetVal As Long
NameSize = Len(UserName)
RetVal = apiGetUserName(UserName, NameSize)
GetUserName = Left$(UserName, NameSize - 1)
End Function

GetComputerNameA
The GetComputerNameA function is very similar to GetUserNameA. It returns the network
name of the local computer.

Declare Function apiGetComputerName _
Lib “Kernel32” _
Alias “GetComputerNameA”( _
ByVal Buffer As String, _
BufferSize As Long) As Long

GetComputerNameA retrieves the name of the current computer system. If the function fails, its
return value is zero; otherwise, the return value is 1, and the BufferSize argument returns the
number of characters copied to the ComputerName string variable. The GetComputerName
function is a wrapper for the GetComputerNameA declaration and returns a string containing the
name of the computer.

Function GetComputerName() As String
Dim ComputerName As String * 255
Dim NameSize As Long
Dim RetVal As Long
NameSize = Len(ComputerName)
Free download pdf