11. Adding and Updating Data

Changing Field Values from a Container

To update any field or group of fields in a database record from the contents of a single container in the scripting environment, use the FileFlex DBWriteRec function with three arguments.

The first argument is any character other than G,E, B or C (or their lower-case equivalents).

The next two arguments are the database record number and the record's new value. The new field value(s) must be stored in a field or variable with a separate line for each field being updated. Each of these lines will have two comma-delimited values, the first representing the name of the field and the second containing the field's new value.

Unmodified fields (i.e., those whose names do not appear in the container) are left as they were before the update process started. For example, to change the marital status of an employee from single to married and his number of dependents from zero to two, you might store the following information in a variable called newData:
  MStatus,M
  Dependents,2
and then write a line something like this:
  put DBWriteRec("L",12,newData) into dbResult
DBWriteRec also has the ability to save data and encrypt it. See the chapter on Office Quality Encryption for details.

Changing a Memo Field from a Container

You may completely replace the contents of a given memo field with the contents of a container (typically a field which the user has edited) by using the FileFlex DBWriteMemo function, supplying the database field name and its new value. The new value can be text or the name of a container holding the text.

Assume you've had the user provide some additional information for the Notes field in the current database record, and that you've stored the new memo field in a background field called "New Notes." You can update the information in the database with a line like this:
  put DBWriteMemo("NOTES",field "New Notes") into dbResult
DBWriteMemo also has the ability to save data and encrypt it. See the chapter on Office Quality Encryption for details.

Changing a Field's Value from Global Variables

FileFlex has the ability to place the contents of global variables into corresponding FileFlex data file fields. If a matching global variable doesn't exist, the field will not be updating.

Note: This also implies that if the variable exists, it will be written. Therefore, make sure the contents of all applicable global variables are kept up to date.

Use DBWriteRec by using the option character "G", as in the following example:
  put DBWriteRec("G") into DBResult

Updating from a Template Card

If you use HyperCard or SuperCard, you can use this feature. If you name the fields in your scripting environment to correspond with the names of the database fields, you can perform a record update from the contents of a card with a single call to the function DBWriteRec. In this case, the function requires only two arguments rather than the three we needed above.

The first is the letter "C" or "c", if you want FileFlex to take data from the card fields on the card involved, or the letter "B"or "b" if you want FileFlex to take the fields' new values from background fields.

The second is the number of the record to be updated.

FileFlex will only update fields in the database that have corresponding fields in the card involved in the update.

Assume that record 14 of a database file contains these fields and values:
  Name     C J Nielsen
  Salary   65000
  Dept     13B
  Jobcode  5505.12
If you have a card with background fields named Name, Salary, and Jobcode and you put the name "C J Nielsen" into the first field, a value of "67500" into the second, and a value of "3918.2"into the third, you can update the salary and jobcode fields with this command:
  put DBWriteRec("C",14) into dbResult
with the card containing the correct values being the current card at the time of the update. After that command executes, record 14 looks like this:
  Name     C J Nielsen
  Salary   67500
  Dept     13B
  Jobcode  3918.2

Adding a New Record to the Database

To add a record to the current database file, you may use any of the commands that update a record (as described above) and supply a record number that is at least one higher than the number of records in the database. You can find out how many records are in the database with the DBCount function. For example, if you want to use DBWriteRec to add a new record to the database from the card fields on the current card, you would write a script like this:
  on mouseUp
    -- update happens in response to mouse-click on a button
    put DBCount() into numRecs
    add 1 to numRecs
    put DBWriteRec("C" numRecs) into DBResult
  end mouseUp
It is not essential that you do this, however. If you use an arbitrarily large number for the record number, FileFlex will add the new information to the end of the file, if that number is at least one larger than the total number of records in the file.

Multiple Database Update Technique

You can use the DBWriteRec with the G,B, or C option to update more than one open database file from the same card containing new information. Because DBWriteRec ignores all globals or fields that are invalid for the current database, only those fields that are found in the current database will be udpated when you use one of these command formats.

Thus if you had information stored in globals that belonged in three different databases, you would simply use a DBWriteRec() call for each database in turn, something like this:
  -- Assume DB opened earlier
  put DBSelect(partList) into dbResult 
  put DBWriteRec("G",32) into dbResult -- updates Record #32

  -- Assumes we opened DB earlier
  put DBSelect(openItemFile) into dbResult 
  put DBWriteRec("G",11) into dbResult -- updates Record #11
  put DBUse("BACKORDERS") into dbResult -- opens a database

  -- get total number of records in file
  put DBCount() into numRecs 

  -- add record at end of file
  put DBWriteRec("G",numRecs+1) into dbResult 
Each of the three databases would be updated with the information in global variables whose names exactly matched fields contained in that database.



  [Previous Chapter]    [Table of Contents]    [Next Chapter]


Copyright (c) 1996 David Gewirtz under license to Component Software Corp. All rights reserved worldwide.