Access 2007 Runtime call of Environment Variables in 2008 R2 Server RDS

Posted on 2014-07-18
Last Modified: 2014-07-24
We run an access application which we compile to a 2007 runtime file for an application on our production floor.  The application pulls the username from the environment variable and uses that username to pull the parameters of the station from a SQL Database.

We run the application on a NLB group of 2008 R2 Remote Desktop Servers.  So, a thin client on the floor connects, logs in as the username of that specific station, the environment variable for username and computername are read, and the station proceeds to load its particulars.

Well, we have a problem periodically where one of the 4 servers, when stations load, they cant get the username, and as such the program cannot load completely.   We end up moving stations off that server, rebooting the server and then moving them back on.  Any stations currently active though work fine, it is only on a fresh login that the stations have issues.  

Further, if we move the session from Server A to Server B for example, it works.  Move it back to A and it doesn't work until a Reboot occurs.

So, my question is:  we use the following two calls for collecting %username% and %Computername%


Is there a better solution for pulling reliably the username of the logged in user, and the server name that the station is connected to?

Keeping in mind, this is a remote desktop server environment.  It needs to pull the logged in username of that session, not of the console or anything like that.

Any suggestions would be greatly appreciated.

Question by:TheMetalicOne
    LVL 56

    Accepted Solution

    Paste the code below into a module and then call WhoAmI(True) to return the user name or (false) to return the computer name.


    Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
    Private Declare Function GetUserNameA Lib "advapi32.dll" (ByVal lpBuffer As String, nSize As Long) As Long

    Public Function WhoAmI(bReturnUserName As Boolean) As String

            ' Function returns either user name or computer name

            Dim strName As String * 255

    10      If bReturnUserName = True Then
    20        GetUserNameA strName, Len(strName)
    30      Else
    40        GetComputerNameA strName, Len(strName)
    50      End If

    60      WhoAmI = left$(strName, InStr(strName, vbNullChar) - 1)

    End Function
    LVL 5

    Author Closing Comment

    Thanks, we used chunks of the code and accomplished our goals.  Much appreciated!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    This article is a continuation or rather an extension from Cascading Combos ( and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
    Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
    Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
    Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now