Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Autonumber data type

Status
Not open for further replies.

gambhir

Programmer
Oct 17, 2000
15
US
I know that in Access you can use the autonumber feature as a datatype. I am a relatively new user to Foxpro and would like to know how to use a similar feature in this environment. I want my Foxpro table to add a unique ascending number every time a user fills in a web form and it is submitted for an add to the database. [sig][/sig]
 
Use integer field. As the default value for that field use stored procedure (function call). In the functiion you will need to write code to generate unique key. You may use any way you want for generator. Usually, I have a table that stores last ID number for each table and incremented for each new record insertion by function. My default value for key field looks like
GetNextID("MyTable")

If you use views, however, you will need to use the same function for default value for key field in view, and make key field updatable.
[sig]<p>Vlad Grynchyshyn<br><a href=mailto:vgryn@softserve.lviv.ua>vgryn@softserve.lviv.ua</a><br>[/sig]
 
Here is a proceedure I use. You may want only to return the next number in the sequence as Vlad suggested. the code below updates the new record for you

*sample
create table &quot;c:\vfp\temp.dbf&quot; (uid n(9,0), text c(10))
APPEND BLANK
pUniqueID('temp','uid',RECNO())



PROCEDURE pUniqueID (cTable, cField, nRec)
IF nRec < 1 THEN
RETURN
ENDIF
IF TYPE('&cTable..&cField')='U' THEN
RETURN
ENDIF
cTalk = SET('TALK')
cCurrentArea = ALIAS()
SELECT &cTable
SET TALK OFF
CALCULATE MAX(&cField) TO nMax
GO nRec
REPLACE &cField WITH nMax+1
SELECT &cCurrentArea
SET TALK &cTalk
ENDPROC
[sig]<p> Pete<br><a href=mailto:blindpete@mail.com>blindpete@mail.com</a><br><a href= > </a><br>What your mother told you is true! You will go blind! (from moonshine anyway)[/sig]
 
Pete, your routine will work slowly with large data sets. In multi-user environment some other user might add record with the same ID during that time. Well, you can lock table for that time, but this is not good idea for large table, indeed.
See FAQ section, I gonna add my routine there. [sig]<p>Vlad Grynchyshyn<br><a href=mailto:vgryn@softserve.lviv.ua>vgryn@softserve.lviv.ua</a><br>[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top