Cheryl, Tell me if this is right:
You have a main form, the record source table is tblOrders and the PK of the table is the field OrderID - autonumber.
You have a sub form, the record source table is tblTagDetails and the PK of the table is the field TagNo. The field TagNo is type Double (defined in the table)
The tables are linked on tblOrders.OrderID - tblTagDetails.OrderID
You are NOT using tblTagDetails.TagNo to link to other tables. (NOTE: is is really a bad idea to use any type except a Long Integer to link tables.)
Can a main form record (1) have sub form records 1, 2, 3, 4
AND
main form record (2) have sub form records 1, 2, 3, 4?????
BTW, You are using a test (backup) dB to do this testing until the bugs have been worked out!!
Right?????
You need to get the max tag number, based on the main form PK (linking field).
OK, open the subform and remove "=GetNextTag()" from the control "TagNo" Default property.
Add code for the subform before update event:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.TagNo = GetNextTag(Me.Parent.OrderID)
End Sub
Change the GetNextTag code to:
Code:
Public Function GetNextTag(pOrder As Long) As Double
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim lsMaxTagNo As Double
Set db = CurrentDb
strSQL = "SELECT Max([TagNo]) AS MaxTagNo "
strSQL = strSQL & " FROM tblTagDetails"
strSQL = strSQL & " WHERE [OrderID] = " & pOrder & ";"
Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)
' calc next number
lsMaxTagNo = Nz(rst!MaxTagNo) + 1
'set return value
GetNextTag = lsMaxTagNo
'clean up
rst.Close
Set rst = Nothing
Set db = Nothing
End Function