Retrieving and setting user logon name in a form

  • Thread starter Thread starter Gordon
  • Start date Start date
G

Gordon

On my form I have fields for "input by" and "updated by" which I want
to populate with the user's system network logon name. I am using a
function obtained from Dev Ashish' website:

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

I set the default value for "input by" to be =fOSUserName() and that
works fine but I can't get the "updated by" field on the form to work.

I have tried (in the beforeupdate event):

If Me.Dirty = True Then
Me!fldUpdatedBy = fOSUserName
Me!fldDateLastUpdated = Now()
End If

....but that doesn't work.

Can anyone help?

Gordon
 
You don't need to test if me.dirty because it only fires when a change to the
data has occured.

The code works fine when I tested it rapidly using the Form_BeforeUpdate
event. Do you get any errors? If so, which one(s)? Are you sure of the
naming of the form controls used in your code?
 
You don't need to test if me.dirty because it only fires when a change tothe
data has occured.

The code works fine when I tested it rapidly using the Form_BeforeUpdate
event.  Do you get any errors?  If so, which one(s)?  Are you sure of the
naming of the form controls used in your code?
--
Daniel Pineaulthttp://www.cardaconsultants.com/
For Access Tips and Examples:http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.













- Show quoted text -
Thanks for your response Daniel. I've dropped the me.dirty code and
double checked all the control names (including renaming them as
below) but it still doesn't work - no error messages - just doesn't
populate the form fields. Code now reads:

Me!txtUpdatedBy = fOSUserName
Me!txtDateLastUpdated = Now()

Curiously, something I didn't notice before, when I include the first
line above, the updating of the LastUpdated field doesn't work either
- again, no error it just doesn't populate. When I REM out the
UpdatedBy line, the LastUpdated field does get populated. Is there
some kind of clash at work here?

Gordon
 
Back
Top