Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / November 2005

Tip: Looking for answers? Try searching our database.

Default User ID

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nick hfrupn - 20 Nov 2005 00:37 GMT
I have a table, tbSamples, with a field UserID. when a new recorded is added
I would like to default the User ID of the person loged on, adding the record.
I have tried using; =CurrentUser() , this only returned Admin regardless of  
who was logged on.
It was suggested that I try this web site;
http://www.mvps.org/access/api/api0008.htm
I created a module, modUserID and pasted in the code from below.
This is where I come un-stuck. How do I activate this module to default the
User ID into the field?
I am using Windows and Access 2000.
Any help appreciated.

Nick

'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
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 **************************
tina - 20 Nov 2005 01:13 GMT
you pasted the function into a public module. now you just have to call the
function. to set the default value of a control in a form, add the following
expression to the control's DefaultValue property, as

=fOSUserName()

if you want to set the value of the UserID field when the record is added,
add the following code to the form's BeforeUpdate event procedure, as

Private Sub Form_BeforeUpdate(Cancel As Integer)

   Me!UserID = fOSUserName

End Sub

hth

> I have a table, tbSamples, with a field UserID. when a new recorded is added
> I would like to default the User ID of the person loged on, adding the record.
[quoted text clipped - 37 lines]
> End Function
> '******************** Code End **************************
tina - 20 Nov 2005 01:16 GMT
oops! close but not quite. the following is better:

if you want to set the value of the UserID field when the record is added,
add the following code to the form's BeforeUpdate event procedure, as

Private Sub Form_BeforeUpdate(Cancel As Integer)

   If IsNull(Me.UserID) Then
       Me!UserID = fOSUserName
   End If

End Sub

hth

> you pasted the function into a public module. now you just have to call the
> function. to set the default value of a control in a form, add the following
[quoted text clipped - 58 lines]
> > End Function
> > '******************** Code End **************************
Nick hfrupn - 20 Nov 2005 04:38 GMT
Thanks very much Tina.
Your directions where very clear and precise. I didn’t have any problems
following your instructions, which makes it easy for someone like myself with
a basic understanding of Access.

Thanks again
Nick

> oops! close but not quite. the following is better:
>
[quoted text clipped - 75 lines]
> > > End Function
> > > '******************** Code End **************************
tina - 20 Nov 2005 05:50 GMT
you're welcome  :)

> Thanks very much Tina.
> Your directions where very clear and precise. I didn't have any problems
[quoted text clipped - 83 lines]
> > > > End Function
> > > > '******************** Code End **************************
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.