[FONT=Times New Roman][SIZE=1]Private Sub cmdDuplicateOrder_Click()[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Dim dbs As DAO.Database[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Dim rstOrderID As DAO.Recordset[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Dim rst1 As DAO.Recordset[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Dim rst2 As DAO.Recordset[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Dim rstProducts As DAO.Recordset[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Dim lngOrderID As Long[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Dim strSQL As String[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Dim x As Long[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Set dbs = CurrentDb[/SIZE][/FONT]
[SIZE=1]'Get the last OrderID so the new OrderID can be computed[/SIZE]
[FONT=Times New Roman][SIZE=1]Set rstOrderID = dbs.OpenRecordset("tblOrders")[/SIZE][/FONT]
[SIZE=1][FONT=Times New Roman]With rstOrderID [/FONT][FONT=Times New Roman] .MoveLast[/FONT][/SIZE]
[FONT=Times New Roman][SIZE=1] lngOrderID = ![OrderId] + 1 'Increment OrderID by 1[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]End With[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]'-----------------------------------------------------------------------------[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]'This portion of code writes the new order information into the Orders table[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]'-----------------------------------------------------------------------------[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]strSQL = "SELECT * " & _[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]"FROM [tblOrders] " & _[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]"WHERE [OrderID] = " & Me.lstOrdersToDuplicate.Column(2) 'Column 2 contains the original OrderID #[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Set rst1 = dbs.OpenRecordset(strSQL, dbOpenSnapshot)[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Set rst2 = dbs.OpenRecordset("tblOrders")[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]rst2.AddNew[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]For x = 0 To rst1.Fields.Count - 1[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] If rst1.Fields(x).Name = "OrderID" Then[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] rst2.Fields(x) = lngOrderID 'Enter new OrderID[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] ElseIf rst1.Fields(x).Name = "OrderDate" Then[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] rst2.Fields(x) = Date 'Update and enter todays date[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] Else[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] rst2.Fields(x) = rst1.Fields(x)[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] End If[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Next x[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]rst2.Update[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]'--------------------------------------------------------------------------------[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]'This portion of code writes the existing order details into the tblOrderDetails table[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]'--------------------------------------------------------------------------------[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]strSQL = "SELECT * " & _[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]"FROM [tblOrderDetails] " & _[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]"WHERE [OrderID] = " & Me.lstOrdersToDuplicate.Column(2)[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Set rst1 = dbs.OpenRecordset(strSQL, dbOpenSnapshot)[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]strSQL = "SELECT * " & _[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]"FROM [tblOrderDetails];"[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Set rst2 = dbs.OpenRecordset(strSQL)[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]While Not rst1.EOF[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] rst2.AddNew[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] For x = 0 To rst1.Fields.Count - 1[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] If rst1.Fields(x).Name = "OrderID" Then 'Enter new OrderID[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] rst2.Fields(x) = lngOrderID[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] 'This portion of code checks for recent price changes[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] ElseIf rst1.Fields(x).Name = "Price" Then[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] strSQL = "SELECT [ProductID], [Price] " & _[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] "FROM [tblProducts] " & _[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] "WHERE [ProductID] = " & rst1![ProductID][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] Set rstProducts = dbs.OpenRecordset(strSQL, dbOpenSnapshot)[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] rst2.Fields(x) = rstProducts![Price][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] Else[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] rst2.Fields(x) = rst1.Fields(x)[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] End If[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] Next x[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1] rst2.Update[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]rst1.MoveNext[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Wend[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]'--------------------------------------------------------------------------[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Set rst1 = Nothing[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Set rst2 = Nothing[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Set rstProducts = Nothing[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]Set rstOrderID = Nothing[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=1]End Sub[/SIZE][/FONT]