zelarra821
Registered User.
- Local time
- Today, 07:59
- Joined
- Jan 14, 2019
- Messages
- 835
Hi guys.
I'm trying to make a loop that goes through the records in a table, but the program doesn't respond when I run it.
This is the code:
Let me explain a little:
This is a table where records of hours worked are kept.
What I am looking for with this loop is to update the calculations. To do this, it must be crossed with the price table of the different amounts to be searched to obtain the price corresponding to the date.
I don't want to confuse you too much, so I don't know if this explanation can be useful or if I will have to expand on it.
I need help to make this work. I don't mind going into more detail, sending screenshots, videos, whatever is necessary.
I am not sending the database because isolating the specific case, with so many related tables, is impossible.
Thank you very much.
I'm trying to make a loop that goes through the records in a table, but the program doesn't respond when I run it.
This is the code:
Code:
Private Sub CalcularImporte()
Dim rstTable As DAO.Recordset
Dim rst As DAO.Recordset
Dim strSql As String
Dim ManoDeObra As Double
Dim Vehiculo As Double
Dim Apero1 As Double
Dim Apero2 As Double
Set rstTable = CurrentDb.OpenRecordset("TGastosHoras")
Do Until rstTable.EOF
rstTable.MoveFirst
rstTable.Edit
'Mano de obra
strSql = "SELECT Top 1 Precio" _
& " FROM TManoDeObra" _
& " WHERE CampañaNumero <= " & rstTable("CampañaNumero") & " And IdTrabajador=" & Nz(rstTable("IdTrabajador"), 1) & "" _
& " ORDER BY CampañaNumero DESC"
Set rst = CurrentDb.OpenRecordset(strSql)
If Not (rst.EOF And rst.BOF) Then
ManoDeObra = rst("Precio")
rstTable("ManoDeObra") = Nz(ManoDeObra, 0) * Nz(rstTable("Horas"), 0)
End If
'Vehiculos y maquinaria
strSql = "SELECT Top 1 Precio" _
& " FROM TVehiculosPrecios" _
& " WHERE CampañaNumero <= " & rstTable("CampañaNumero") & " And IdVehiculo=" & Nz(rstTable("IdVehiculo"), 1) & "" _
& " ORDER BY CampañaNumero DESC"
Set rst = CurrentDb.OpenRecordset(strSql)
If Not (rst.EOF And rst.BOF) Then
Vehiculo = rst("Precio")
rstTable("Vehiculo") = Nz(Vehiculo, 0) * Nz(rstTable("Horas"), 0)
End If
'Aperos
'1
strSql = "SELECT Top 1 Precio" _
& " FROM TAperosPrecios" _
& " WHERE CampañaNumero <= " & rstTable("CampañaNumero") & " And IdApero=" & Nz(rstTable("IdApero1"), 1) & "" _
& " ORDER BY CampañaNumero DESC"
Set rst = CurrentDb.OpenRecordset(strSql)
If Not (rst.EOF And rst.BOF) Then
Apero1 = rst("Precio")
rstTable("ImpApero1") = Nz(Apero1, 0) * Nz(rstTable("Horas"), 0)
End If
'2
strSql = "SELECT Top 1 Precio" _
& " FROM TAperosPrecios" _
& " WHERE CampañaNumero <= " & rstTable("CampañaNumero") & " And IdApero=" & Nz(rstTable("IdApero2"), 1) & "" _
& " ORDER BY CampañaNumero DESC"
Set rst = CurrentDb.OpenRecordset(strSql)
If Not (rst.EOF And rst.BOF) Then
Apero2 = rst("Precio")
rstTable("ImpApero2") = Nz(Apero2, 0) * Nz(rstTable("Horas"), 0)
End If
'Terminar rst
rst.Close
Set rst = Nothing
'Resultado
rstTable("Importe") = (Nz(ManoDeObra, 0) + Nz(Vehiculo, 0) + Nz(Apero1, 0) + Nz(Apero2, 0)) * Nz(rstTable("Horas"), 0)
rstTable("Total") = rstTable("Importe") + rstTable("SumaDesglose")
rstTable.Update
rstTable.MoveNext
Loop
rstTable.Close
Set rstTable = Nothing
End Sub
Let me explain a little:
This is a table where records of hours worked are kept.
What I am looking for with this loop is to update the calculations. To do this, it must be crossed with the price table of the different amounts to be searched to obtain the price corresponding to the date.
I don't want to confuse you too much, so I don't know if this explanation can be useful or if I will have to expand on it.
I need help to make this work. I don't mind going into more detail, sending screenshots, videos, whatever is necessary.
I am not sending the database because isolating the specific case, with so many related tables, is impossible.
Thank you very much.