mfaqueiroz
Registered User.
- Local time
- Today, 14:06
- Joined
- Sep 30, 2015
- Messages
- 125
Hei 
I've a module that compares two tables:
-Table1: with all On registers
-Table2:with all Off registers
And give me the time difference between this two.
When I run this module it crashes after 4863 row.
I have put all my variables as Long, but still crashing ..any other suggestion?

Thank you!
--------------------
Code:
Sub TimeBetweenOnOff()
DoCmd.SetWarnings False
Dim x, y As Long
Dim EndOn, EndOff As Long
Dim DateOn, MachineOn, DepartOn, DateOff, MachineOff, msOff, DepartOff, Difms As Long
Dim flag As Long
Dim TimeOnOff As Double
Set dbs = DBEngine(0)(0)
Set On = dbs.OpenRecordset("Table1", DB_OPEN_DYNASET) 'Query Lig já com visto e EventLogDesl
Set Off = dbs.OpenRecordset("Table2", DB_OPEN_DYNASET) 'Query Lig já com visto e EventLogDesl
EndOn = DCount("[ID]", "Table1")
EndOff = DCount("[ID]", "Table2")
x = 0
y = 0
On.MoveFirst
For x = 1 To EndOn
DateOn = On.Fields(2).Value
MachineOn = On.Fields(6).Value
msOn = On.Fields(3).Value
DepartOn = On.Fields(5).Value
Off.MoveFirst
For y = 1 To EndOff
DateOff = Off.Fields(2).Value
MachineOff = Off.Fields(6).Value
msOff = Off.Fields(3).Value
DepartOff = Off.Fields(5).Value
If MachineOn = MachineOff And DepartOn = DepartOff And DateOn < DateOff Then
If msOff > msOn Then
TimeOnOff = DateDiff("s", DateOn, DateOff)
Difms = msOff - msOn
TimeOnOff = TimeOnOff * 1000 + Difms
Off.Edit
Off.Fields(11).Value = "Sim"
Off.Update
On.Edit
On.Fields(11).Value = TimeOnOff
On.Fields(12).Value = DateOff
On.Update
flag = 1
If flag = 1 Then
y = EndOff
Else
End If
ElseIf msOff < msOn Then
TimeOnOff = DateDiff("s", DateOn, DateOff)
Difms = msOff - msOn + 1000
TimeOnOff = ((TimeOnOff - 1) * 1000) + Difms
Off.Edit
Off.Fields(11).Value = "Sim"
Off.Update
On.Edit
On.Fields(11).Value = TimeOnOff
On.Fields(12).Value = DateOff
On.Update
flag = 1
If flag = 1 Then
y = EndOff
Else
End If
End If
End If
Off.MoveNext
Next y
On.MoveNext
Next x
End Sub

I've a module that compares two tables:
-Table1: with all On registers
-Table2:with all Off registers
And give me the time difference between this two.
When I run this module it crashes after 4863 row.
I have put all my variables as Long, but still crashing ..any other suggestion?

Thank you!
--------------------
Code:
Sub TimeBetweenOnOff()
DoCmd.SetWarnings False
Dim x, y As Long
Dim EndOn, EndOff As Long
Dim DateOn, MachineOn, DepartOn, DateOff, MachineOff, msOff, DepartOff, Difms As Long
Dim flag As Long
Dim TimeOnOff As Double
Set dbs = DBEngine(0)(0)
Set On = dbs.OpenRecordset("Table1", DB_OPEN_DYNASET) 'Query Lig já com visto e EventLogDesl
Set Off = dbs.OpenRecordset("Table2", DB_OPEN_DYNASET) 'Query Lig já com visto e EventLogDesl
EndOn = DCount("[ID]", "Table1")
EndOff = DCount("[ID]", "Table2")
x = 0
y = 0
On.MoveFirst
For x = 1 To EndOn
DateOn = On.Fields(2).Value
MachineOn = On.Fields(6).Value
msOn = On.Fields(3).Value
DepartOn = On.Fields(5).Value
Off.MoveFirst
For y = 1 To EndOff
DateOff = Off.Fields(2).Value
MachineOff = Off.Fields(6).Value
msOff = Off.Fields(3).Value
DepartOff = Off.Fields(5).Value
If MachineOn = MachineOff And DepartOn = DepartOff And DateOn < DateOff Then
If msOff > msOn Then
TimeOnOff = DateDiff("s", DateOn, DateOff)
Difms = msOff - msOn
TimeOnOff = TimeOnOff * 1000 + Difms
Off.Edit
Off.Fields(11).Value = "Sim"
Off.Update
On.Edit
On.Fields(11).Value = TimeOnOff
On.Fields(12).Value = DateOff
On.Update
flag = 1
If flag = 1 Then
y = EndOff
Else
End If
ElseIf msOff < msOn Then
TimeOnOff = DateDiff("s", DateOn, DateOff)
Difms = msOff - msOn + 1000
TimeOnOff = ((TimeOnOff - 1) * 1000) + Difms
Off.Edit
Off.Fields(11).Value = "Sim"
Off.Update
On.Edit
On.Fields(11).Value = TimeOnOff
On.Fields(12).Value = DateOff
On.Update
flag = 1
If flag = 1 Then
y = EndOff
Else
End If
End If
End If
Off.MoveNext
Next y
On.MoveNext
Next x
End Sub