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

Status
Not open for further replies.

lashwarj

IS-IT--Management
Nov 1, 2000
1,067
US
I have noticed there is no function to create an autonumber. Is there any way to create an easy process.

ex = lost focus (field.data + 1)
 
UDF = User Defined Function

It might look something like...

*want the next autonumber
nNewAutoNumber = GetAutoNumber(ALIAS(),"KeyField")

PROCEUDRE GetAutoNumber
PAREMETERS cAlias, cKeyField
SELECT 0
SELECT MAX(&cKeyField) FROM &cAlias
uReturnValue = MAX_&cKeyField + 1
use
RETURN uReturnValue
ENDPROC

This needs more enhancements for multiuser system but it give you enough to get the idea. -Pete
 
VFP doesn't currently have this feature (although it was demonstrated as a possible new feature in Toledo - VFP 8.0!) See the sample code in Tastrade to for one implementation [ DO (_samples+"\tastrade\tastrade") ]. Or go to Craig's site ( and read the article on "Primary Keys".

Rick
 
If in multi-user scenario, you'll need to temporarily perform an RLOCK on the lookup table that holds the sequence counter to ensure that other users don't get the same number.

My adaptation:

FUNCTION NewKeyID
LPARAMETER tcAlias
* tcAlias is the name of the table which needs a unique key value

LOCAL lcAlias, lcID, lcOldReprocess, lnOldArea

lnOldArea = SELECT()

IF PARAMETERS() < 1 then
lcAlias = UPPER(ALIAS())
ELSE
lcAlias = UPPER(tcAlias)
ENDIF

lcID = &quot;&quot;
lcOldReprocess = SET('REPROCESS')

*-- Lock until user presses Esc
SET REPROCESS TO AUTOMATIC

IF NOT USED(&quot;SETUP&quot;) then
USE lookupdb!setup IN 0 shared
endif

IF SEEK(lcAlias,&quot;setup&quot;,&quot;keyname&quot;) then
IF RLOCK(&quot;setup&quot;) then && will wait for lock if necessary
lcID = setup.keyvalue
REPLACE keyvalue WITH (lcID + 1) IN SETUP
UNLOCK in &quot;setup&quot;
ENDIF
else
messagebox(&quot;Cannot find &quot; + lcAlias + &quot; in LOOKUPDB!SETUP table for unique key generation.&quot;,16,&quot;Unique key generation problem.&quot;)
ENDIF

SELECT (lnOldArea)
SET REPROCESS TO lcOldReprocess

RETURN lcID

ENDFUNC && NewKeyID

HTH,
--Michael

Michael J. Babcock, MCP
Founder, Central PA Visual Foxpro Users Group
mbabcock@cpvfug.org
&quot;Work smarter, not harder.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top