问题描述
我为办公室创建了一个用于日常温度检查的表格。员工打开表单时,它使用以下命令显示该员工的网络用户ID:
Public Function GetUserName() As String
GetUserName = Environ("UserName")
因此他们不必登录。然后,员工为其温度选择一个“正常”或“高温”值,然后单击提交。我想查找实际的员工姓名并显示它,而不是网络ID。我有一个表格,其中包含网络ID和员工姓名的列。表单打开时,在我的表单上显示“雇员姓名”的最佳方法是什么?Data Entry Form
解决方法
您需要根据表中的UserID和索引(或您使用的字段名称)在表中查找记录。
Sub Test()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim TestVal As String
UserName = GetUserName()
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Employees") 'Adjust table name as necessary
rst.Index = "UserID" 'Name of field in table,must be indexed!
rst.Seek "=",UserName
If rst.NoMatch Then
MsgBox "User Name not Found in Users Table"
Else
MsgBox "Full Name: " & rst.Fields("EmployeeName")
End If
'*** Clean Up ***
rst.Close
Set rst = Nothing
Set dbs = Nothing
End Sub
注意:调整字段名称以适合您的表。您可以转换为函数并返回全名。