How Can i assign a value to a filed that is primary key?

I have a form with 2 fields as primary key (filed1 and field2), field1 is a foreing key for my form master-detail and field2 is a field that count the records created.
For example:
Table Master
field1
1
2

Table Detatil
field1 field2
1 ------1
1 ------2
1 ------3
2 ------1
2 ------2

The question is how can i generate the value for field2 ? it is autoincrement for each field1 (i tried onbeforeinsertrecord) assign a value through of a function, but SC not alow do it.

Is it an issue of your database or Scriptcase? I do things like these a lot without problems. In the event onbeforeinsert I do a select max and increase the value. That I assign to the {field}. Nothing more to it. So there must be something different to your approach?