【问题标题】:GetCurrentUserName() is causing crash in MS Access 2010GetCurrentUserName() 导致 MS Access 2010 崩溃
【发布时间】:2016-10-27 21:49:36
【问题描述】:

我正在运行 Windows 7 专业版。我有一个 MS Access 前端到一个 MS Access 后端。在开始打开前端时打开的表单会导致应用崩溃。

代码如下:

Private Sub Form_Open(Cancel As Integer)
Dim strMyDir As String
Dim intPos As Integer
Dim rst As dao.Recordset
Dim strSQL As String
Dim rstWhatsNew As dao.Recordset

DoCmd.ShowToolbar "Database", acToolbarNo
DoCmd.ShowToolbar "Toolbox", acToolbarNo
DoCmd.ShowToolbar "Form View", acToolbarNo

If Application.GetOption("ShowWindowsInTaskbar") = -1 Then
    Application.SetOption "ShowWindowsInTaskbar", 0
End If


If DLookup("Locked", "luLockOut") <> 0 Then
    MsgBox "Database is being worked on.  Please try back in a couple minutes.", vbInformation, " "
    DoCmd.Quit
Else
    strSQL = "Select * From tblLastLogins Where UserName = '" & GetCurrentUserName() & "'"

这是我将错误追踪到的地方:GetCurrentUserName()

    Set rst = CurrentDb.OpenRecordset(strSQL)
        With rst
            If Not .EOF Then
                .Edit
                strSQL = "Select WhatsNewID From tblWhatsNew Where DateAdded >= #" & !LastLoginDate & "#"
                Set rstWhatsNew = CurrentDb.OpenRecordset(strSQL)
                    While Not rstWhatsNew.EOF
                        DoCmd.OpenForm "frmWhatsNew", , , , , acDialog, rstWhatsNew!WhatsNewID
                        rstWhatsNew.MoveNext
                    Wend
                    rstWhatsNew.Close
                Set rstWhatsNew = Nothing
            Else
                .AddNew
                !UserName = GetCurrentUserName()
            End If
            !LastLoginDate = Now()
            !IsLoggedIn = -1
            Me.txtLastLoginID = !LastLoginID
            .Update
            .Close
        End With
    Set rst = Nothing
    DoCmd.OpenForm "frmPrivacyNote"
    Debug.Print Me.txtLastLoginID
End If

我需要跟踪用户名,所以如果 GetCurrentUserName() 已过时,当前的语法是什么?

进一步跟进。我在 Bing 上找不到 GetCurrentUserName() 的数据,这是有充分理由的。它是 MOD 中的一个函数,所以我需要弄清楚为什么 MOD 没有被调用,或者出现故障。

经过进一步研究,我发现一个引用的 MDB 具有由我们的一个用户创建的另一个函数,这是导致此错误的原因。

目前这不是 MS Access 工作不正常的问题。这是用户创建的代码的问题。

【问题讨论】:

  • 游戏有模组,也许你的意思是附加组件?
  • 没有。 MOD 是 MS Access 中 MODULE 的缩写。这些是与表单的 VBA 代码没有直接关联的代码持有者。
  • 啊,这不是一个很常见的缩写(我认为)。如果您想分析它,请将代码添加到您的问题中(在评论中很难阅读)。必须同时声明 API 函数 GetUserName()。但实际上您可以删除所有这些并使用答案中发布的功能 - 不再需要使用 API 功能。
  • 我进一步追踪。我找到了一个需要注册的DLL。 ADVAPI32.DLL。不幸的是,当我尝试注册它时,我收到以下错误:“无法添加对特定文件的引用”。谷歌搜索该错误,我发现很多人都遇到过,但没有与我的问题直接相关(建议的解决方案不起作用)。
  • 你试过CreateObject("WScript.Network").UserName 吗?我感觉您正在尝试解决一个不再存在的问题,因为有更好的选择。

标签: ms-access-2010 windows-7-x64


【解决方案1】:

GetCurrentUserName() 不是由 Access 定义的,因此您应该查看(并发布)它的代码。

如果您要查找 Windows 用户名,请使用此函数:

Public Function GetUserName() As String
    ' GetUserName = Environ("USERNAME")
    ' Environ("USERNAME") is easily spoofed, see comment by HansUp
    GetUserName = CreateObject("WScript.Network").UserName
End Function

Source

【讨论】:

  • Function GetCurrentUserName() As String Dim strName As String Dim lngCharacters As Long Dim lngReturn As Long ' strName = Space(255) lngCharacters = 255 lngReturn = GetUserName(strName, lngCharacters - 1) If lngReturn = 0然后 GetCurrentUserName = "无法检索名称。" Else GetCurrentUserName = OnlyLetters(strName) End If End Function
【解决方案2】:

下面的链接建议

当前用户()

是函数

CurrentUser()

【讨论】:

  • 奇怪的是,CurrentUser() 返回了值“Admin”。这可能是我的角色,但不是我的用户名。
  • Access 中始终有一个用户名,由CurrentUser() 返回。如果没有在命令行中用/User xyz 指定它,它总是Admin。这(曾经)用于 Access 中的用户级安全性。
【解决方案3】:

安德烈,非常感谢您的洞察力!我找到了这个链接: http://www.codeproject.com/Articles/1422/Getting-User-Information-Using-WSH-and-VBScript

Dim objNet
On Error Resume Next

'In case we fail to create object then display our custom error

Set objNet = CreateObject("WScript.NetWork")
If Err.Number <> 0 Then                 'If error occured then display notice
    MsgBox "Don't be Shy." & vbCRLF &_
               "Do not press ""No"" If your browser warns you."
    Document.Location = "UserInfo.html"
                                        'Place the Name of the document.
                                    'It will display again
End If

Dim strInfo
strInfo = "User Name is     " & objNet.UserName & vbCrLf & _
          "Computer Name is " & objNet.ComputerName & vbCrLf & _
          "Domain Name is   " & objNet.UserDomain
MsgBox strInfo

Set objNet = Nothing                    'Destroy the Object to free the Memory

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    相关资源
    最近更新 更多