View Full Version : Help - returning windows logon name


fraser_lindsay
01-14-2009, 09:51 AM
Hi,

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

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

pbaldy
01-14-2009, 09:53 AM
Make sure the module does not have the same name as the function.

fraser_lindsay
01-14-2009, 09:24 PM
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.

boblarson
01-14-2009, 09:29 PM
This part must go in a STANDARD MODULE, not a form module and it goes just after the

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:

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

=fOSUserName()

For the text box

fraser_lindsay
01-14-2009, 10:05 PM
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.

fraser_lindsay
01-14-2009, 10:19 PM
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!

boblarson
01-15-2009, 08:17 AM
Not sure why it wouldn't be working. Can you post the database?
How to upload a database to the forums (http://www.access-programmers.co.uk/forums/showthread.php?t=140587)

jezzah
12-11-2009, 03:36 AM
Hi,

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

Thanks

pbaldy
12-11-2009, 09:56 AM
Use a textbox rather than a label.

jezzah
12-12-2009, 06:59 AM
drr- many thanks for the help. Cheers :)

pbaldy
12-12-2009, 08:22 AM
No problemo, happy to help.

pooqster
12-15-2009, 01:11 PM
Unless I've missed something, could you not just use: environ("username") to do same thing with much less effort?

dbDamo
12-16-2009, 04:37 AM
Thats what I use - much simpler