Derek said:
How do I insert records into two tables one table is the
main form and the second is a subform table as in orders
and items. I want to be able to automatically place a
repeat order without having to do all the detail work
I am assuming that if I choose the main form record first
and append the record to its underlying table, that the
new record will generate a new number in the counter field
of the main form table. As this is also the referencial
integrity linking field to the subform table when I append
records from the subform it needs the new linking field
number in the main table to allow the appending of new
records to the subform table. how will it know the number
of the linking field? Am I missing something simple?
Add the parent record via a recordset so that you can get the autonumber
ID for the added record, then use an append query to copy the records
from the child table, specifying the new number as a literal value.
Here's a fairly flexible routine for the purpose:
'----- start of code -----
Private Sub cmdDupAll_Click()
Dim varOldMainKey As Variant
Dim varNewMainKey As Variant
Dim fld As DAO.Field
Dim strSQL As String
Dim strFieldList As String
Dim strLinkMaster As String
Dim strLinkChild As String
strLinkMaster = Me.tSub.LinkMasterFields
If Left(strLinkMaster, 1) = "[" Then
strLinkMaster = Mid(strLinkMaster, 2, Len(strLinkMaster) - 2)
End If
varOldMainKey = Me.Controls(strLinkMaster).Value
strLinkMaster = Me.Controls(strLinkMaster).ControlSource
strLinkChild = Me.tSub.LinkChildFields
If Left(strLinkChild, 1) = "[" Then
strLinkChild = Mid(strLinkChild, 2, Len(strLinkChild) - 2)
End If
' Build a list of the subform fields to be copied.
' This list must exclude the LinkChildField and the
' table's autonumber key.
For Each fld In Me!tSub.Form.Recordset.Fields
If fld.Name = strLinkChild _
Or (fld.Attributes And dbAutoIncrField) <> 0 Then
' skip this field
Else
strFieldList = strFieldList & ", [" & fld.Name & "]"
End If
Next fld
' Copy the main form's record, noting the new ID
With Me.RecordsetClone
.AddNew
varNewMainKey = .Fields(strLinkMaster).Value
For Each fld In Me.Recordset.Fields
If fld.Name <> strLinkMaster Then
.Fields(fld.Name) = fld.Value
End If
Next fld
.Update
End With
' Build a SQL statement to copy the related records.
Select Case VarType(varNewMainKey)
Case vbString
varOldMainKey = _
"""" & _
Replace(varOldMainKey, """", """""", , , _
vbBinaryCompare) & _
""""
varNewMainKey = _
"""" & _
Replace(varNewMainKey, """", """""", , , _
vbBinaryCompare) & _
""""
Case vbDate
varOldMainKey = Format(varOldMainKey, "\#mm/dd/yyyy\#")
varNewMainKey = Format(varNewMainKey, "\#mm/dd/yyyy\#")
Case Else
varOldMainKey = Str(varOldMainKey)
varNewMainKey = Str(varNewMainKey)
End Select
strSQL = _
"INSERT INTO " & Me.tSub.Form.RecordSource & " " & _
"([" & strLinkChild & "]" & strFieldList & ") " & _
"SELECT " & varNewMainKey & strFieldList & _
" FROM " & Me.tSub.Form.RecordSource & _
" WHERE [" & strLinkChild & "] = " & varOldMainKey
CurrentDb.Execute strSQL, dbFailOnError
Me.Requery
Me.Recordset.FindFirst "[" & strLinkMaster & "] = " & varNewMainKey
End Sub
'----- end of code -----
You don't have to be quite this flexible if you don't want to; you can
hard-code field and table names if you want.