Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Deleting
Message
From
27/11/1999 01:05:19
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
00295796
Message ID:
00295921
Views:
24
Jim,

Or, go over to Rick Strahl's website, swipe his CoCreateGUID wrappers and to hell with it. Unique every time! (Big, though). ;-)


>Autogenerating Primary keys from the same table is a bad idea. It will give you many headaches over time. Here's a simple approach to generating unique values for the PK for a table.
>
>Creat a table named SysKeys.dbf with the following structure;
>
>
>Field            Type         Width
>TableName          C            40
>LastID             I
>
>
>Now use this code to create a new value;
>
>
>PROCEDURE GetNewKey( pcTable )
>LOCAL lcAlias, liNewID
>* Same current work area
>lcAlias = ALIAS()
>IF NOT USED( "Syskeys" )
>   * If syskeys is not open
>   USE SysKeys ALIAS Syskeys AGAIN IN 0
>ENDIF
>* Select the syskeys table and set the index order
>SELECT Syskeys
>SET ORDER TO TableName
>
>* Find the record for the table requested
>SEEK pcTableName
>IF NOT FOUND()
>   * If there is no record for the table, make one
>   APPEND BLANK
>ENDIF
>* Lock the record so no one else can get an id while you are in process
>DO WHILE NOT RLOCK()
>ENDDO
>* Increment the lastKey field to the variable
>liNewID = LastID + 1
>* Update the record with the newly assigned ID
>REPLACE TableName WITH pcTable, ;
>        LastID    WITH liNewID
>* Unlock the record to let others in
>UNLOCK
>* Restore the work area
>IF NOT EMPTY( lcAlias )
>   SELECT ( lcAlias )
>ELSE
>   SELECT 0
>ENDIF
>* Return the new ID
>RETURN liNewID
>
>
>You can call this function from the Default Value property of the Primary key field in the database as; GetNewKey( "TheTablesName" ).
------------------------------------------------
John Koziol, ex-MVP, ex-MS, ex-FoxTeam. Just call me "X"
"When the going gets weird, the weird turn pro" - Hunter Thompson (Gonzo) RIP 2/19/05
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform