F
Frank Dulk
I have that code that calculates the medium price of a product, but he only
calculates of 1 product of every time. I wanted him to calculate at once of
several products.
To each new product he went cleaning the variables and calculating
everything again.
Sub CalcCustoMedio()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Dim rst As DAO.Recordset
Dim dblValorAcum As Double
Dim dblQtAcum As Double
Dim dblCustoMed As Double
Dim dblUltCusto As Double
Set db = CurrentDb
Set qdf = db.QueryDefs("qryMovimentoInventario")
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecordset
If Not rst.RecordCount = 0 Then
rst.Sort = "MV_DTC ASC"
rst.MoveFirst
End If
Set rstCusto = New ADODB.Recordset
rstCusto.Fields.Append "ID", adInteger
rstCusto.Fields.Append "Preco", adDouble
rstCusto.Fields.Append "Custo", adDouble
rstCust
pen , , adOpenStatic, adLockOptimistic
With rst
Do Until .EOF
dblValorAcum = dblValorAcum + !PrecoTotal
dblQtAcum = dblQtAcum + IIf(!MV_TM = "C", !SaldoDeUnidades, _
-1 * !SaldoDeUnidades)
dblCustoMed = dblValorAcum / dblQtAcum
'Grava no recordset ADO
rstCusto.AddNew
rstCusto!ID = !mv_id
If !MV_TM = "C" Then
rstCusto!preco = !PrecoUnitario
dblUltCusto = dblCustoMed
Else
rstCusto!preco = dblUltCusto
End If
rstCusto!custo = dblCustoMed
rstCusto.Update
.MoveNext
Loop
.Close
End With
Set rst = Nothing
Set qdf = Nothing
Set db = Nothing
End Sub
calculates of 1 product of every time. I wanted him to calculate at once of
several products.
To each new product he went cleaning the variables and calculating
everything again.
Sub CalcCustoMedio()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Dim rst As DAO.Recordset
Dim dblValorAcum As Double
Dim dblQtAcum As Double
Dim dblCustoMed As Double
Dim dblUltCusto As Double
Set db = CurrentDb
Set qdf = db.QueryDefs("qryMovimentoInventario")
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecordset
If Not rst.RecordCount = 0 Then
rst.Sort = "MV_DTC ASC"
rst.MoveFirst
End If
Set rstCusto = New ADODB.Recordset
rstCusto.Fields.Append "ID", adInteger
rstCusto.Fields.Append "Preco", adDouble
rstCusto.Fields.Append "Custo", adDouble
rstCust
With rst
Do Until .EOF
dblValorAcum = dblValorAcum + !PrecoTotal
dblQtAcum = dblQtAcum + IIf(!MV_TM = "C", !SaldoDeUnidades, _
-1 * !SaldoDeUnidades)
dblCustoMed = dblValorAcum / dblQtAcum
'Grava no recordset ADO
rstCusto.AddNew
rstCusto!ID = !mv_id
If !MV_TM = "C" Then
rstCusto!preco = !PrecoUnitario
dblUltCusto = dblCustoMed
Else
rstCusto!preco = dblUltCusto
End If
rstCusto!custo = dblCustoMed
rstCusto.Update
.MoveNext
Loop
.Close
End With
Set rst = Nothing
Set qdf = Nothing
Set db = Nothing
End Sub