Auto fill user name

  • Thread starter Thread starter wishfulthinking
  • Start date Start date
W

wishfulthinking

I have a form set up to track what the calls we recieve are about. There are
15ish people that will be using the form and I was wondering if there is a
way to have the form auto fill the name of the user creating the new record,
the way it is set up to auto fill the date and time the new record is
created. This would prevent anyone from accidently clicking on the wrong
name if set it up as a list box. Please advise if this can be done and how.
 
For the username you could either pull the Network Username and use it
directly, or use a table to cross-reference the Network Username with the
user's real name. One way or another you can automate the whole thing by
using the Network username and the code for that can be found at:

http://www.mvps.org/access/api/api0008.htm

As for the date/time when the record is created. In your form design, set
the default value for the date/time control to =date()
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
 
I figured it had something to do with pulling the Network Username but was
unsure how. So your advice sounds great, however my limited Access skills
leave me confused as to how to do anything with a Module. Is it really as
simple as copy and pasting a code? I think I am missing some steps to make
it useful to the form I am using.
 
Your help with the date/time stamp works great but I don't understand what to
do with your tip on the user name. I figured it had something to do with
pulling the Network Username but was unsure how. So your advice sounds
great, however my limited Access skills leave me confused as to how to do
anything with a Module. Is it really as simple as copy and pasting a code?
I think I am missing some steps to make it useful to the form I am using.
 
It is pretty much that easy.

Open the VBE by pressing Alt-F11
Insert a new module and paste in the API Code from the link I supplied
Open your form in design view and in the default value for the name control
enter =fOSUserName()
Close and save your form and the reopen it and see the result
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
 
Daniel, I have tried to follow your directions below, however the field on my form shows #Name?. I have bound the text field on the form to a field in the table which is where I would like the autostamp of the record. It would preferable to only have the timestamp visible in the table and not on the form. Will I need to create a new table for users in order for this function to work?
I have a form set up to track what the calls we recieve are about. There are
15ish people that will be using the form and I was wondering if there is a
way to have the form auto fill the name of the user creating the new record,
the way it is set up to auto fill the date and time the new record is
created. This would prevent anyone from accidently clicking on the wrong
name if set it up as a list box. Please advise if this can be done and how.
On Monday, December 07, 2009 4:50 PM Daniel Pineault wrote:
For the username you could either pull the Network Username and use it
directly, or use a table to cross-reference the Network Username with the
user's real name. One way or another you can automate the whole thing by
using the Network username and the code for that can be found at:

http://www.mvps.org/access/api/api0008.htm

As for the date/time when the record is created. In your form design, set
the default value for the date/time control to =date()
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.



"wishfulthinking" wrote:
On Monday, December 07, 2009 6:24 PM KenSheridan via AccessMonster.com wrote:
However you obtain the user name, and the API call suggested by Daniel is the
obvious solution, you should also create a Users table with UserName as its
primary key if you do not already have one, and create an enforced
relationship between it and the 'calls' table. That way the integrity of the
data is protected. Even if automating the insertion of a value into the
UserName column there would otherwise be nothing to prevent a value being
subsequently changed to that of a non-existent user. This might sound
unlikely, but Murphy's Law states not.

If you should decide not to use the unique login names, but employees' real
names then, as names can legitimately be duplicated and therefore are
unsuitable as keys, the Users table should have a numeric UserID as its key,
FirstName and LastName columns and a UserName column which corresponds to the
login name, along with other columns which identify the user such as their
job title etc., i.e. it would be more of a general Employees table. This
then caters for having two users of the same name (I worked once with two
Maggie Taylors). The calls table should then have a numeric UserID column as
a foreign key referencing the key of Users.

You can then put the following code in the calls form's Open event procedure
to set the DefaultValue property of its UserID control, which you would make a
hidden one as there is no point showing the arbitrary numeric value:

Dim strCriteria As String

strCriteria = "UserName = """ & fOSUserName() & """"

Me.UserID.DefaultValue = """" & DLookup("UserID", "Users", strCriteria) &
""""

You could then show the user name in the form in a control, either an unbound
one with a ControlSource:

=DLookup("(Firstname + "" "") & LastName", "Users", "UserID= " & [UserID)

or in a bound control if you base the form on a query which joins the calls
table to the users table. If doing the latter set the control's Enabled
property to False (No) and its Locked property to True (Yes).

Ken Sheridan
Stafford, England

wishfulthinking wrote:
 
Dear,

Go Simple,

if you are expecting the user name to populate automatic in the field on the form,
create the field on form and go to the properties of the same by right click, also create the field of name in the table.
In the property window in control source add the field name by selecting dropdown, then go to default value and call for =FOSUserName(), then go to the form property in Before Update and add [Event Procedure] and add the API Code as mentioned below,

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
' 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
'******************** Code End **************************

Private Sub Form_BeforeUpdate(Cancel As Integer)
End Sub

Close the form and reopen the same and check it works
feel free to revert pl..

sunil
 
Back
Top