[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

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?
0
Wiretwisterz
Asked:
Wiretwisterz
  • 3
  • 2
  • 2
  • +1
1 Solution
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Dim sEn As String
sEn = Environ("username")

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

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now