Comments and corrections inline ...
John B. said:
Thanks for responding. It's quite possible I'm going about this the
wrong way - I've only been doing this for a couple of months,
self-taught. I hadn't thought of using append queries, but that
might be easier. This method seems to work fine for creating the
records in the header table, I'm just having problems with the detail
records. Here is the code I am using. It's in a class module
attached to the form, called from the click event of a command
button. It doesn't seem to recognize the textbox controls on the
subform, so maybe I'm referring to them incorrectly. I've tried a
couple of different ways.
I don't see a problem there, but we'll see.
I'm not sure I'm moving the focus from the
form to the subform and back again correctly, either.
That you weren't.
Also, the
subform control on the main form, and the form that it refers to have
the same name currently (fsubSampleOrderDtl) - would renaming one of
them help?
No, that's no problem, and in fact I usually set it up that way so I
don't get confused and inadvertently use the SourceObject name instead
of the control name.
Although I do think I'd use append queries instead of looping through
and manipulating the form and subform, I've noted some corrections to
the code below, inline, that should either make it work or at least
bring it a lot closer.
Private Sub CreateOrders()
Dim Db As Database
Dim strSeason As String
Dim strSQL As String
Dim rsReqs As Recordset
Dim rsSku As Recordset
**INSERT**
Dim qdfSku As QueryDef
**END INSERT**
'Set Database and current season.
Set Db = CurrentDb
strSeason = Me.cboOrderSeason
'Create recordset of Sample Requirements for current season.
strSQL = "SELECT tblSampleReq.* FROM tblSampleReq WHERE
(((tblSampleReq.Season)='" & strSeason & "'));" ** DELETE **
Set rsReqs = Db.CreateQueryDef("", strSQL).OpenRecordset
** END DELETE **
**INSERT**
Set rsReqs = Db.OpenRecordset(strSQL)
**END INSERT**
'Loop through Sample Requirements recordset.
Do While rsReqs.EOF = False
'Create order header.
**DELETE**
If Me.NewRecord = False Then DoCmd.GoToRecord acDataForm, Me,
acNewRec
**END DELETE**
**INSERT**
Me.txtOrderName.SetFocus
If Me.NewRecord = False Then
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
End If
**END INSERT**
Me.txtOrderName = rsReqs("Name")
Me.txtOrderSeason = strSeason
Me.txtOrderStage = rsReqs("Stage")
Me.txtOrderDate = Date
Me.txtOrderLocID = "XXX"
'Set parameters and open Sku list. **DELETE**
With Db.QueryDefs("qfrmCSOSkuSet")
**END DELETE**
**INSERT**
Set qdfSku = Db.QueryDefs("qfrmCSOSkuSet")
With qdfSku
**END INSERT**
.Parameters("SelSeason") = strSeason
.Parameters("SelChannelSet") = rsReqs("ChannelSet")
.Parameters("SelGenderSet") = rsReqs("GenderSet")
Set rsSku = .OpenRecordset
End With
**INSERT**
' Transfer focus to subform
Me.fsubSampleOrderDtl.SetFocus
**END INSERT**
'Loop through Sku list.
Do While rsSku.EOF = False
'Create detail records
With Me.fsubSampleOrderDtl.Form **DELETE**
If .NewRecord = False Then DoCmd.GoToRecord , ,
acNewRec
**END DELETE**
**INSERT**
DoCmd.GoToRecord , , acNewRec
**END INSERT**
.txtDtlOrderID = Me.txtOrderID
.txtDtlSku = rsSku("Sku")
.txtDtlQty = rsReqs("QtyLeft")
.txtDtlDimension = "L"
End With
**DELETE**
DoCmd.GoToRecord , , acNewRec **END DELETE**
rsSku.MoveNext
Loop
'Close sku list and go to next header record.
rsSku.Close
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
rsReqs.MoveNext
Loop
rsReqs.Close
**INSERT**
Set rsSku = Nothing
Set rsReqs = Nothing
Set qdfSku = Nothing
**END INSERT**
I *think* those changes should make it work. If these lines:
.txtDtlOrderID = Me.txtOrderID
.txtDtlSku = rsSku("Sku")
.txtDtlQty = rsReqs("QtyLeft")
.txtDtlDimension = "L"
don't work, try replacing the dots (.) with bangs (!).