2015-04-04 122 views
0

我想從訪問查詢設計器,在Access中正常工作,但當我嘗試將語句跨VBA它給我這個錯誤消息:運行時錯誤太少參數預計2

運行時錯誤太少參數。預計2.

我已經打印了立即窗口中的聲明並在Access中運行它,它正在運行而無需詢問參數。我做了一些網站的搜索一般的共識似乎是宣告這一切在VBA,包括參數 -

Private Sub CmdAppend_Click() 
    Dim db1 As Database 
    Dim mystr As Recordset2 
    Dim UserName As String 
    Dim UpdateSQL As String 
    Dim SelectIDSQL As String 
    Dim checkstr As String 

    If Validate_Data = True Then 

     UserName = Environ$("Username") 

     SelectIDSQL = "Select Distinct ChecklistResults.[StaffID]" _ 
      & " From ChecklistResults" _ 
      & " Where (((ChecklistResults.[ClientID])=[Forms]![TeamLeader]![ComClientNotFin])" _ 
      & " And ((ChecklistResults.[DateofChecklist])=[Forms]![TeamLeader]![ComDateSelect])" _ 
      & " AND ((ChecklistResults.[ManagerID]) Is Null));" 

     Debug.Print SelectIDSQL 

     Set db1 = CurrentDb 
     Set mystr = db1.OpenRecordset(SelectIDSQL) 
     checkstr = mystr!StaffID 

     If checkstr <> UserName Then 

我收到上述錯誤消息時,我嘗試設置mystr的記錄。我想我可以按照下面的格式獲得記錄集,但是有沒有辦法讓上面的SQL語句/賦值工作?

Dim qdf1 As DAO.QueryDef 

Set qdf1 = db1.QueryDefs("Get_StaffID") 
qdf1.Parameters(0) = [Forms]![TeamLeader]![ComClientNotFin] 
qdf1.Parameters(1) = [Forms]![TeamLeader]![ComDateSelect] 
Set rst1 = qdf1.OpenRecordset(dbOpenDynaset) 

回答

0

我看着this page,我看到的例子,其中OpenRecordset方法有兩個參數。你有一個錯誤消息,說有些東西需要2個參數。嘗試改變這一點:

Set mystr = db1.OpenRecordset(SelectIDSQL) 

這樣:

Set mystr = db1.OpenRecordset(SelectIDSQL, dbOpenDynaset) 
+0

謝謝,我試過,但它似乎沒有區別。我仍然得到相同的錯誤。我認爲這與SQL語句有關,儘管ChecklistResults是一個表,它從表單中提取信息,也許這是導致錯誤 – Gablet 2015-04-04 12:13:13

+0

嘗試更簡單的查詢。就像'select count(*)records from checkslistresults where 1 = 2' – 2015-04-04 13:00:27

0

感謝您的輸入,我用下面的代碼獲取我一直在尋找的結果。它使用查詢SelectClientID返回完成清單第一階段的人員的ID。然後檢查完​​成第二次檢查的人員,如果他們匹配,則返回錯誤消息。如果兩個不同的人已完成它,它使用SQL語句更新以前的記錄與第二個檢查器的ID -

Private Sub CmdAppend_Click() 
Dim rst1 As Recordset2 
Dim db1 As Database 
Dim mystr As Recordset2 
Dim UserName As String 
Dim UpdateSQL As String 
Dim SelectIDSQL As String 
Dim checkstr As String 
Dim qdf1 As DAO.QueryDef 

Set db1 = CurrentDb 
Set qdf1 = db1.QueryDefs("SelectClientID") 
qdf1.Parameters(0) = [Forms]![TeamLeader]![ComClientNotFin] 
qdf1.Parameters(1) = [Forms]![TeamLeader]![ComDateSelect] 
Set rst1 = qdf1.OpenRecordset(dbOpenDynaset) 

If Validate_Data = True Then 

    UserName = Environ$("Username") 

    UpdateSQL = "UPDATE ChecklistResults" _ 
    & " SET ChecklistResults.[ManagerID] = '" & UserName & "'" _ 
    & " WHERE (((ChecklistResults.[ClientID])=[Forms]![TeamLeader]![ComClientNotFin])" _ 
    & " AND ((ChecklistResults.[DateofChecklist])=[Forms]![TeamLeader]![ComDateSelect])" _ 
    & " AND ((ChecklistResults.[ManagerID]) Is Null));" 


    checkstr = rst1!StaffID 
     If checkstr <> UserName Then 
     DoCmd.SetWarnings False 
     DoCmd.RunSQL UpdateSQL 
     DoCmd.SetWarnings True 
     DoCmd.Close 
     Else 
     MsgBox ("This Checklist was created by you and cannot therefore Checked by you") 
     End If 
Else 
Exit Sub 
End If 
End Sub 
相關問題