smtazulislam
Member
- Local time
- Today, 04:49
- Joined
- Mar 27, 2020
- Messages
- 806
Hi, any help will appreciate.
I have multiple users. I would like to do, if user logon and open the report then his/her name is stamped in the report footer. I'm tried it many ways to resolve this problem. Here is show you..
I tried it by TempVars function in Open event.
in form use it:
Tried it too
However, I had tried by Data source. no sense how to add each table sql with Users table.
I have multiple users. I would like to do, if user logon and open the report then his/her name is stamped in the report footer. I'm tried it many ways to resolve this problem. Here is show you..
I tried it by TempVars function in Open event.
Code:
Private Sub Report_Open(Cancel As Integer)
Dim strSQL As String
Dim currentUserName As String
' Get the current user's name from TempVars
currentUserName = TempVars!UserName
' Build the SQL query
strSQL = "SELECT * FROM Users WHERE UserName='" & currentUserName & "';"
' Apply the SQL query to the report's RecordSource
Me.RecordSource = strSQL
End Sub
Code:
Private Sub btnPrint_Click()
Dim currentUserName As String
currentUserName = GetCurrentUserName()
' Set TempVar with the current username
TempVars.Add "UserName", currentUserName
' Open the report
End Sub
Code:
Public Function GetCurrentUserName(userID As Long) As String
Dim currentUserName As String
Dim rs As DAO.Recordset
' Get the current user's name from user table
Set rs = CurrentDb.OpenRecordset("SELECT UserName FROM Users WHERE UserID = " & userID)
If Not rs.EOF Then
currentUserName = rs("UserName")
Else
currentUserName = "Unknown"
End If
rs.Close
GetCurrentUserName = currentUserName
End Function