Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Need help with a primary key
Message
 
 
To
08/06/2001 13:45:26
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00517080
Message ID:
00517087
Views:
16
>Does anybody know of a way to automatically increment your primary key in a table being created in Visual FoxPro?
>
>My goal is to have my user "skip" the field automatically. A new number would be added to the field each time they entered new records in my other fields.
>
>Thanks,
>
>John York

Usually auto-increment feature is done by using default value for this field in database. In other words, say, your table has ID field called PropID. Your table is called PropMstr.
In default value for PropID field you would have lNextID('PropMstr'), where lNextID is a SP (or program, which is always availalbe).

Here is the lNextID program sample (not the best).
*!*****************************************************************
* PROGRAM     : LNEXTID.PRG
* AUTHOR     : DOUGLAS N. GRAY
* DESC          : Unique id generator.
* PARAMETER     : <None>
* CREATED     : 02/20/96
* MODIFIED     : 11/12/1999 - Changed PRIVATE to LOCAL, changed field names.
* MODIFIED  : Nadya Nosonovsky 08/22/2000 12:38:41 PM
*!*****************************************************************
procedure LNextID
lparameters tcalias
local lnoldarea,lnnextid, lcDBC, lcOldSetDBC, llOpened
llOpened=.t.
*--- Variable Setup
tcalias = iif(empty(m.tcAlias),upper(alias()),upper(tcalias))
lnoldarea = select()
lnnextid = 0
lcDBC=cursorgetprop('database',tcalias) && Return the name of owning database
if empty(lcDBC)
     =messagebox('You can not assign Next ID to the free table!',16,'Warning')
     return .f.
endif
lcOldSetDBC=set('database') && Save current status
if !dbused(lcDBC)
     open data (lcDBC)
     llOpened=.f. && Was not open before
endif
set database to (lcDBC) && Make this DBC current
*--- Go to NextID Database to assign the next unique ID
*--- Assumes the you are sitting in the correct DBC.
if !used('NEXTID')
     use NEXTID in 0
     select NEXTID
else
     select NEXTID
endif
set order to table     && NOTE: Index expression = UPPER(table)

if !seek(tcalias)
     insert into NEXTID (table,id) values (tcalias,1)
     lnnextid = 1
else
*--- RLOCK will attempt the lock indefinately, because it would be bad to return 0 (the alternative).
*--- User will be able to cancel attempt if in a deadlock by pressing the ESC key.
     if rlock()
          replace NEXTID.id with NEXTID.id + 1
          lnnextid = NEXTID.id
          unlock
     else
          lnnextid=0
     endif
endif

*--- Return to prior workarea.
select (m.lnoldarea)
if not llOpened
     close database
endif
if !empty(lcOldSetDBC)
     set database to (lcOldSetDBC)
endif
return lnnextid
In the same database there is a table called NextID with two fields: Table and ID. It has an index on upper(Table).

Also take a look at Craig Bernston site, there this problem is discussed.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform