Solved

How to extract current NT user in Access 2003

Posted on 2007-11-29
7
191 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 119

Expert Comment

by:Rey Obrero
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
ID: 20384935

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now