| |
August 26, 1999 | |
Maintaining the active record position when FINDing again in the same file; using the XFER command | |
TAS 4.0 and higher | |
Andrew J. Martineau (Copyright 1999 Addsum Business Software, Inc.) |
There are times when a programmer has an active record in memory but then for validation or other reasons needs to find a record within the same data file which will then cause the current record position to be lost. An example of this is DBA's BKARA (AR-A) program. When entering a customer and you get to the ship to field, there is a look-up and validation into the same file that already has an active record. How can you validate or provide a look-up without losing the fields in memory that have been entered so far?
If you just ignored the fact that you were doing a find in a file that you were just creating or editing a record in, you would lose all the information you just entered.
EXAMPLE: define bkarcust_hndl type i size 5 && file handle for bkarcust file. define ship_to type a size 10 && this ship-to field that has && to be a valid customer record. openv 'BKARCUST' fnum bkarcust_hndl lock r &&open BKARCUST. enter bkar.custcode &&Enter the customer code enter bkar.custname &&Enter the customer name enter ship_to vld chk_ship() &&Enter the ship to field now. { ;This function will check to make sure it's a valid customer record. func chk_ship ;This will compare the ship_to field to the bkarcust file and ;make sure it's a valid customer code. findv m fnum bkarcust_hndl key bkar.custcode val ship_to if flerr() <> 0 && didn't find a matching record. msg 'This is not a valid customer code! ' windows 'warn' novldmsg ret .f. endif ret .t. }
Doing this will cause you to lose the information you entered in the first two fields (BKAR.CUSTCODE and BKAR.CUSTNAME) and they will be replaced with the fields of the record associated with the ship to code.
Possible solutions:
define bkarcust_hndl type i size 5 && file handle for bkarcust file. define ship_to type a size 10 && this ship-to field that has && to be a valid customer record. define cust_rec_hold type a size 1 && this will be the variable that && we will hold the record in. openv 'BKARCUST' fnum bkarcust_hndl lock r &&open BKARCUST. enter bkar.custcode &&Enter the customer code enter bkar.custname &&Enter the customer name enter ship_to vld chk_ship() &&Enter the ship to field now. { ;This function will check to make sure it's a valid customer record. func chk_ship ;This will resize the variable we defined to the size of the ;whole unsaved record that you have been entering. alloc cust_rec_hold type 'A' size rsize(bkarcust_hndl) ;Now pushing it into the variable we just resized. xfer from BKAR.CUSTCODE to cust_rec_hold nchr rsize(bkarcust_hndl) saves findv m fnum bkarcust_hndl key bkar.custcode val ship_to if flerr() = 0 && found a record ship_to = bkar.custcode else ship_to = '' && if record isn't found, set it to nothing && so we know we didn't find it. endif xfer from cust_rec_hold to BKAR.CUSTCODE nchr rsize(bkarcust_hndl) ;now resizing the variable back down to size 1 to not take up ;so much space/resources alloc cust_rec_hold type 'A' size 1 ; redsp if ship_to = '' msg 'This is not a valid customer record' windows 'warn' novldmsg ret .f. endif ret .t. }
\homepage\techmemo\ad082699.htm