get network username

N

Newbie

Hi
I am using the code from the Access web to obtain the user name of the
person that is logged on.

However it returns the following it appears to return the user name and a
load of square control characters (?)

How do I get rid of these as I want the username to populate a textbox so
that users don't have to keep typing their username in

FYI here is the code off the access web

'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
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 = vbNullString
End If
End Function
'******************** Code End **************************
 
J

James

Try this

Put this at the top under option explixit:

' Declare for call to mpr.dll.
Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long
Const NoError = 0 'The Function call was successful

And then the function:

Function GetUserName() As String

' Buffer size for the return string.
Const lpnLength As Integer = 255

Dim dbs As Database

' Get return buffer space.
Dim status As Integer

' For getting user information.
Dim lpName, lpUserName As String

' Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)

' Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)

' See whether error occurred.
If status = NoError Then
' 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 used
' cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr
(0)) - 1)
Else

' An error occurred.
MsgBox "Unable to get the name."
End
End If

' Display the name of the person logged on to the
machine.
Set dbs = CurrentDb
Application.RefreshTitleBar
GetUserName = lpUserName

End Function
 
D

Dirk Maes

why not simply use :

Function fOSUserName() As String
fOSUserName = Environ("USERNAME")
End Function

?

Dirk
 
J

JohnFol

Nice shortcut way of doing it. I guess the API approach may be better as on
some OS's the value USERNAME might not be an environment variable
 

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