AllExperts > Using MS Access 
Search      
Using MS Access
Volunteer
Answers to thousands of questions
 Home · More Using MS Access Questions · Answer Library  · Encyclopedia ·
More Using MS Access Answers
Question Library

Ask a question about Using MS Access
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Robert Austin
Expertise
I can answer questions relating to database design, User Interface design, Visual Basic and VBA coding and Reporting Design, as well as intermediate level SQL query statements.

Experience
I have been creating MS Access databases since version 2.0, for almost 20 years.

Education/Credentials
Most of my technical training has been on-the-job, but I have almost completd my Bachelors of Science in Technology Management at DeVry University.

 
   

You are here:  Experts > Computing/Technology > Business Software > Using MS Access > Windows User Name in Access 2007

Using MS Access - Windows User Name in Access 2007


Expert: Robert Austin - 5/5/2008

Question
I have a field that I want to auto-populate with the Windows username of the current user when they create a new record.  The field is "UserName" with in a form.  there are no users for this database as they are all opening the DB with the Access 2007 runtime if I use Currentuser() I always get Admin (obviously)  how can I get the windows username?

Answer
Create a module wtih the following code (or add to an existing one).


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

Then, to get the user name, you could fill a variable with it,

txtUserNamestring = fOSUserName

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.