Technical support memos

ADDSUM TECHNICAL SUPPORT MEMO

Date:
August 26, 1999
Subject:
Maintaining the active record position when FINDing again in the same file; using the XFER command
Product:
TAS 4.0 and higher
Authors:
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:


  1. Temporary variables. This is a good bulletproof way of going about solving this, by setting up a temporary field for each of the matching fields in the file you are doing a second find in. However, this could cause you to allocate more memory than is necessary, and could slow down programs depending on how large the file is and could involve tedious programming if the field descriptor.
  2. Saving the record before the find. You could save out the information that you have already entered into the file, then save the customer code to a temp variable and do a find on it after working with the ship_to field, but if you choose to back out of entering the record, you would then have to go through the trouble of deleting the record that you just backed out of.
  3. Putting the whole record into a field via the xfer command. This also involves using a temporary variable for the record, but instead of defining many temporary variables that would take space during the entire program, you can use the alloc command to resize the varable accordingly.
  4. Use the IFDUP command. This could help in a situation where you are validating a field. It would not be a solution when you are wanting to LIST records from the same file layout in different fields in the same program. Further, the IFDUP command has proven to be unreliable (and we accordingly never use it).

    Method Three is recommended. It is the most simple and takes care of everything you need to take care of in one spot of code, and with much less variable field usage. An example showing how it can be used follows.

    
           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.
             }
    
Copyright © 1999 Addsum Business Software, Inc.
ADDSUM is a registered service mark of Addsum Business Software, Inc.
Advanced Accounting and TAS are trademarks of Business Tools, Inc.
Technical support phone number: 801-277-9240

\homepage\techmemo\ad082699.htm