Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Module to get user name 2

Status
Not open for further replies.

Joeclueless

Technical User
Jan 30, 2002
116
US
Hi,

please refer to:
thread700-448341


I started this thread in the Access tables and relationships forum, thinking that the default value of a field could return the username to the field. Then a respondant informed me that I should use a module to accomplish this.

KenReay (Programmer) offered me this code:

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 = &quot;&quot;
End If
End Function

I am trying to use this code as a module. The way I am trying to accomplish this is by creating a new module, pasting the code into it, then save the module as Module1. Then in the form that the table is the record source, I am putting in the before update event of the form:

Me!User = fOSUserName()

Now, when I open the form the field that the username should appear in is blank. When I type in something and try to leave the form, the error:

UserTracker! can't find the Macro 'Me!User=fOSUserName().' etc...etc...

So, how can I make this work?? Any ideas are appreciated.
Thanks again!

Joe
 
Hi Joe!

You need to use the word Public at the front of your function declaration.

Public Function fOSUserName() As String

hth
Jeff Bridgham
bridgham@purdue.edu
 
Well Jeff,

I tried the Public declaration that you suggested and I'm still getting the cannot find the macro error.

Do I need to make a macro that runs the module? Thanks again!

Joe
 
Hi again Joe!

Where did you put the Me!User = fOSUserName()? This error makes it sound like you typed it directly into the Before Update line on the Event tab of the form's property sheet. If you did, then you need to click into the Before Update line and then click on the drop down arrow. Select [Event Procedure]. An elipse (...) will appear to the side of the box, click on that. This will take you to the code window with the following:

Private Sub Form_BeforeUpdate(Cancel As Integer)
End Sub

Your code, Me!User = fOSUserName(), needs to go between those two lines.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Thanks! that works!

The only thing I did'nt thuink of is that now the username changes if you go to that record....

How could it retain the original function result?? Thanks again!

Joe
 
Hi Joe!

Use this code in the Before Update event procedure:

If Nz(Len(Me!User), 0) = 0 Then
Me!User = fOSUserName()
End If

This will add the current user's name only if the field User is Null or an empty string.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top