Discussion Locked

Need the syntax for GetUserName

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I need the syntax on getting the user's id so I can direct the user to their own work table in an Access 97 database. We're using NT 4.0 and VB 6.0. I just need the code to access their logon id.

3 replies

by dklippert

Here's some code that has been floating around in the forums:
(I've put a blank line between each line of code to help indicate line wrap)

Option Compare Database

Option Explicit

'***** Code Start **************************

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 = ""

EndIf

End Function

'*********** Code End **************************

This discussion is closed to new replies.