dbAddNew()

Database function that generates a new, empty data record.

Synopsis

int dbAddNew(dbRecordset recordset);

Parameters

Parameter Meaning
recordset Record set reference

Return Value

The return value is an error code. 0 denotes successful execution of the operation. With other values detailed error information can be retrieved using dbGetError().

Errors

On transfer of an invalid record set reference variable or if the data source or record set cannot be modified, an error is returned.

Description

The function dbAddNew() is called to create new data records. This generates a new, empty database that becomes the current data record. After the data fields have been set (with dbPutField() ), they are entered in the new record with dbUpdate().

Depending on the data source, it may be necessary to call dbRequery() to be able to address the new record in the record set with the navigations methods. The dbAddNew() method can only be used with the cursor types adOpenKeyset and adOpenDynamic (provided that the data source supports these types).

Note that open changes (i.e. with data fields changed by dbPutField() without dbUpate()) are automatically saved in the data source by dbUpdate() before dbAddNew is called (internally).

Example

main()
{
  int rc; //declaration of the connection function
  dbConnection conn ;//declaration of the connection
  dbRecordset rs; //declaration of the data subset
  anytype fld; //declaration of the data field
  rc = dbOpenConnection ("DRIVER=Microsoft Access Driver
  (*.mdb); DBQ=TEST.MDB;DefaultDir=C://test//jetdb;
  UID=;PWD=;", conn);
  // opens the connection to the data source
  if (!rc)
  {
    rc = dbOpenRecordset(conn,"SELECT * FROM PERS",rs,2);
    // opens the connection to the data subset
    if (!rc)
    {
      rc = dbAddNew (rs); //create new data record
      if (!rc)
      {
        rc = dbPutField (rs, 0, "4711");
        // writes "4711" to field 0
        if (!rc)
        rc = dbPutField (rs, 1, "Doe, John");
        // writes "4711" to field 1
        if (!rc)
        rc = dbPutField (rs, 2, 6823.25);
        // writes 6823.25 to field 2
        if (!rc)
        rc = dbPutField (rs, 3, getCurrentTime());
        // writes the currently queried time to field 3
        if (!rc)
        rc = dbUpdate (rs);
      } 
      //updates the changes to the data record
      rc = dbRequery (rs); // rereads the data again
      if (!rc)
      rc = dbAddNew (rs); //goes to the first data record
      if (!rc)
      rc = dbMove(rs, 5); //goes to the 5th data record
      if (!rc)
      rc = dbDelete (rs); //deletes the record
      dbCloseRecordset (rs); //closes the data subset
    }
    dbCloseConnection (conn); //closes the connection
  }
}

Assignment

ADO, Database functions

Availability

CTRL