Environ$("USERNAME") and Access 2003

mshelley1

Carbo
Local time
Today, 09:45
Joined
Feb 12, 2006
Messages
45
[RESOLVED] Environ$("USERNAME") and Access 2003

How do I use the following as the default value of a textbox in Access2003?
Environ$("USERNAME")

Resolution:

Create Module and insert the following code:

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

Function fOSUserName() As String
On Error GoTo fOSUserName_Err

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 = ""
End If


fOSUserName_Exit:
Exit Function

fOSUserName_Err:
MsgBox Error$
Resume fOSUserName_Exit
End Function

Use this function as default value
=fOSUserName()
 
Last edited:
By putting it in the default value property (preceded by = of course).
 
What you say is true if you were using Access 2000 but this does not hold true for Access 2003, This function simply does not work at face value.

Thanks
 
I tested it in Access 2003 before I responded.
 
The application I am referring to was written a number of years age in access 2000, however when I ran it a machine that had access 2003 It does not work. In your opinion, do you think that it is because the application was written in Access 2000 and has not been upgraded? Perhaps the function only works in 2003 if it is written in 2003.

Thanks
 
I don't know, but I doubt it. Have you tried creating a new db in 2003 on that machine and seeing if it worked?
 
You can also drop the dollar sign and use Environ("username").
 
It's a security issue relating to XP SP2 I believe, the Environ function has been blocked as its deemed to be unsafe. I discovered this when the function worked on some PC's and not others, I couldn't find a solution to it.

I did find an article on it somewhere, but can't lay my hands on it at the moment.
 
I too have seen an article on this however I believe it is to do with Office 2003 not XP SP2, and something called Sandbox. I thought that it had been discussed on the Forum.
Try reading this

Brian
 

Users who are viewing this thread

Back
Top Bottom