2015-06-29 109 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

我不知道到底发生了什么,因为我没有改变任何代码,但它的工作原理现在不再有错误。感谢您的帮助。