Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
In a multi user environment
Message
From
23/10/2002 09:01:18
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
 
To
23/10/2002 08:30:40
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00714293
Message ID:
00714308
Views:
14
>Hello all
>
>In a multi user environment is it a good way to generate the auto number when the user clicks add or to generate it when the user clicks save.

Save the numbers to a file. Assign the auto-number through a function, called from the field's default value. There are downloads, in the download section, that help you do this. I include my version at the end.

The field's default value is good if you don't mind skipping numbers - no problem for PKs which the user doesn't see. (If the user creates a record and then issues TableRevert(), the number won't be used again, and will be skipped in the sequence. Enough numbers are available - an integer can have numbers up to 2e9.)

But if you need document numbers which the user does see, you have to assign the number only when the user saves, and use a transaction.
**********************************************************************
FUNCTION SerialNumber(tcSequence)
	* Get serial number. Used mainly to generate primary keys.
	* The easiest way to achieve this is to call this function from a fields default value.
	* Example: default value for primary key "Client" of table "Client" = SerialNumber("Client")
	* This function accesses table SerialNumber. Fields: Sequence C(30), NextNum I.
	tcSequence = lower(tcSequence)
	local lnSelect
	lnSelect = select()
	if used("serialnumber")
		select serialnumber
	else
		select 0
		use serialnumber
	endif
	set order to "sequence"
	seek padr(tcSequence, len(sequence))
	if not found()
		append blank
		replace sequence with tcSequence, nextnum with 1
	endif
	local lnReturnValue
	if lock()
		lnReturnValue = nextnum
		replace nextnum with nextnum + 1
	else
		lnReturnValue = -1
	endif
	unlock
	select (lnSelect)
	return lnReturnValue
ENDFUNC
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform