Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to extract current NT user in Access 2003

Posted on 2007-11-29
7
Medium Priority
?
200 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

609 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