MS Access 97 GetUserName Function Conversion Problem

J

jm

Using Access97, I was able to extract the Windows login name using the
following function:
Option Compare Database
Option Explicit



' 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

Public Function GetUserName() As String

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

' 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.
GetUserName = "Unknown" 'MsgBox "Unable to get the name."
End
End If

' Display the name of the person logged on to the machine.
GetUserName = lpUserName 'MsgBox "The person logged on this machine
is: " & lpUserName

End Function


However, this does not work in MS Access 2000.

The MS Access new function, CurrentUser() works, but it only provides the MS
Access User Name (Admin) and I do not have security and user groups enabled
in my MS Access program. I would like to still get the network user login
name.

Any ideas ?
Many thanks,
J.
 
J

John Nurick

Hi John,

There's no reason why that should work in Access 97 and fail in Access
2000. It certainly worked fine when I pasted it into Access 2002.

What error are you getting and at what line? What versions of Windows
are involved?
 

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