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

时间:2015-03-28 13:19:49

标签: forms vba ms-access access-vba ms-access-2010

我有一个带登录表单的数据库,并且在成功登录后,主导航表单应该打开并且登录表单应该关闭...除了登录表单拒绝关闭自己而是抛出&# 34;运行时错误' 2585':处理表单或报告事件时无法执行此操作。"

以下是我为代码所做的事情:

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只会向下滚动到在那里结束并打开导航表单,然后它应该关闭自己。调试器专门指向DoCmd.Close acForm,&#34; frmLogin&#34;每次都在底部。我已经尝试将该行移动到frmNavMain Form_OnLoad()中,但它仍会引发相同的运行时错误。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

移动

DoCmd.Close acForm, "frmLogin"

到frmNavMain的OnActivate事件。 另外,检查即使是frmLogin在OnClose中运行的代码。

答案 1 :(得分:0)

关闭它们后尝试取消初始化您的记录集(在释放cpu资源时,始终是对所有设置对象的良好做法)。见SO post。这甚至暗示了Compact&amp;修复可能会帮助您,因为资源可能已经堆积在内存中:

Set rs = nothing 
Set rc = nothing

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