ABProgram.tpw
#IF((EXTRACT(UPPER(%FieldUserOptions), UPPER(‘IsIdentity’),1) = ‘1’ OR UPPER(EXTRACT(UPPER(%FieldUserOptions), UPPER(‘IsIdentity’),1)) = ‘TRUE’) OR EXTRACT(UPPER(%FieldUserOptions), UPPER(‘ServerAutoIncColumn’),1) <> ‘’)
! EmulateAutoNumKey, this will force the FM to auto insert the record and
! recover the Identity column
IF UseType = UseType:Initialize AND ~SELF.Initialized
SELF.HasAutoInc = True
END
#BREAK
#ENDIF
#IF((EXTRACT(UPPER(%FieldUserOptions), UPPER(‘IsIdentity’),1) = ‘1’ OR UPPER(EXTRACT(UPPER(%FieldUserOptions), UPPER(‘IsIdentity’),1)) = ‘TRUE’) AND %FieldType<>‘GROUP’)
#IF(%FileIdentityFieldInstance=0)
#SET(%FileIdentityField,%Field)
#SET(%FileIdentityFieldInstance,%lFileCount)
#SET(%FileIdentityFieldColumn,1)
#ELSE
#ERROR(‘Only one Identity column per File is allowed - Second instance (File:’&%File&’ - Column:’&%Field&’)’)
#ENDIF
#ENDIF
Field User Option
IsIdentity = TRUE
is the same as
FileLabel{PROP:ServerAutoInc, FieldNumber} = 1
FileLabel{PROP:ServerAutoInc, FieldNumber} = True
You can only have one field which is autoincremented by the MS SQL table using this method.
Just dont forget to make the field in the MS SQL table, auto incrementing!
Now as a habit, with MS SQL I would create an auto incrementing field for each table. This becomes my entry order index because I never delete a record from MS SQL, I set other fields to indicate its been deleted and then its not shown in the app. Not GDPR compliant but…
I would then use Clarion to do the auto incrementing for other fields because something Clarion can do out the box which MS SQL cant do unless you write a stored procedure, is have a two or more field element key with the auto incrementing taking place on the last field. The first fields would be related to another file/table or prefilled with a value.
To do this, I edit
abFile.inc
HasAutoInc BYTE,PROTECTED ! Auto-increment or not flag
changed to
HasAutoInc BYTE !,PROTECTED ! Auto-increment or not flag
I then have a template which sets HasAutoInc to zero, so that autoinc fields are only incremented at the time the record is saved. They are not primed before then.
To do this in code just add the following to the embed Brwx.Ask Priority 2501
IF Request = InsertRecord
Access:%AutoIncFile.HasAutoInc = 0 !generated by IS_HasAutoIncBrowse procedure extension template
END