I recognize that code! ([sing]We are the champions[/sing] - We includes WayneRyan)spacepro said:Hi Guys,
Played with the code abit and managed to do what I needed for > 21 by using the following code:
If Totalvalue = > 21 Then
rst.Edit ' New Line
rst!Loadnumber = Newvalue + 1
rst.Update ' New Line
rst.movenext
NewValue = NewValue + 1 ' Need to point to the next truck
End If
Why do you need to loop through the new records?spacepro said:Sorry to be a pain but I am really struggling with the looping through the added records.
NumOfRec = rst.RecordCount
...
'This will only look through the existing records
'and not any records added via this loop
For i = 1 To NumOfRec
...
Next i
'Will loop through all the records.
While Not rst.EOF and Not rst.BOF
...
Wend
This goes towards my last post.spacepro said:Thanks Guys for all of your help, and am know working on the 21 part of the code so that the qty is changed and the balance is inserted into the new record.
Case Is > 21
BalanceValue = TotalValue - 21
[color=green] 'Store current records value[/color]
CurrentValue = rst.Fields("NumOfLanes")
[color=green] 'Calculated what the value of current records should be to add up to 21[/color]
UpdatedValue = CurrentValue - BalanceValue
[color=green] 'Repeat the next line's logic for any other fields
'that need to be duplicated[/color]
CurrentLane = rst.Fields("MyField")
With rst
[color=green] 'Change current record's value to the calculated value[/color]
.Edit
.Fields("NumOfLanes") = UpdatedValue
.Fields("MyField") = CurrentLane
.Update
[color=green] 'Add new record[/color]
.AddNew
[color=green] 'Enter values for new record[/color]
[color=green] 'Add one to current lane since the balance should go to a new lane[/color]
.Fields("MyField") = CurrentLane + 1
.Fields("NumOfLanes") = BalanceValue
.Update
End With
[color=green] 'If using the same NewValue as previous record,
'it should increment after adding the new record
'if not, then it should be incremented before adding the new record?[/color]
NewValue = NewValue + 1
StoredValue = 0
rst.MoveNext
Dim myDB As DAO.Database
Dim rst As DAO.Recordset
Dim NumOfRec As Long
Dim CurrentLanes, StoredLanes, TotalLanes As Double
Set myDB = CurrentDb()
Set rst = myDB.OpenRecordset("tblLanes", dbOpenDynaset)
StartOver:
[color=green]'Set StatusBar message[/color]
StsBar = SysCmd(acSysCmdSetStatus, "Calculating Lanes Usage...")
rst.MoveLast
NumOfRec = rst.RecordCount
StoredLanes = 0
rst.MoveFirst
For i = 1 To NumOfRec
CurrentLanes = rst.Fields("Lanes")
TotalLanes = CurrentLanes + StoredLanes
Select Case TotalLanes
Case Is = 21
StoredLanes = 0
[color=green] 'If at end of records and =21 then assigning of lanes is complete[/color]
If i = NumOfRec Then
GoTo AssignTruck:
End If
rst.MoveNext
Case Is < 21
[color=green] 'If at end of records and <21 then assigning of lanes is complete[/color]
StoredLanes = StoredLanes + CurrentLanes
If i = NumOfRec Then
GoTo AssignTruck:
End If
rst.MoveNext
Case Is > 21
BalanceLanes = TotalLanes - 21
[color=green] 'Store current records value[/color]
CurrentLanes = rst.Fields("Lanes")
[color=green] 'Calculated what the value of current records should be to add up to 21[/color]
UpdatedLanes = CurrentLanes - BalanceLanes
With rst
[color=green] 'Change current record's value to the calculated value[/color]
.Edit
.Fields("Lanes") = UpdatedLanes
.Update
[color=green] 'Add new record[/color]
.AddNew
[color=green] 'Enter values for new record
'Add one to current lane since the balance should go to a new lane[/color]
.Fields("Lanes") = BalanceLanes
.Update
End With
StoredLanes = 0
rst.MoveNext
End Select
Next i
GoTo StartOver:
AssignTruck:
StsBar = SysCmd(acSysCmdSetStatus, "Assigning Trucks...")
rst.MoveLast
NumOfRec = rst.RecordCount
TruckNum = 1
StoredLanes = 0
rst.MoveFirst
For n = 1 To NumOfRec
CurrentLanes = rst.Fields("Lanes")
TotalLanes = CurrentLanes + StoredLanes
Select Case TotalLanes
Case Is = 21
With rst
.Edit
.Fields("Truck") = TruckNum
.Update
End With
TruckNum = TruckNum + 1
StoredLanes = 0
Case Is < 21
With rst
.Edit
.Fields("Truck") = TruckNum
.Update
End With
StoredLanes = StoredLanes + CurrentLanes
Case Is > 21
[color=green] 'This case should [b]never[/b] return true [/color]
MsgBox "Somthing is wrong!", vbCritical, "Problem occured..."
End Select
rst.MoveNext
Next n
StsBar = SysCmd(acSysCmdSetStatus, " ")
Set rst = Nothing
Set myDB = Nothing
Dim myDB As DAO.Database
...
Set myDB = CurrentDb()
Set rst = myDB.OpenRecordset("tblLanes", dbOpenDynaset)
[color=red][b]StartOver[/b][/color]:
rst.MoveLast
NumOfRec = rst.RecordCount
StoredLanes = 0
rst.MoveFirst
For i[b]Assign[/b] = 1 To NumOfRec
CurrentLanes = rst.Fields("Lanes")
TotalLanes = CurrentLanes + StoredLanes
Select Case TotalLanes
Case Is = 21
...
Case Is < 21
...
Case Is > 21
...
End Select
Next i[b]Assign[/b]
rst.MoveLast
NumOfRec = rst.RecordCount
StoredLanes = 0
rst.MoveFirst
[color=green]'Loop to check if all the lane assignments are OK,
'If not, go back to assigning loop[/color]
For i[b]Check[/b] = 1 To NumOfRec
CurrentLanes = rst.Fields("Lanes")
TotalLanes = CurrentLanes + StoredLanes
Select Case TotalLanes
Case Is = 21
StoredLanes = 0
rst.MoveNext
Case Is < 21
StoredLanes = StoredLanes + CurrentLanes
rst.MoveNext
Case Is > 21
[color=green] 'If there is still a sequence >21
'go back and rerun assigning of lanes[/color]
GoTo [color=red][b]StartOver[/b][/color]:
End Select
Next i[b]Check[/b]
[color=blue][b]AssignTruck[/b][/color]:
...
Set myDB = Nothing
Be carefull when using int, see these knowlwedge base article for why.spacepro said:I ran the code and it was ok but didn't like when the lane value was 9.112233 etc.
So I have formatted the calculation so that it round off to the next whole number using the "int" function in my query that calculates the lanes.
Aw... shucks..WayneRyan said:A big thanks to Cosmos, not only is he QUICK, but once you get
his attention ... problem solved!