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

Setting Environ$("UserName") for a form filter query


I am setting up a form that has a filter in place (im modifying something I found on the web and am not too familiar with how the filter works but...)

I already found out how to set the default form value to Environ$("UserName") so that the value is stored when a new record is created.

The filter property i am looking at should look like:

([Work Hours Extended].[UserName]=theUser)

How do I set "theUser" as a Environ$("UserName")?

Thank you!
  • 4
3 Solutions
Rey Obrero (Capricorn1)Commented:
create a function in a regular module

Funtion getUserName() as string
Exit Function

in your query call the function

([Work Hours Extended].[UserName]=getUserName())
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Minor point that you should be aware of; if your environment is not locked down (user can get to a command prompt), it's very easy to set the username to something else.

Instead, use the function below to return the login name from windows.


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
billg7Author Commented:
Ok, well I'm getting a syntax error. I tried yours above. First off, the word function was missing a 'c' :) no big deal, but I'm still getting a syntax error. I also tried changing the 'exit' to 'end' with no luck.

Also, I didn't have any modules in this, so I just set this up in the general declarations. I also tried creating a module which resulted in the same error.

I'm not at all worried about security and i want to keep this very very simple.

Here's what I've got:

Function theUser() As String
    theUser = Environ("UserName")
End Function

Thank you so much for the help!
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

billg7Author Commented:
Ok, for whatever reason it started working. Very strange. Anyway, thanks again for your help!
billg7Author Commented:
Just FYI for anyone else, this is what I ended up with in it's own module:

Public Function theUserName() As String

    theUserName = Environ$("UserName")

End Function
billg7Author Commented:
This is the working solution with proper syntax based on the experts feedback :).

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

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