首页 > 解决方案 > 表单打开的 Microsoft Access 用户日志

问题描述

我有一个带有 sql 后端的 Microsoft 访问数据库。我需要成功打开表单的日期和时间的 Windows 用户日志。我更喜欢将数据记录到链接的 sql server 表中。有没有办法做到这一点?

标签: sqlvbams-access

解决方案


作为 Lee Macs 答案的附录:

出于安全原因,您应该使用

CreateObject("Wscript.Network").UserName

代替

Environ("username")

检索到当前登录的 Windows 用户,因为环境变量username很容易被覆盖(意味着有人可以假装不同的用户)。

或者,如果您愿意,也可以使用 Windows API:

Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetWindowsUserName() As String
    Dim buffer As String * 255

    Dim result As Long
    result = GetUserName(buffer, 255)

    If result > 0 Then _
        GetWindowsUserName = Left(buffer, InStr(buffer, Chr(0)) - 1)
End Function

推荐阅读