When the solution matters

Tips...

Registration Documentation Knowledgebase Seminars / Training Technical Support Partner Central

Surveys

If you are new to databases, you might want to make sure your seatbelt is tighly fastened before you proceed. We are going to get into a bit of the mechanics of how a database works. In a database, there exists something called a "Primary Key." It is a value which is unique for a table. For instance, if you had a [US_Citizens] table, you might have the Primary Key field for that table be the Social Security number, since this is supposed to be unique for every citizen.

The operative word in the last sentence is supposed. In fact, there are a few duplicated social security numbers out there. So if that's the case, how do you uniquely identify a record? Well, you could do all kinds of clever things to distinguish one record from another. But "clever" is usually a bad thing when it comes to database design. So the simple thing to do is to just add a "Primary Key" field and have it be a number. Just assign a new number to it for each new record is created. Keep incrementing that number for each new record, never decrement it - and the number will be unique (for the table).

For this reason, the first field of the Surveys table is called "ID" - as is the first field of the "Answers" table. When records for these tables are created, a unique number will be entered into the ID field. How will that happen? We will write a method to do that. So! Back in 4D's Design Environment, create a new Project Method and call it "LDB_TriggerMethod." Type into this method the following:

C_POINTER($1;$pKeyField)
C_LONGINT($0)

$0:=0  `assume success

Case of
: (Count parameters=0)
$0:=-15001
Else

C_LONGINT($iEvent)
C_POINTER($pTable)

$pKeyField:=$1
$pTable:=Table(Table($pKeyField))
$iEvent:=Database event

If ($iEvent=On Saving New Record Event ) | ($iEvent=On Saving Existing Record Event )

C_LONGINT($iType)

$iType:=Type($pKeyField->)

Case of
: ($iType#Is LongInt )
: ($pKeyField->#0)
Else
$pKeyField->:=Sequence number($pTable->)
End case

End if

End case

screenshot

When you are done, your LDB_TriggerMethod should look like the one in the picture above.

Save the project method by closing it.

 Previous Step   1 2 3 4 5 6  Next Step


International | Company | Contact 4D | Site Map | Privacy Policy | © 4D UK 1995-2008 | Change font size: [A] [A] [A] | Print this page