Solved

How to extract current NT user in Access 2003

Posted on 2007-11-29
7
190 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 …

762 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

19 Experts available now in Live!

Get 1:1 Help Now