fOSUsername (1 Viewer)

garywood84

Registered User.
Local time
Today, 20:24
Joined
Apr 12, 2006
Messages
168
I want a field in my table to list the name of the person who created the record. I've been trying to do this using the fOSUsername function, by copying and pasting that code into a module and then setting the default value of the field to =fOSUsername(). But, this isn't working because I get an error that the function is unknown when I try to save the table. I've spent ages trawling forums looking for an answer but can't make it work.

Can anyone help me, possibly by posting a sample database with the module and a table that has a default value based upon calling the function? I think from that I'd be able to make it work in my own database.

Thanks,

Gary
 

rainman89

I cant find the any key..
Local time
Today, 15:24
Joined
Feb 12, 2007
Messages
3,015
Are you using the windows username of the user? You can use
=environ("username")
 

LPurvis

AWF VIP
Local time
Today, 20:24
Joined
Jun 16, 2008
Messages
1,269
fOSUsername as in: http://www.mvps.org/access/api/api0008.htm
??

Expressions used in Table Definitions are confined by the limitations of the native Jet expression service - not that enhanced by Access. (It's a necessary safeguard - build a Jet DB and access it externally without Access to resolve the expressions and you'll be unable to add any rows at all! :-s)

A UDF such as this isn't included (naturally).
Even Eval won't help you as it's not a shared function - it's an Access Application method.
Assign the default at the form level rather than the table at runtime. (Or set the default of the table at runtime to be the string resolved value of the user name - but that's be crazy in a multi-user implementation).

Cheers.
 

garywood84

Registered User.
Local time
Today, 20:24
Joined
Apr 12, 2006
Messages
168
Thanks for all your replies.

I moved the default value to the form control instead of the table field, and now it works :)

Gary
 

garywood84

Registered User.
Local time
Today, 20:24
Joined
Apr 12, 2006
Messages
168
Yeah, I spaced on that one. I wasn't even thinking about where you were trying to use it. My bad.

Not at all - thanks for your help, both here and in my other thread about validation! :)
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:24
Joined
Jul 9, 2003
Messages
16,272
I've Updated Dev Ashish's fOSUserName() Code:-

To 64 Bit

Original HERE:-
http://access.mvps.org/access/api/api0008.htm
fOSUserName()

Found HERE:-
http://stackoverflow.com/questions/16847583/how-to-retrieve-windows-userid-in-vb-for-64-bit-access-2013

Code:
Option Compare Database
Option Explicit

'OLD:- Private Declare Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As LongPtr) As Long
'-----------------------------------------------------------------------------------------
' This code is used to retrieve the network user name by accessing the API apiGetUserName.
' Created by: Unknown (Found on Dev Ashish web site http://home.att.net/~dashish/api)
' This code has not been altered in anyway.
' Added to database: 27 Dec 1999
' Added by: Richard Rensel
'-----------------------------------------------------------------------------------------

Function fOSUserName() As String
On Error GoTo fOSUserName_Err

    Dim lngLen As LongPtr, lngX As LongPtr
    Dim strUserName As String
    
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, CInt(lngLen))
        
    If lngX <> 0 Then
        fOSUserName = Left$(strUserName, CInt(lngLen) - 1)
    Else
        fOSUserName = ""
    End If
    
  
fOSUserName_Exit:
  Exit Function
  
fOSUserName_Err:
  MsgBox Error$
  Resume fOSUserName_Exit
End Function
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:24
Joined
Jan 20, 2009
Messages
12,851
The user name can generally be found with a single line.

CreateObject("wscript.network").UserName
 

Users who are viewing this thread

Top Bottom