Multiuser examples

The following examples show how to write the scripts to handle basic multiuser scenarios. The first example shows how to read a record, the second shows how to save a record, and the third shows how to delete a record.

Example 1

The following is the Part Number change script that retrieves and actively locks a record. It uses the err() function to handle an error resulting from the record being actively locked by another user.

{Release the lock on any record currently in the table buffer.}
release table 'Inventory_Data';
{Set the value of the key field in the table buffer.}
'Part Number' of table Inventory_Data = 'Part Number' of window  'Inventory_Maintenance';
{Attempt to retrieve the record from the table and lock it.}
change table 'Inventory_Data' by Inventory_Data_Key1, lock;
if err() = OKAY then
	{If there was no error, the record was successfully read.} 
	copy from table 'Inventory_Data';
	clear changes form 'Inventory_Maintenance';
	{Enable the Delete button and lock the Part Number field.}
	enable 'Delete Button';
	lock 'Part Number';
elseif err() = LOCKED then
	{The record is actively locked by another user.}
	error "This record is currently locked by another user.";
	restart form;
else
	{The record is missing. The user must be entering a new
	record. Prevent the Part Number field from being modified.}
	lock 'Part Number';
end if;

Example 2

The following is the Save button change script. It uses the err() function to handle an error resulting from the record being changed or being actively locked by another user.

if required(form Inventory_Maintenance) then
	copy to table Inventory_Data;
	save table Inventory_Data;
	if err() = RECORDCHANGED then
		{The record was changed by another user.}
		warning "This record has been changed by another user. The record will be read again.";
		change table Inventory_Data;
		copy from table Inventory_Data;
	elseif err() = LOCKED then
		{The record is actively locked by another user.}
		warning "This record is currently locked by another user. Changes will not be saved";
	else
		{Save was successful.}
		restart form;
	end if;
else
	warning "Please enter all required fields.";
end if;

After the successful save operation, the save table statement automatically updates the fields in the table buffer that were changed by other users. This means that the values in the table buffer after the save operation may differ from the values in the table buffer before the save operation. After a successful save operation, you may want to update the data displayed in your application to reflect these changes.

Example 3

The following is the Delete button change script. It uses the err() function to trap and handle an error resulting from the record being actively locked by another user.

if ask("Delete this item?","Cancel","Delete","") = ASKBUTTON2 then
	remove table Inventory_Data;
	if err() = LOCKED then
		{The record is actively locked by another user.}å It can't be deleted.";
	elseif err() = MISSING then
		{The record has been deleted by another user. No need to 
		 delete record.}
		restart form;
	else
		restart form;
	end if;
end if;

When you’re writing scripts that handle errors using the err() function, you may want to use the check error statement as a debugging tool while you’re working. The check error statement will display a message indicating the type of table error that occurred.



Documentation Feedback