Applying Form Filter By UserName

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The code below, supplied by Dev Ashish, correctly produces the username from
the fOSUserName() call in the Form_Open event procedure, since the Msgbox()
statement displays the correct user name.

However, instead of filtering the form, it displays a dialog box with the
Heading “Enter Parameter Valueâ€. The username is shown in the “eyebrowâ€,
with the input field blank. If I type the username in, the filter is applied
correctly. If left blank, the form opens to a blank record.

Can someone help me determine how to apply the filter correctly?

Thank you.

Sprinks

'******************* Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
'******************** Code End **************************

Private Sub Form_Open(Cancel As Integer)
Dim ctl As Control
Application.Echo False

' Reset Labels & initialize totals
Call ResetLabels
Call UpdateTotals


' Filter By UserName
Me.Filter = "[UserName]= " & fOSUserName()
Me.FilterOn = True

' Move to subform
Me!TimeSheetDetail.SetFocus
Application.Echo True

End Sub
 
Okay, so you've got the function that returns the user name, and, according
to you, it works properly.

How are you attempting to use it?
 
Douglas,

I'm trying to use it in the Form Open event to filter the form by the
username. See the code at the end of my original post.

Sprinks
 
Because the User Name is text, you need quotes around its value:

Me.Filter = "[UserName]= " & Chr$(34) & fOSUserName() & Chr$(34)

or

Me.Filter = "[UserName]= """ & fOSUserName() & """"

or

Me.Filter = "[UserName]= '" & fOSUserName() & "'"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Using the Windows Username in an Access DB 2
advapi32.dll 1
User Name Stamp 3
Update form field 2
User Logon Name 2
Update username 6
Porting from 2003 to 2007 6
Retrieving and setting user logon name in a form 2

Back
Top