How to record the OS username

flester

New member
Local time
Today, 09:22
Joined
Aug 26, 2002
Messages
8
How do you get Access to pick up th OS username currently logged in to the database? For example I'm logged on to the PC as FLESTER and I wnat that recorded in the dB.
 
There are a few ways to go about this but the best I have found:

Paste this into a new module:

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


Then, use this where you want the log in name to be recorded:

=FOSUserName()
 
Use the Environ("UserName") function. Search the forum for the Environ function for more information you can grab from the users computer.

Use the built-in CurrentUser() funtion to get the users Workgroup "user" name.

Just search the forum before posting and you will be amazed out how easy it is to answer your own questions.
 
Last edited:
further question...

I put this in and it works - puts the username into the textbox

txtUserName.Text = Environ$("USERNAME")

How would I get it to do this only for a NEW record?
 
flester said:
I put this in and it works - puts the username into the textbox

txtUserName.Text = Environ$("USERNAME")

How would I get it to do this only for a NEW record?
Put it in the design view of the table as the Default Value for that field.

But you can not use the Environ() function as the default value in an Access 2003 db.
 

Users who are viewing this thread

Back
Top Bottom