Question Username field not filling

skwilliams

Registered User.
Local time
Today, 12:07
Joined
Jan 18, 2002
Messages
516
I have a form in MS Access with a control that prefills on startup with the current username from Windows.

I have a module.
Code:
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
      
      On Error GoTo Err_fOSUserName
      
      Dim lngLen As Long, lngX As Long
      Dim strUserName As String
10        strUserName = String$(254, 0)
20        lngLen = 255
30        lngX = apiGetUserName(strUserName, lngLen)
40        If (lngX > 0) Then
50            fOSUserName = Left$(strUserName, lngLen - 1)
60        Else
70            fOSUserName = vbNullString
80        End If

Exit_fOSUserName:
Exit Function

Err_fOSUserName:
MsgBox Err.Number & " - " & Err.DESCRIPTION
Resume Exit_fOSUserName

End Function

The control source for the form is a query related to an Employee table. I have the tblEmp.UserName criteria set to = fOSUserName().

Code:
SELECT tblEmp.Username, tblEmp.Customers, tblEmp.Quotes, tblEmp.IDLaborcosts, tblEmp.MgMachines, tblEmp.MgFoil, tblEmp.SecondProcess, tblEmp.IDIncidentals, tblEmp.StockList, tblEmp.DIncidentals, tblEmp.ColorType, tblEmp.CutParameters, tblEmp.Admin, tblEmp.PrimProcess, tblEmp.StockView
FROM tblEmp
WHERE (tblEmp.Username)=fOSUserName();

This works great.

The problem is on some PC's the form's username control populates and on other PC's it doesn't.

Tried Environ ("Username") as well with same result.

When checking the code in the immediate window of a problem machine the username populates. It just doesn't seem to like it in the query criteria.

Any ideas??
 

Users who are viewing this thread

Back
Top Bottom