Help - returning windows logon name

fraser_lindsay

Access wannabe
Local time
Today, 00:41
Joined
Sep 7, 2005
Messages
218
Hi,

I have made a module (fOSUserName) using this code from my searches:

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



On my welcome screen I have created an unbound text box (txtUserName) and I have set the control source to = fOSUserName()

However, I just get '#Name' displaying in my text box.

I can't see what I'm doing wrong and Google generally keeps giving the same code and no additional explanation on usage.

Can anyone help me out please?
 
Make sure the module does not have the same name as the function.
 
Ok, I changed the module name to 'windowsusername' and then I changed my text box control source to =windowsusername().

However, I still only get '#Name' returned in my text box.


Also, should the code in the module have a line through it after this line:


Code:
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As LongWhen I paste it in it automatically puts this break between sections of code.
 
This part must go in a STANDARD MODULE, not a form module and it goes just after the
Code:
Option Compare Database
Option Explicit

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Then this gets pasted below in the module:
Code:
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

And to call it you use
Code:
=fOSUserName()
For the text box
 
Bob,

Many thanks, that's it working now. I already had the standard module and the code in it but I appeared to be missing this part:

Option Compare Database
Option Explicit


Thanks again.
 
Bob,

Sorry, me again. Immediately after fixing that problem above, I closed the DB, copied it and opened the new version to make further major changes.

Now my text box doesn't work. I didn't change anything else, the code looks the same as I left it in the standard module.

Why would that happen?

Curiously, if I know open the previous copy it does the same, but it was definitely working. I saw my Windows user name and I realigned the text in the box to left!
 
Hi,

I too am having the same issues and have attached my access database. Help would be much appreciated.

Thanks
 

Attachments

  • username displayed.zip
    username displayed.zip
    19.4 KB · Views: 126
  • username.gif
    username.gif
    37.7 KB · Views: 124
Use a textbox rather than a label.
 
No problemo, happy to help.
 
Unless I've missed something, could you not just use:
Code:
environ("username")
to do same thing with much less effort?
 
yes,

environ("username")

works as far as I know, everytime. There is no reason to do anything else.

Assign it to a string, then use a DLookup to see if that user has access to the said form.
 
yes,

environ("username")

works as far as I know, everytime.
Well, not quite every time. You may need to use

VBA.Environ("username")

in some environments.

There is no reason to do anything else.
Well, technically that isn't true. Someone who knows how, can change the Environment variable for a session and go about things as if they were a different user. The fOSUserName code makes it so that security hole is closed.

But unless you absolutely need to know, with 100% certainty, who did what was accurately recorded, you can use the Environ("username") just fine. But if there was, for example, a regulatory reason for maintaining the user info, you would be at risk if you used the Environ("username") for your capture.
 

Users who are viewing this thread

Back
Top Bottom