We help IT Professionals succeed at work.

How to a make a form filter using Environ("username")?

Wiretwisterz
Wiretwisterz asked
on
I am trying to use:

"txtEmployeeName = '" & Environ("username") & "'"

in the Filter on a form so as to only display the records that have the same user name as the person launching the form.  I only want the user to be able to see their own records.

The above code is not working, what am I doing wrong?
Comment
Watch Question

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
Dim sEn As String
sEn = Environ("username")

Me.Filter = [EmployeeName] = CHr(34)  & sEn  & Chr(34)
Me.FilterOn = True

mx
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
You need to reference the Field name in the filter, not the text box.

mx
Most Valuable Expert 2014

Commented:
Where are you putting that? And can we see the surrounding code?

Author

Commented:
Please excuse my ignorance but I'm afraid I still don't quite understand and am unable to get it to work properly.  If I understand your code MX, I could put it on the Form OnLoad function and have it do the filtering from there without having to edit the Filter in the property box of the form.  The code currently looks like this.

The text box that contains the user name is called "txtEmployeeName".  I am unsure what you meant on your second comment:

"You need to reference the Field name in the filter, not the text box."

Please clarify whether or not I am able to put the code solely in the Form_Load() event and have it filter from there, or if I also need to input text into the Filter property box.

Thanks
Private Sub Form_Load()
 
Dim sEn As String
sEn = Environ("username")
 
Me.Filter = [EmployeeName] = Chr(34) & sEn & Chr(34)
Me.FilterOn = True
 
 
End Sub

Open in new window

CERTIFIED EXPERT
Top Expert 2016
Commented:


if you want to filter the records of the form using the environ("username")

me.filter="[UserName]='" & environ("username") & "'"
me.filteron=true


just change the field [username] to the actual name of field/control

Author

Commented:
Capricorn, I placed your code like this:

Private Sub Form_Load()
Me.Filter = "[txtEmployeeName] ='" & Environ("username") & "'"
Me.FilterOn = True
 End Sub

All that happens is when I open the form, a box pops up asking for the user name, I enter a username to filter by and it still doesn't filter, it displays all records.  Also, I'd like this to be done automatically without having them input a user name in a box when they open the form.

Thanks,
CERTIFIED EXPERT
Top Expert 2016

Commented:
what is the recordsource of the form you are trying to filter?
post the field names here
Most Valuable Expert 2014

Commented:
Environ("username") will return who is logged on the box such as "SmithJ" not "John Smith" or "Smith"

You need a column that is something like txtEmployeeLogon and contains SmithJ.

Author

Commented:
Ah, it's working now.  Got it, was of course a Spelling issue =\

Thanks everyone for your help.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.