KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Assigning an automatic sequence number when adding a subrecord in a subform
PRODUCT: 4D | VERSION: 6.5 | PLATFORM: Mac & Win
Published On: August 10, 2001
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

It is true that you can assign a sequence number to a field with #N (set to the Default Value option in the Property List or Object Properties) when you are creating a new record. However, when you are creating a subrecord in the subform, 4D automatically creates and saves a blank record. What you are doing is basically modifying a blank record. Since 4D assigns 0 to all numeric fields as a default value and #N can only be used with the record that has not been saved to the disk, adding a subrecord will always display 0 as the current value. To solve this problem, you can add the following code to the object method of the sequential field of the subform.

` Object Method: sequential field
Self->:=Sequence Number([RelatedTable])

Note: If the same list form is also used as an output form, you should modify your database to the following to prevent all records from being displayed with the same sequence number when you open the form as an output form.

1) Create a Boolean variable in the On Startup Method.

` On Startup Method
C_BOOLEAN(vIsNew)
vIsNew:=False

2) Create an Object Method for the sequential field in the subform.

` Object Method: sequential field
If (vIsNew)
Self->:=Sequence Number([RelatedTable])
vIsNew:=False
End if

3) Create an Object Method for the Add Subrecord button.

` Object Method: Add Subrecord button
vIsNew:=True