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

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?
LVL 1
WiretwisterzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Dim sEn As String
sEn = Environ("username")

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

mx
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
You need to reference the Field name in the filter, not the text box.

mx
0
Jim P.Commented:
Where are you putting that? And can we see the surrounding code?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

WiretwisterzAuthor 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

0
Rey Obrero (Capricorn1)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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WiretwisterzAuthor 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,
0
Rey Obrero (Capricorn1)Commented:
what is the recordsource of the form you are trying to filter?
post the field names here
0
Jim P.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.
0
WiretwisterzAuthor Commented:
Ah, it's working now.  Got it, was of course a Spelling issue =\

Thanks everyone for your help.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.