'Date Created: 27-Feb-2003 10:49:55 PM 'Last Updated: 02-Mar-2003 12:14:57 AM 'Created By : Cal 'Updated By : Cal FUNCTION Auto_incr_c AS C ( incr_table as C, fld_name as C, dflt_val="" as C ) 'Get a pointer to the field in this local table. tbl = table.current() fld = tbl.field_get(fld_name) Restart_here: 'Increment the field value. IF fld.value_get() = dflt_val 'The incr_fld_value() function returns the current Character value in "incr_table" and 'increments it by 1. If it is not able to open the "incr_table" it returns the original '"dflt_val" so it can be checked below. newval = incr_fld_value( incr_table, dflt_val ) fld.value_put(newval) END IF 'Not really necessary. Just allows the function to return something meaningful. Auto_incr_c = fld.value_get() 'If increment didn't succeed, allow trying again or cancelling. 'This allows for the possibility that someone is working directly on the table or it has 'been left 'permanently' open for some reason such as another computer crashing at the 'wrong time. IF fld.value_get() = dflt_val msg = "The master order number cannot be updated because the '"+incr_table+"' table " msg = msg + "is in use." +chr(13)+chr(13)+ "Someone else could be using the table " msg = msg + "or it could be locked due to a previous system crash somewhere on the " msg = msg + "network." +chr(13)+chr(13) msg = msg + "If nobody else is using the file, it may be necessary for everyone to " msg = msg + "shut down and re-boot. In extreme cases, it may even be necessary to " msg = msg + "re-boot the server." +chr(13)+chr(13)+ "To try again, click 'OK'. To " msg = msg + "quit and lose this record, click 'Cancel'." resp = ui_msg_box( "*** ERROR - Record NOT saved ***", msg, 16+ui_ok_cancel ) IF resp = ui_ok_selected GOTO Restart_here END IF 'This seems to work even *after* the message box. It didn't in version 4. cancel() END END IF END FUNCTION FUNCTION Incr_fld_value AS C ( tablename AS C, init_val as C ) 'Description:Returns current value in field 1 of 'tablename' and increments it by one. Used to implement xbasic autoincrement in CanSave Record event. DIM curval as C 'If exclusive access isn't obtained, this returns the initial value so 'the field value won't be changed. Add'l checks will be run in the main function above. Incr_fld_value = init_val error_loops = 0 ON ERROR GOTO Not_exclusive tp = table.open(tablename,file_rw_exclusive) ON ERROR GOTO 0 tp.fetch_goto(1) fld = tp.field_get(1) 'Get a pointer to the first field in the record. curval = fld.value_get() newval = increment_value(curval) 'Store the next value in the increment table. tp.change_begin() fld.value_put(newval) tp.change_end() tp.close() Incr_fld_value = curval EXIT FUNCTION Not_exclusive: 'Give it 3 tries in case someone else is entering a record at exactly the same time. IF error_loops < 3 error_loops = error_loops + 1 RESUME 0 END IF END FUNCTION