• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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

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.

1 Solution
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
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
TheMetalicOneAuthor Commented:
Thanks, we used chunks of the code and accomplished our goals.  Much appreciated!

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now