Recent

Author Topic: [SOLVED] Adding a record and updating in one form  (Read 331 times)

Pe3s

  • Sr. Member
  • ****
  • Posts: 335
[SOLVED] Adding a record and updating in one form
« on: November 11, 2022, 10:33:37 am »
Hello, I have a question if it is possible to add a record and update it using the same form. In order not to create a separate window for adding and a separate window for updating existing records?

Regards
« Last Edit: November 11, 2022, 04:11:35 pm by Pe3s »

Zvoni

  • Hero Member
  • *****
  • Posts: 1605
Re: Adding a record and updating in one form
« Reply #1 on: November 11, 2022, 10:38:51 am »
That's just plain code-logic.
I have something like this.
I use a StringGrid to display existent Records. If i select a record from the Grid, my code goes into "Update"-Mode. Any Changes are considered an Update.
Additionally i have a Button "New". Once clicked, code goes into INSERT-Mode (cleaning up any Controls which might have been filled beforehand).

I'm pretty sure, other people will advise to use Databound controls (DBGrid et al), where it's easy too, to add, delete or update records.

many ways to skin that cat.

No need for separate Forms
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Pe3s

  • Sr. Member
  • ****
  • Posts: 335
Re: Adding a record and updating in one form
« Reply #2 on: November 11, 2022, 11:15:53 am »
@Zvoni as for the logic of the code, a conditional statement is enough?

Zvoni

  • Hero Member
  • *****
  • Posts: 1605
Re: Adding a record and updating in one form
« Reply #3 on: November 11, 2022, 12:20:32 pm »
@Zvoni as for the logic of the code, a conditional statement is enough?
Pretty much.
Don't get me wrong: Using DB-bound Controls is a viable way, it's just that in basically 25 years i've never used them, since i like to have control over what is happening.
So if you want to do it yourself:
I have a Variable which is valid for the scope of the Form (could be "global", could be a property of the Form, of a Class i use there, whatever).
This variable is "out of the gates" always first in Update-Mode (or if i use an Enum the third state: Undefined).
Click on the Grid (selecting a record) definitely switches the Update-Mode on. Change what you have to change (i use separate TEdit's for that). Click on Save, DB gets UPDATED.
Click on "New"-Button: Switch to INSERT-Mode, clear the TEDit's, enter new Data, hit Save, record gets INSERTED into DB.

In both cases, when hitting "Save" i check the State of the Variable (Undefinded, Update, Insert).
Basically a
Code: Pascal  [Select][+][-]
  1. Case StateVariable Of
  2. Case Update: CallUpdateSQLStatement;
  3. Case Insert: CallInsertSQLStatement;
  4. Case Undefined: ShowMessage('Ooops');
  5. End;

Done
« Last Edit: November 11, 2022, 02:03:09 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Pe3s

  • Sr. Member
  • ****
  • Posts: 335
Re: Adding a record and updating in one form
« Reply #4 on: November 11, 2022, 04:11:19 pm »
@Zvoni, Thank you :)

 

TinyPortal © 2005-2018