Access 2010 登录表单不会自行关闭

Access 2010 Login Form won't close itself

我有一个带有登录表单的数据库,成功登录后,主导航表单应该打开,登录表单应该关闭...除了登录表单拒绝自行关闭而是抛出"Run-time error '2585': This action can't be carried out while processing a form or report event."

这是我得到的代码:

Private Sub buttonLogin_Click()
Dim hash As New CMD5
Dim salt As String
Dim result As String
Dim rs As DAO.Recordset
Dim rc As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("tblCurrentUser", dbOpenDynaset)
Set rc = CurrentDb.OpenRecordset("tblCustom", dbOpenDynaset)
'----Check If User ID Or Password Is Null----
If IsNull(ID) Then
    MsgBox "Please enter your User ID", vbOKOnly
    Me.ID.SetFocus
    Exit Sub
End If
If IsNull(Password) Then
    MsgBox "Please enter your password", vbOKOnly
    Me.Password.SetFocus
    Exit Sub
End If
'----Validate Login Information----
' This section builds the salted MD5 hash and compares
' it to the password stored in the personnel table.
result = DLookup("Password", "tblPersonnel", "EmpID = '" & ID & "'")
salt = ID & "-" & Password
If result <> hash.MD5(salt) Then
    MsgBox "Please enter a valid User ID and Password", vbOKOnly
    Me.ID.SetFocus
    Exit Sub
End If
'----Check User Access Permission----
If DLookup("AccessLevel", "tblPersonnel", "EmpID = '" & ID & "'") = 0 Then
    MsgBox "Access denied, please contact " & rc![DBA] & " for database access.", vbExclamation
    Me.ID.SetFocus
    Exit Sub
End If
'----Store Current User and Access Level to temp table----
rs.Edit
rs![UserID] = ID
rs![AccessLevel] = DLookup("AccessLevel", "tblPersonnel", "EmpID = '" & ID & "'")
rs.Update
rs.Close
rc.Close
'----Open Navigation Form, close Login----
DoCmd.OpenForm "frmNavMain", acNormal
DoCmd.Close acForm, "frmLogin"
End Sub

正如你所看到的,当用户登录时我有很多事情要做。任何无效的条目都会被捕获,如果发生这种情况,sub 就会退出,但假设一切都已检查完毕,sub 就会向下滚动到在那里结束并打开导航表单,然后它 应该 自行关闭。调试器每次都专门指向底部的 DoCmd.Close acForm, "frmLogin" 行。我已经尝试将该行移动到 frmNavMain Form_OnLoad() 但它仍然会抛出相同的 运行-time 错误。

我在这里错过了什么?

移动

DoCmd.Close acForm, "frmLogin"

frmNavMain 的 OnActivate 事件。 此外,检查代码 运行 在 OnClose even of frmLogin.

尝试在关闭记录集后取消初始化它们(在释放 cpu 资源时对所有设置的对象始终是一个很好的做法)。参见 SO post。这甚至表明 Compact & Repair 可能会帮助您,因为资源可能已堆积在内存中:

Set rs = nothing 
Set rc = nothing

临时 table 更新的另一个建议是使用由 DoCmd.OpenQuery 或 VBA 查询调用的更新操作存储查询与 DoCmd.RunSQLCurrentDB.Execute而不是记录集方法。