ebmDevMag.com home
ebm Developer's Magazine
links

developer's mag
main page

article
part 1
part 2
part 3
part 4
part 5
part 6
part 7
part 8


7 - Accessing Records

If you look at the sample code, you'll notice that the database and record functions are not part of the CWindow1 class. The reason is that I wanted them to be somewhat portable, so they could be reused in other code. However the DisplayRecord() is part of the class, since the data in the database needs to be converted to an edit field, which is also part of CWindow1. To display the fields, we create a function called RecordCopyToArray() which copies the field entries into a char pointer array passed to the function:
  DBErr RecordCopyToArray(int recNo,char**array)
  {
    char *ptr=0;
    CFMRecord record(&m_fMgr);
    DBErr error=record.Open(recNo); // open record 'recNo'
    if ( error )
      return error;
    int totalField=m_fMgr.GetNumFields();
    for (int fieldNo=0;fieldNo<totalField;fieldNo++)
    {
      // get pointer to each field
      record.GetField(fieldNo,(U8**)&ptr);
      // copy to a string on the array
      array[fieldNo]=new char[strlen(ptr)+1]; 
      strcpy(array[fieldNo],ptr);
    }
    return error;
  }
From there, the program determines what to do with the array of char strings; in this case, the three fields are copied into CTextEdit objects and displayed.

Another necessary function is the reverse, moving an array of data back into a record. The 'Save Current Record' is a variation of our data adding routine, since we aren't appending, but rewriting. For that, we follow the same steps, except we do not use CFMRecord::New() to create a new record, but CFMRecord::Open() with a record index to open a current one:
  DBErr RecordCopyFromArray(const ebo_name_t*fileName,int recNo,char**array)
  {
    CFMRecord record(&m_fMgr);
    DBErr error=record.Open(recNo);
    if ( error )
      return error;
    int totalField=m_fMgr.GetNumFields();
    for (int fieldNo=0;fieldNo<totalField;fieldNo++)
    { 
      // copy field, and check with OS if we need room
      if ( DB_OUT_OF_MEMORY==record.PutField(fieldNo,(U8*)array[fieldNo]))
      {
        GUI_NeedFileSpace(fileName);
        error=record.PutField(fieldNo,(U8 *)array[fieldNo]);
        if ( DB_OUT_OF_MEMORY==error )
          return error;
      }
    }
    return error;
  }
To wrap up, we have several other buttons: 'Delete Current Record' is a small wrapper around the COpenDB::DeleteRecord() call; 'Add As New Record' simply copies the current display into a new record (rather than reusing the current one), with the coding therefore very similar to our 'Add Data to Database' button. Finally, 'Close Database' closes the database.

Previous Section
Next Section

Copyright © 2001-2006 ebmDevMag.com - Legal Notice