Reading the Windows username

It's really useful to be know who is logged into your Access database but sometimes you don't want the overhead of forcing your users through a login and password at startup. The answer is to retrieve the user's Windows identity and Access makes this very easy for us with the VBA function Environ().

Environ() in fact allows us to retrieve a variety of settings from the Windows environment but the most useful of these is the username:

'-- Get the userID and allow the developer to add and delete records on this form.
strUserID = Environ("UserName")
If strUserID = "franklin" then
  Me.AllowDeletions = True
  Me.AllowAdditions = True
Else
   '-- Do nothing - leave the settings as they were at Design Time.
Endif

Other environment variables

Three other useful calls are:

'-- The computername
strComputerID = Environ("ComputerName")
'-- The Windows temp folder
strTempFolder = Environ("Temp")
'-- Also the Windows temp folder
strTmpFolder = Environ("Tmp")

If you are curious, this snippet of VBA code will list the names and values available from the Windows environment:

i = 1
Do Until Environ(i) = ""
   Debug.Print Environ(i)
   i = i + 1
Loop

Most of these values are too obscure to be useful but if you ever need to know whether your user has a two-core or four-core core processor then the Environ() function can tell you.

Security Warning

It is possible that a knowledgeable user might be able to falsify the user name in the environment variables. There is a more robust alternative technique using API calls described on Allen Browne's Access site.

Related Items

FoxPro 2.6 under Windows 7

Running FoxPro 2.6 on a Windows 7 machine.

Read More

Access 2000 under Windows 7

Running old and new versions of Access on the same machine.

Read More

Visual FoxPro under Windows 8

Visual FoxPro 9 running under Windows 8.

Read More

Exporting files to the desktop

Saving files on the Windows desktop with FoxPro.

Read More

Wrapping the call to MsgBox

Making the MsgBox function easier to use in Microsoft Access

Read More