Solved

How to extract current NT user in Access 2003

Posted on 2007-11-29
7
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

717 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