Search code examples
c++c++buildervclfiredacc++builder-12-athens

FireDAC Append function not creating a new record on the dataset, instead it modifies the record


For my internship, I'm developing a simple tcl Windows-only app using C++Builder 12.

The app is used to do some simple CRUD with an interface. I'm using SQLite as a database, and also FireDAC, although that may not be relevant.

The app is divided into 2 main menus and one of them does not seem to obey the same rules.

image

Whenever I click on "nouvelle rallonge" (new extension) or "nouveau tube" (new tube), I call another window as well as Append() on the right table, and after filling the required field(s) I Post() it.

The problem is that the Append() acts weirdly. On the rest of the app, it works as intended. But here, it gets the value of the already-existing entry in the database (displayed in the TDBGrid) and I can modify it, but I cannot create a new entry.

image

I use the same window "architecture" (i.e : using TDBEdit) and code logic as the other pages where it works perfectly fine.

Here is the code causing issues:

2nd main page :

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "menuApp.h"
#include "menuRallonges.h"
#include "datamodule.h"
#include "gestionRallonge.h"
#include "gestionTube.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFMenuRallonges *FMenuRallonges;
extern bool isEditing;
//---------------------------------------------------------------------------
__fastcall TFMenuRallonges::TFMenuRallonges(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFMenuRallonges::btRallongeClick(TObject *Sender)
{
    TFRallonge *FRallonge = new TFRallonge(this);
    short res = FRallonge->ShowModal();
    delete FRallonge;

}
//---------------------------------------------------------------------------
void __fastcall TFMenuRallonges::btTubeClick(TObject *Sender)
{
    TFTube *FTube = new TFTube(this);
    short res = FTube->ShowModal();
    delete FTube;

}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::FormShow(TObject *Sender)
{
   DataModule1 = new TDataModule1(this);
    isEditing=false;
    DBGrid1->DataSource->DataSet->Refresh();
    DBGrid2->DataSource->DataSet->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::btModifRallongeClick(TObject *Sender)
{
   isEditing=true;
   btRallongeClick(this);
   FormShow(this);
}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::btModifTubeClick(TObject *Sender)
{
   isEditing=true;
   btTubeClick(this);
    FormShow(this);
}
//---------------------------------------------------------------------------


void __fastcall TFMenuRallonges::FormDestroy(TObject *Sender)
{
    if (DataModule1 != nullptr)
    {
        delete DataModule1;
        DataModule1 = nullptr;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::FormClose(TObject *Sender, TCloseAction &Action)
{
    FormDestroy(this);
}
//---------------------------------------------------------------------------

Tube page:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "datamodule.h"
#include "menuRallonges.h"
#include "gestionTube.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFTube *FTube;
extern bool isEditing;
//---------------------------------------------------------------------------
__fastcall TFTube::TFTube(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFTube::Button1Click(TObject *Sender)
{
    Button1->SetFocus();
    try {
        DataModule1->TubevideTable->Post();
    }
    catch (const Exception &E) {
        ShowMessage("Erreur lors de l'insertion : " + E.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFTube::FormShow(TObject *Sender)
{

    if (isEditing==true) {
//      ShowMessage("editing true");
    }
    else
    {
//      ShowMessage("editing false");
//      DataModule1->TubevideTable->Last();
        DataModule1->TubevideTable->Append();

    }

}
//---------------------------------------------------------------------------
void __fastcall TFTube::Button2Click(TObject *Sender)
{
    DataModule1->TubevideTable->Cancel();
}
//---------------------------------------------------------------------------

I don't really know what is causing this, so if you have any idea please tell me.


Solution

  • As @RemyLebeau said, the creation of a new instance of 'DataModule1' is suspicious. Even if it worked fine on the other page, i don't really know why but on the other it was causing the bug this post's about. I just removed the line from the functions and now everything works fine, thanks everyone !