2015-06-29 111 views
0

我有一個函數根據傳入它的兩個參數對記錄集執行一些工作。VBA返回集合類型的函數

工作完成後,我試着讓該函數返回一個帶有函數工作ID的集合。代碼工作,但是當它試圖End Function我得到錯誤450

的參數個數錯誤或無效的屬性賦值

我已經看過了好幾個小時,我無法弄清楚是什麼原因造成的錯誤。它絕對不是語法錯誤或拼寫錯誤,因爲整個代碼在沒有錯誤的情況下執行直到結束。我已使用Set將該集合分配回該函數。 (即Set functionA = someCollection

Public Function ConsumedMaterial(prodName As String, totalQty As Integer) As Collection 
On Error GoTo ConsumedMaterial_Err 
Dim rst As Recordset 
Dim db As Database 
Dim strSQL As String 

Set ConsumedMaterial = New Collection 
Set db = CurrentDb() 
strSQL = "SELECT * FROM [Production Data] " & _ 
     "WHERE Product = '" & prodName & "' " & _ 
     "AND Status > '3' " & _ 
     "ORDER BY ID;" 
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset) 

Dim oBal, curBal, amtRem, totLots 
totLots = DCount("[Lot Number]", "Production Data", "[Product] = '" & prodName & "' " & "AND [Status] > '3'") 
oBal = DSum("[Amount Remaining]", "Production Data", "[Product] = '" & prodName & "' " & "AND [Status] > '3'") 
curBal = totalQty 

If totalQty > oBal Then 
    MsgBox "Not enough material for this order. Please create a new Production Order.", vbCritical 
    GoTo ConsumedMaterial_Exit 
End If 


Dim lotsCol As Collection 
Set lotsCol = New Collection 
With rst 
    Do While Not rst.EOF 
     amtRem = .Fields("Amount Remaining").Value 
      If curBal = 0 Then 
       Exit Do 
      ElseIf (amtRem - curBal) < 0 Then 
       .Edit 
        curBal = Abs(amtRem - curBal) 
       .Fields("Amount Remaining").Value = amtRem - (totalQty - curBal) 
        lotsCol.Add (rst.Fields("Lot Number").Value) 
        totalQty = totalQty - amtRem 
        .Update 
       .MoveNext 
      Else 
        .Edit 
        amtRem = amtRem - curBal 
       .Fields("Amount Remaining").Value = amtRem 
        lotsCol.Add (rst.Fields("Lot Number").Value) 
        curBal = 0 
        .Update 
       .MoveNext 
      End If 
    Loop 
End With 

Set ConsumedMaterial = lotsCol 


'Dim i 
'For Each i In lotsCol 
' Debug.Print i 
'Next 

rst.Close 
db.Close 
Set rst = Nothing 
Set db = Nothing 
Set lotsCol = Nothing 

ConsumedMaterial_Exit: 
Exit Function 

ConsumedMaterial_Err: 
MsgBox Error$ & vbCrLf & vbCrLf & "Unable to create an order at this time." 
Resume ConsumedMaterial_Exit 



End Function 
+0

歡迎來到Stackoverflow!和你的**代碼**請嗎? – bonCodigo

+0

我看到兩個人問你的代碼......但我會爭辯說他們應該要求一個顯示你的問題的最小例子。張貼這些代碼通常會讓人們只是去「TLDR」 – firelynx

+0

@firelynx希望有人會憐惜我,並閱讀它。 – JBLucky24

回答

0

我不知道到底發生了什麼,因爲我沒有改變任何代碼,但它的工作原理現在不再有錯誤。感謝您的幫助。