View Full Version : Have record number in subform auto generate


karyng
12-07-2000, 06:37 PM
This should be extremely easy, but I cannot get it to work.

I have a main form where the header information of an order is stored. The order number is an AutoNumber. Then I have a subform with the detailed lines of the order. The first line for every order number should be 1. All additional lines should be 1 more than the last line.

Thanks in advance.

Fornatian
12-08-2000, 07:41 AM
I dealt with a similar problem a while back and used this code that you can manipulate.

On Error GoTo IanError
Dim rst As Recordset
Set rst = Me.RecordsetClone
Dim lngRecNo As Long
Dim lngPrimKey As Long
lngPrimKey = Me.PrimaryID
If rst.RecordCount < 1 Then
rst.AddNew
rst("PrimaryID") = lngPrimKey
rst("IncrementalNo") = 1
rst.Update
Else
rst.MoveLast
lngRecNo = rst("IncrementalNo") + 1

rst.AddNew
rst("PrimaryID") = lngPrimKey
rst("IncrementalNo") = lngRecNo
rst.Update
End If
Me.Requery
Exit_Ian:
Exit Sub
IanError:
MsgBox Err.Number & Err.Description
Resume Exit_Ian
End Sub

it's not as easy as it first seems because of the one-to-many relationshiop between the customer + orders table - orderno1 may be related to cust1 and orderno2 may be related to cust2,therefore the next orderno for cust1 will be cust3 not cust2 because it has already been used.

I'm still working on the code but you're welcome to my WIP.

Ian