2015-05-26 47 views
0

我試圖用VBA在2010年進入創建登錄表單,但我經歷了一個錯誤信息:訪問對象所需

運行時錯誤(424)所需的對象。

任何人都可以幫助我嗎?

代碼:

Dim dbs As Database 
Dim rstUserPwd As Recordset 
Dim bFoundMatch As Boolean 

Set dbs = CurrentDb 
Set rstUserPwd = dbs.OpenRecordset("users") 

bFoundMatch = False 

If rstUserPwd.RecordCount > 0 Then 
    rstUserPwd.MoveFirst 

    ' check for matching records 
    Do While rstUserPwd.EOF = True 
     ' ERROR occurs on next line ... 
     If rstUserPwd![user_name] = login.txtName.Value And rstUserPwd![Password] = login.txtPass.Value Then 
      bFoundMatch = True 
      Exit Do 
     End If 
     rstUserPwd.MoveNext 
    Loop 
End If 

If bFoundMatch = True Then 
    'Open the next form here and close this one 
    DoCmd.Close acForm, Me.Name 
    DoCmd.OpenForm "frmNavigation" 
Else 
    ' 
    MsgBox "Incorrect Username or Password" 
End If 

rstUserPwd.Close 
+0

什麼是'login.txtName'? – HansUp

+0

就像下面的評論所說,知道斷點會有很大幫助,但這裏有兩個猜測。首先,login.txtname應該可能有一個表單!在它前面。你也可以使用me.txtnames。其次,您可能需要在前兩行中指定ado.database和ado.recordset。 – Gene

+0

login是表單的名稱,txtName是用於輸入用戶名的文本字段的名稱 –

回答

0

它在哪裏突破? 不知道,我會做的第一件事是添加「昏暗的dbs作爲數據庫」。出於某種原因,它可能沒有看到這一點。檢查工具,參考下,並確保您的DAO庫被選中。

編輯補充 -

閱讀您對其中的代碼是打破回覆之後,我相信你的登錄表單已損壞。試試這些恢復方法 - http://www.datawright.com.au/access_resources/recovering_corrupted_form_or_report.htm

+0

我做到了。但問題仍然存在 –

+0

在以下行: 如果rstUserPwd![user_name] = login.txtName.Value和rstUserPwd![Password] = login.txtPass.Value然後 我試圖將login.txtName更改爲任何字符串值如「一些文本」,如:如果rstUserPwd![user_name] =「一些文本」。問題沒有出現。但我仍然沒有找到問題的原因。誰能幫我? –

+0

查看我對答案的編輯。 – BCole