Solved

Is there an alternative method to using  Environ("UserName") with Microsoft access

Posted on 2010-11-25
12
748 Views
Last Modified: 2012-05-10
I have been using the  Environ("UserName") funcion and it works great.  It retuns the username of the current user logged into the system and I can use that name to perform different tasks.

Problem:  I built an application for an organization that categorizes its users into different security classes.  Power Users, laptop users, and desktop users.  

The application works fine for the Power users and laptop users, but falls into error when desktop users are using the application.

What I found is that the deskstop users are restricted from accessing their C: drive and desktop and can only access the shared folders on the network and applications installed on the PC.

Is there another funciton or method I can use to capture the user name of a user with desktop privilages?
0
Comment
Question by:RDLFC
  • 6
  • 4
  • 2
12 Comments
 
LVL 7

Expert Comment

by:RemRemRem
ID: 34213124
This might do the trick:

Public Function funWinUser() As String
    ' Comments  : Retrieves the name of the user logged into Windows
    
    On Error GoTo gtErr
    '
    Dim lngLen As Long
    Dim strBuf As String
    
    Const MaxUserName As Integer = 255
    
    strBuf = Space$(MaxUserName)
    
    lngLen = MaxUserName
    
    If CBool(TSB_API_GetUserName(strBuf, lngLen)) Then
        funWinUser = Left(strBuf, lngLen - 1)
    Else
        funWinUser = ""
    End If
    
gtExit:
    Exit Function
    
gtErr:
    MsgBox Err
    GoTo gtExit
    
End Function

Open in new window


-Rachel
0
 
LVL 7

Expert Comment

by:RemRemRem
ID: 34213131
Whups, you'll need the declaration:

' Objects for GetUserName_TSB()
Declare Function TSB_API_GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Open in new window

0
 
LVL 17

Expert Comment

by:JezWalters
ID: 34213147
What exactly do you mean by "desktop privileges"?
0
 

Author Comment

by:RDLFC
ID: 34213189
RemRemRem i will check out your solution later.

JezWalters by desktop privileges I mean the user is restricted because of network security reasons from accessing their C: drive.
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 34213204
No offense to RemRemRem, but I reckon you'll find her solution returns the same results as your Environ() approach.

Have you thought of establishing desktop privileges by trying to write a temporary file to the C: drive instead?
0
 

Author Comment

by:RDLFC
ID: 34213219
No I have to work around the company's security envirionment.  I will not be able to alter any user privialges.  

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 17

Expert Comment

by:JezWalters
ID: 34213250
I think you're misunderstanding my suggestion - you can determine the user's privileges by attempting to write a file to the C: drive.

If the attempt fails the user DOESN'T have desktop privileges.  If the attempt succeeds, the user DOES have desktop privileges and you can then delete the temporary file.
0
 

Author Comment

by:RDLFC
ID: 34213265
okay.  I can use that.  Do you have code for me to do that via VBA?
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 34213273
... please hold the line ...!  :-)
0
 
LVL 17

Accepted Solution

by:
JezWalters earned 500 total points
ID: 34213288
OK, how about something like this?
Option Explicit

Option Compare Database



Public Function DesktopPrivileges() As Boolean



    ' Declare constants

    Const TEST_FILE As String = "C:\Temp\TestFile.txt"



    ' Declare variables

    Dim intTestFile As Integer



    ' Open test file

    intTestFile = FreeFile

    On Error GoTo OpenError:

    Open TEST_FILE For Output As intTestFile

    DesktopPrivileges = True



    ' Close & delete test file

    Close intTestFile

    Kill TEST_FILE



    ' Exit

    Exit Function



' Handle open error

OpenError:



    DesktopPrivileges = False



End Function

Open in new window

0
 

Author Closing Comment

by:RDLFC
ID: 34213572
I can use your solution to accomplish what I am interested in doing.
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 34216097
Only a 'B' grade?  What didn't work?
0

Featured Post

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.

Question has a verified solution.

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

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 originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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 …
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…

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

18 Experts available now in Live!

Get 1:1 Help Now