Solved

How to extract current NT user in Access 2003

Posted on 2007-11-29
7
193 Views
Last Modified: 2012-08-14
Hi,

I've created a little program in Access 2003 and i now want to start building security for it. I decided to follow the concept of using the Windows NT log in to distinguish which user has got what rights.

How do i go about doing this? What's the code to extract the currently logged user. All users in my company used Windows XP.

Best Regards,
CV
0
Comment
Question by:connelvalentine
  • 3
  • 2
  • 2
7 Comments
 
LVL 18

Accepted Solution

by:
jmoss111 earned 500 total points
ID: 20373501
I use the below to grab user name from the system

Private Declare Function GetUserName Lib "ADVAPI32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public gUserNm AS String

Public Function GetCurrentUserName() As String
On Error GoTo Err_GetCurrentUserName
 Dim lpBuff As String * 25
 Dim ret As Long, Username As String
   ret = GetUserName(lpBuff, 25)
   Username = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
   GetCurrentUserName = Username & ""
   gUsrNm = GetCurrentUserName
 
   Forms.frmFindPayments.txtUsername = gUsrNm
   Exit_GetCurrentUserName:
    Exit Function

Err_GetCurrentUserName:
        MsgBox Err.Description
        Resume Exit_GetCurrentUserName
End Function
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20373574
you can also use

environ("username")
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 20373591
I use this line to display the user name in a form, you'll want to remove that:

Forms.frmFindPayments.txtUsername = gUsrNm
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 39

Expert Comment

by:stevbe
ID: 20384642
<environ("username")>
environ is easily spoofed and I have even seen systems where the username environ var does not even exist so that call will error out. I use the api call in a function quiote similar to what jmoss posted without the 25 char limiation and I also use a module level var to speed up repeat calls ...
    Private Declare Function GetUserNameAPI Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
 
    Private mstrUserName As String
 
 
Public Function GetUserName() As String
    Dim strUser As String
    Dim lngLen As Long
    Dim lngRet As Long
 
    'only do this if we have to
    If Len(mstrUserName) = 0 Then
 
        'build return parameters
        strUser = String$(255, vbNullChar)
        lngLen = 255
 
        'call api
        lngRet = GetUserNameAPI(strUser, lngLen)
 
        If lngRet = 0 Then
            'return of 0 indicates failure
            strUser = vbNullString
        Else
            'non-zero return indicates success, strip trailing null character
            strUser = Left$(strUser, lngLen - 1)
        End If
 
        'assign return value to private var
        mstrUserName = strUser
    End If
 
    'assign return value
    GetUserName = mstrUserName
 
End Function

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20384711
stevbe,
have not experienced that.... and have not read anything like that.
0
 
LVL 39

Expert Comment

by:stevbe
ID: 20384795
I have experienced it first hand a handful of times over the last 5 years while I have been replacing environ with api calls in all the apps I am responsible for.

if you google ...

undefined function environ(username)

you get lots of results ... pleanty of reading :-)

Steve
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20384935

those problems could have been caused by defective libraries, broken access or some gremlins in the system...
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question