Question : Working with recordsets

Can someone tell me why this code doesn't work? What I want it to do is to loop through a subform that has multiple assortments and insert them into the Order Details Subform? The way this is it onlyl inserts the first assortment.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
Private Sub cmdInsertassmt_Click()
 
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String, strSQLInsert As String
 
Set dbs = CurrentDb
Set rst = Me.frmInternationalAssortments.Form.RecordsetClone
strSQL = "Select ProductID, Quantity FROM tblAssortments WHERE AssortmentID = '" & Me!frmInternationalAssortments.Form!txtAssortmentID & "'"
      
Set rst = dbs.OpenRecordset(strSQL)
    rst.MoveFirst
       Do Until rst.EOF
            strSQLInsert = "INSERT INTO [tblInternationalOrderDetails] " & _
                "(OrderID, ProductID, Quantity) SELECT " & Me!OrderID & ", " & _
                    rst!ProductID & ", " & rst!Quantity * Me.txtDefaultQty
                CurrentDb.Execute strSQLInsert, dbFailOnError
            rst.Edit
            rst.Update
            rst.MoveNext
        Loop
 
Set dbs = Nothing
Set rst = Nothing
Me!sbfInternationalOrderDetails.Requery
Me!sbfInternationalOrderDetails.SetFocus
DoCmd.GoToRecord , , acNewRec
Me!sbfInternationalTotals.Requery
End Sub

Answer : Working with recordsets

Sorry, I mis-understood what an assorment was.  There needed to be another loop when fetching data from tblAssortment.   Corrected code below.

Also, I would convert the subform for picking an assortment to a combo or list control.

JimD.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
Private Sub cmdInsertassmt_Click()
 
  Dim dbs As DAO.Database
  Dim rstForm As DAO.Recordset
  Dim rstData As DAO.Recordset
  Dim strSQL As String
 
  Set dbs = CurrentDb
 
  ' We need to look at the forms records, so get a reference to
  ' its recordset
  Set rstForm = Me!frmInternationalAssortments.Form.RecordsetClone
 
  ' Loop through the forms records.
  rstForm.MoveFirst
  Do Until rstForm.EOF
    ' Fetch data from tblAssortments based on the current form record.
    strSQL = "Select ProductID, Quantity FROM tblAssortments WHERE AssortmentID = '" & rstForm![AssortmentID] & "'"
    Set rstData = dbs.OpenRecordset(strSQL)
  
    Do Until rstData.EOF
      ' and now append record using data in rstData
      strSQL = "INSERT INTO [tblInternationalOrderDetails] " & _
                  "(OrderID, ProductID, Quantity) SELECT " & Me!OrderID & ", " & rstData!ProductID & ", " & rstData!Quantity * Me.txtDefaultQty & ""
      CurrentDb.Execute strSQL, dbFailOnError
      rstData.MoveNext
    Loop
    rstData.Close
    Set rstData = Nothing
 
    ' Move to next form record
    rstForm.MoveNext
  Loop
 
  Set rstForm = Nothing
  Set dbs = Nothing
 
  Me!sbfInternationalOrderDetails.Requery
  Me!sbfInternationalOrderDetails.SetFocus
  DoCmd.GoToRecord , , acNewRec
  Me!sbfInternationalTotals.Requery
End Sub
Random Solutions  
 
programming4us programming4us