Public Function ItemMCost1(ByVal ItmCod As String, ByVal SizeCode As Long) As Double

Dim Dy As Recordset Dim Sql As String Dim MCost As Double Dim Quant As Double

MCost = 0 Quant = 0

Sql = "SELECT DocType ,Quntity ,Price FROM ItemTran" & Chr(13) Sql = Sql & " WHERE ItemCode = '" & Trim$(ItmCod) & "'" & Chr(13) Sql = Sql & " AND SizeCode = " & Trim$(SizeCode) & Chr(13) Sql = Sql & " ORDER BY DocDate" Set Dy = DB1.OpenRecordset(Sql, 2) If Not Dy.EOF Then Dy.MoveFirst While Not Dy.EOF Select Case Dy("DocType") Case 1, 2, 8 If Quant + Dy("Quntity") <> 0 Then MCost = ((Quant * MCost) + (Dy("Quntity") * Dy("Price"))) / (Quant + Dy("Quntity")) Quant = Quant + Dy("Quntity") Else MCost = 0 Quant = 0 End If Case Else Quant = Quant + Dy("Quntity") End Select Dy.MoveNext Wend End If Dy.Close Set Dy = Nothing ItemMCost = MCost End Function