How I have to do to write data into dbf in particular cell?

Hello,

I use the cicode below to write data into dbf files. 

PRIVATE INT FUNCTION DataFromAPPToDB(STRING sRicetta);
INT hDev,hRec;
STRING s;

hDev = DevOpen("Ric_riv",0);
hRec = DevFind(hDev,sRicetta,"RICETTA");
IF hRec <> 0
THEN
Message("Error","@(Ricetta non trovata)",0);
RETURN -1;
END

//Inizio Variabili
DevSetField(hdev,"RICETTA",_nome_ric_ricetta );
DevSetField(hdev,"TIPO1",_SCRITTA_SACCO );

DevSetField(hdev,"ING1",IMP_14F_ART );
DevSetField(hdev,"CODICE1",IMP_14F_DES );
DevSetField(hdev,"PERC1",_Perc_ric_14F);

DevSetField(hdev,"ING2",IMP_15F_ART );
DevSetField(hdev,"CODICE2",IMP_15F_DES );
DevSetField(hdev,"PERC2",_Perc_ric_15F);


DevClose(hDev);
Prompt("@(Ricetta salvata)");
RETURN 0;
END

The dbf is configured with the first line like :

RICETTA,C,50 ING1,C,50 ING2,C,50 PERC1,C,6 PERC2,C,6 CODICE1,C,20 CODICE2,C,20 TIPO1,C,20

In this case, all data will be written in the column below the specified cell.

Question:

How I have to do to write one value to a cell B4  or C15?

  • You explain that the first line of the DBF is "RICETTA,C,50 ..." but that is not the first record in the dbf. It is just how OpenOffice displays the record headers, it is not part of the data.

    I will try to explain how to write to a single field of a single record (again, references like "B4" and "C15" are just a representation in OpenOffice or Excel, this is not how dBase-files are used).

    1. In cicode, find the record (the line in a spreadsheet) that you would like to change:
    1a. by using DevFind() to search for a unique identifier (like in your example using the "RICETTA" field)
    1b. or by using DevSeek() to move to the record number directly (dbf files start with number 1)
    1c. or by using DevFirst() and DevNext() to loop through all records
    1d. or by using DevAppend() to add a new record.

    2. Modify the field (the cell in a spreadsheet) by using DevSetField(). You only need to call this for the field(s) you want to change, the rest of the fields remain unaltered.

    Some remarks on your example:
    1. You should check if DevOpen() is succesful (<> -1) before you call DevFind() and RETURN if not.
    2. If DevFind() is not succesful (<> 0) then you should close the device using DevClose() before exiting your function to prevent memory leaks.
  • Ok, thank you. But if I want to write directly in cell positioned in column 2 and line 5 of DB, how I have to wrote cicode?
  • That is not a logical thing to do, but as I already mentioned you could use DevSeek(5) in cicode to go to line 5. For the column you have to know the field name (like "RICETTA" or "PERC2", etc.) and use it in DevSetField().