FILEFLEX PROGRAMMER MANUAL


CHAPTER 10
Adding and Updating Data

In this chapter, you'll learn how to add new information to a FileFlex database, and update already-existing information in specific records and fields. The primary function for updating and changing data (i.e., writing data) is DBWriteRec.

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 the command code, and to update data from a container, use the character "A".

The next two arguments are the database record number and the record's new value. The set of new field values must be stored in a container 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 (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
DEPENDENT,2

and then write a line something like this:

put DBWriteRec("A",12,newData) into dbResult

This set of field and contents pairs works for all types of fields except large text fields such as memo fields. To update memo field contents, use the DBWriteMemo function.

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 castmember 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.

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. It's best to get the value of all fields into globals using DBGetCurrRecVal prior to changing a global's value. This is because FileFlex will write back the entire record and if a global is not defined for a given field, it will write an empty value into that field.

Use DBWriteRec with the option character "G", as in the following example:

global name
put DBGetCurrRecVal("G") into DBResult
put "COSTELLO" into name
put DBWriteRec("G",15) into DBResult

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 exactly 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

Multiple Database Update Technique

You can use the DBWriteRec with the G option to update more than one open database file from the same screen 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 updated 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.


Discuss this chapter on the FileFlex Boards.


Copyright © 1992-1998 by David Gewirtz, under license to Component Enterprises, Inc.
Contact: info@component-net.com

Casa de Bender