K
Kevin
First of all thanks in advance for any ideas to help me resolve this problem.
I am a total novice when it comes to coding and have had a friend helping me
with this work project because I really do not understand the whole looping
thing and all of the coding. Here goes…
I have a work application that I am putting together that I need to
duplicate Work Orders “WOâ€. The following code is currently what I am using
and it duplicates the WO’s just fine in the table “work_orders†based on the
number of mowings, interval and workdate entered on the form along with all
required fields. The problem that I am having is that in a related table
“JobParts†I need it to enter the duplicated WorkOrderID along with up to 4
PartIDs that were entered with the original WO in the JobParts table and if
no parts are entered it will continue and not put any WorkOrderIDs or PartIDs
in the JobParts Table but still duplicate the WOs in the Work_Order table.
The current code is duplicating the WorkOrderIDs in the JobParts table but
is only entering the first PartID on all duplicated WorkorderIDs and is
generating an additional WorkOrderID. Example…
Work_Order table duplicating 1 work order 2 times
265000 - original
265001 - 1st duplicate
265002 – 2nd duplicate
JobParts table original records with 2 PartIDs
265000 PartID – 4001
265000 PartID – 4002
265001 PartID – 4001
265001 PartID – 4001
265002 PartID – 4001
265002 PartID – 4001
265003 PartID – 4001 Extra unwanted record
265003 PartID – 4001 Extra unwanted record
Clear as mud? here is the cuurrent code that I have been trying
Private Sub Create_Lawn_WorkOrder_Click()
Dim rstWO As ADODB.Recordset
Dim rstJP As ADODB.Recordset
Dim i As Integer, iInterval As Integer, iNumMowings As Integer
Dim dtStart As Date
Dim j As Integer
Set rstWO = New ADODB.Recordset
Set rstJP = New ADODB.Recordset
rstWO.Open "SELECT * From [Work_Orders]", CurrentProject.Connection,
adOpenStatic, adLockOptimistic
rstJP.Open "SELECT * From [JobParts] WHERE [WorkOrderID] = " &
rstWO![WorkorderID], CurrentProject.Connection, adOpenStatic, adLockOptimistic
iInterval = Me.Interval
iNumMowings = Me.NumMowings
dtStartDate = Me.WorkDate
DoCmd.SetWarnings False
For i = 1 To iNumMowings
rstWO.AddNew
rstWO![CustomerID] = Me.Select_Customer
rstWO![CatagoryID] = Me.CatagoryID
rstWO![ChemWO] = Me.ChemWO
rstWO![StatusID] = Me.Status
rstWO![EmplID] = Me.employee
rstWO![WorkTBD] = Me.WorkTBD
rstWO![WorkDate] = DateAdd("d", i * iInterval, dtStartDate)
For j = 1 To rstJP.RecordCount
DoCmd.RunSQL "INSERT INTO JobParts ([WorkorderID],[PartID]) VALUES ("
& rstWO![WorkorderID] & ", " & rstJP![PartID] & ")"
Next j
Next i
DoCmd.SetWarnings True
Set rstWO = Nothing
Set rstJP = Nothing
DoCmd.Close acForm, "New Work Order"
End Sub
I am a total novice when it comes to coding and have had a friend helping me
with this work project because I really do not understand the whole looping
thing and all of the coding. Here goes…
I have a work application that I am putting together that I need to
duplicate Work Orders “WOâ€. The following code is currently what I am using
and it duplicates the WO’s just fine in the table “work_orders†based on the
number of mowings, interval and workdate entered on the form along with all
required fields. The problem that I am having is that in a related table
“JobParts†I need it to enter the duplicated WorkOrderID along with up to 4
PartIDs that were entered with the original WO in the JobParts table and if
no parts are entered it will continue and not put any WorkOrderIDs or PartIDs
in the JobParts Table but still duplicate the WOs in the Work_Order table.
The current code is duplicating the WorkOrderIDs in the JobParts table but
is only entering the first PartID on all duplicated WorkorderIDs and is
generating an additional WorkOrderID. Example…
Work_Order table duplicating 1 work order 2 times
265000 - original
265001 - 1st duplicate
265002 – 2nd duplicate
JobParts table original records with 2 PartIDs
265000 PartID – 4001
265000 PartID – 4002
265001 PartID – 4001
265001 PartID – 4001
265002 PartID – 4001
265002 PartID – 4001
265003 PartID – 4001 Extra unwanted record
265003 PartID – 4001 Extra unwanted record
Clear as mud? here is the cuurrent code that I have been trying
Private Sub Create_Lawn_WorkOrder_Click()
Dim rstWO As ADODB.Recordset
Dim rstJP As ADODB.Recordset
Dim i As Integer, iInterval As Integer, iNumMowings As Integer
Dim dtStart As Date
Dim j As Integer
Set rstWO = New ADODB.Recordset
Set rstJP = New ADODB.Recordset
rstWO.Open "SELECT * From [Work_Orders]", CurrentProject.Connection,
adOpenStatic, adLockOptimistic
rstJP.Open "SELECT * From [JobParts] WHERE [WorkOrderID] = " &
rstWO![WorkorderID], CurrentProject.Connection, adOpenStatic, adLockOptimistic
iInterval = Me.Interval
iNumMowings = Me.NumMowings
dtStartDate = Me.WorkDate
DoCmd.SetWarnings False
For i = 1 To iNumMowings
rstWO.AddNew
rstWO![CustomerID] = Me.Select_Customer
rstWO![CatagoryID] = Me.CatagoryID
rstWO![ChemWO] = Me.ChemWO
rstWO![StatusID] = Me.Status
rstWO![EmplID] = Me.employee
rstWO![WorkTBD] = Me.WorkTBD
rstWO![WorkDate] = DateAdd("d", i * iInterval, dtStartDate)
For j = 1 To rstJP.RecordCount
DoCmd.RunSQL "INSERT INTO JobParts ([WorkorderID],[PartID]) VALUES ("
& rstWO![WorkorderID] & ", " & rstJP![PartID] & ")"
Next j
Next i
DoCmd.SetWarnings True
Set rstWO = Nothing
Set rstJP = Nothing
DoCmd.Close acForm, "New Work Order"
End Sub