2015-05-09 159 views
0

我得到一個編譯錯誤:「參數不是可選的」 基本上在這段代碼中,我試圖圍繞表格樣式自動分配。當代碼被調用時,它開始於AssignNullProjects(),當我得到的錯誤,GetNextAssignee是亮點中的一部分" & GetNextAssignee & "編譯錯誤MS Access SQL

我真的不知道如何解決它,這是我下面全碼:

Public Function AssignNullProjects() As Long 

    Dim db As dao.Database 
    Dim rs As dao.Recordset 
    Dim strSQL As String 

    Set db = CurrentDb 
    strSQL = "SELECT CFRRRID, [program], [language] FROM CFRRR WHERE assignedto Is Null" 
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset) 
    If Not rs.BOF And Not rs.EOF Then 
     While Not rs.EOF 
      strSQL = "UPDATE CFRRR SET assignedto = " & GetNextAssignee & ", assignedby = " & [Forms]![CFRRR]![assignedby] & ", Dateassigned = #" & Now & "#, actiondate = #" & Now & "#, Workername = " & _ 
           [Forms]![CFRRR]![assignedto] & ", WorkerID = " & [Forms]![CFRRR]![assignedto] & " WHERE CFRRRID = " & rs!CFRRRID 

      db.Execute strSQL, dbFailOnError 
      rs.MoveNext 
     Wend 
    End If 

    rs.Close 
    db.Close 
    Set rs = Nothing 
    Set db = Nothing 

End Function 

Public Function GetNextAssignee(program As String, Language As String) As Long 
' Returns UserID as a Long Integer with the lowest [TS] value, 
' and updates same [TS] by incremented with 1. 

    Dim db As dao.Database 
    Dim rs As dao.Recordset 
    Dim strSQL As String 

    Set db = CurrentDb 
    strSQL = "SELECT TOP 1 WorkerID FROM attendance WHERE [Programs] LIKE '*" & program & "*' AND [Language] = '" & Language & "' AND [Status] = '" & Available & "' ORDER BY TS ASC" 
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset) 
    If Not rs.BOF And Not rs.EOF Then 
     'Found next assignee, update date/time stamp 
'  strSQL = "UPDATE tblUser SET TS = " & DMax("[TS]", tblUser) + 1 & " WHERE [WorkerID]= " & rs!workerid 
     strSQL = "UPDATE attendance SET TS = " & DMax("[TS]", "attendance") + 1 & " WHERE [WorkerID]= " & rs!workerid 
     db.Execute strSQL, dbFailOnError 
     GetNextAssignee = rs!workerid 
    Else 
     'Field TS has NO VALUE FOR ALL RECORDS! 
     'Code calling this function should check for a return of 0 indicating an error. 
     GetNextAssignee = 0 
    End If 

    rs.Close 
    db.Close 
    Set rs = Nothing 
    Set db = Nothing 

End Function 
+1

'GetNextAssignee'顯然需要兩個參數('program'和'Language'),如聲明中所示;你的調用代碼並不提供'&GetAssignee&')。你還希望發生什麼? 「爲了通過郵遞服務送達,你的信件需要兩件事:一個送貨地址和一張郵資充足的郵票,你們兩個都沒有提供,我們無法完成你寄送這封信的請求。」 –

回答

2

當你調用這個函數,你必須提供2個參數(字符串值):

Public Function GetNextAssignee(program As String, Language As String) As Long 

但構建UPDATE語句時,你叫塔沒有提供所需的參數t的函數:

strSQL = "UPDATE CFRRR SET assignedto = " & GetNextAssignee & ", assignedby = " 

這樣進出抱怨那些(串)參數是不可選的 - 你必須提供他們。當你給函數像這樣2串代碼應該編譯...

strSQL = "UPDATE CFRRR SET assignedto = " & GetNextAssignee("foo", "bar") & ", assignedby = " 

儘管代碼將與這些字符串編譯,功能可能不會返回你所需要的結果。因此用實際值代替foobar

+0

我是否按照功能鍵入? ''&GetNextAssignee(程序爲字符串,語言爲字符串)As Long&「'但我收到一個錯誤,說明列表分隔符或)並且'As'突出顯示。 – Lilly

+0

偉大的伎倆,現在我得到一個錯誤的下半部分,說太少的參數:期望2.'strSQL =「選擇TOP1 WorkerID從出席在哪裏[程序]像'*」&程序&「* '和[語言] =''和語言&''和[狀態] =「&(」可用「)和」按順序ASC「 Set rs = db.OpenRecordset(strSQL,dbOpenDynaset)''''雖然。 – Lilly

+0

是的,這是一個不同的問題,所以一個新的問題是要走的路。當你寫這個問題時,當你遇到新的錯誤時,請粘貼「Debug.Print strSQL」的輸出。最好向我們展示實際的失敗聲明,而不僅僅是創建聲明的代碼。 – HansUp