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!
Who is Participating?
billg7Connect With a Mentor Author 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
Rey Obrero (Capricorn1)Connect With a Mentor 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)Connect With a Mentor President / OwnerCommented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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!
billg7Author Commented:
Ok, for whatever reason it started working. Very strange. Anyway, thanks again for your help!
billg7Author Commented:
This is the working solution with proper syntax based on the experts feedback :).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.