Using the Windows Username in an Access DB

F

fred

I've read a few posts that explain how to capture the windows username,
but I'm having trouble getting this into a form.

I've added the following code to a public module:

Option Compare Database

'******************** 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 **************************

When I try to user GetUserNameA or fOSUserName, I get a response of
#NAME.

Am I calling the code incorrectly?

Thanks,

Fred
 
D

Douglas J. Steele

What did you name the module? If you called it fOSUserName, rename it:
modules cannot be named the same as functions or subs.
 

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

Similar Threads

advapi32.dll 1
Applying Form Filter By UserName 3
User Name Stamp 3
User Logon Name 2
Update form field 2
Porting from 2003 to 2007 6
Update username 6
Retrieving and setting user logon name in a form 2

Top