Form to Load automatically Showing Users Records

Sharkiness

Registered User.
Local time
Today, 12:23
Joined
Sep 25, 2008
Messages
31
Morning All,

I have a form that users access where they can deal with their own records. Currently the form shows all records for all users.

I am trying to use code that will retrieve the users logon ID and match this to their name in a table. I want the form to use this to automatically load the details for their records only.

Any help would be great.

Code:
Option Explicit
'API Declaration
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
'Form level variable
Private validEntry As Boolean

This is the first piece of code. I now need a form load code that will perform the task I require. I have come up with this but it will only change the name of the owner of the record to my own.

Code:
Private Sub Form_Load()
    'Variables
    Dim userPID As String, userName As String
    Dim objRSet As Recordset
 
    'Get user PID
    userPID = String(100, Chr$(0))
    GetUserName userPID, 100
    userPID = Left$(userPID, InStr(userPID, Chr$(0)) - 1)
 
    'Get user name
    Set objRSet = CurrentDb.OpenRecordset("tblRecordOwner", dbOpenTable)
    Do While Not objRSet.EOF
        If objRSet.Fields("PID") = userPID Then
            userName = objRSet.Fields("RecordOwner")
            Exit Do
        End If
        objRSet.MoveNext
    Loop
 
    'Enter Name
    RecordOwner.SetFocus
    RecordOwner.Text = userName
 
    'Clean up
    Set objRSet = Nothing
End Sub
 
I would expect something like this after getting the logged in user:

Me.RecordSource = "SELECT * FROM TableName WHERE PID = '" & UserName & "'"
 

Users who are viewing this thread

Back
Top Bottom