Jump to content
PenelopeSkye

How is text being saved to the database?

Recommended Posts

I have inherited an app written in Pascal which I have never written before. I am barely competent with c sharp.  I am using Embarcadero® Delphi 10.3

 

I have created my own program that gets data from a database and inserts data, but as far as the uber app goes I cannot figure out how text from edit boxes is being written to the database as soon as it is typed.  

 

For example I created a button that populates a edit box (the person I work with is tired of everybody entering a certain string incorrectly).  When the user types in text it is automatically saved to the database, but in order for the text that is populated by clicking on the button to be saved the user has to click an edit button on a TBDNavigator, then press the button to populate the box, then press the post button.  I have included the code snippet below, I couldn't see any instructions on how to format code.

 

          procedure TfDesignMaster.NoUPCButtonClick(Sender: TObject);

          begin
          DBEdit85.Text := 'NO UPC ON ITEM';
          end;

 

I know there must be a way to open edit and post but I can't find the datasource for it, there are so many associated with the project and I don't know the name of it.

 

Does anyone know of a way to find the datasource that will allow me to add the code to edit and post?  This is the only button that does this in the entire app or I would copy the code, and the other edit boxes are not pointing me anywhere.

 

I am sure I am leaving out all sorts of information needed to answer this question, apologies and please let me know what information I can provide!

 

Thanks! 

Share this post


Link to post

Well, if I understand correctly, the DBEdit85 component has a property called "DataSource", which in turn should point to a "ClientDataSet"

Share this post


Link to post

There's probably an OnChange or OnExit event handler for that DBEdit that is doing the automatic save.

 

DBEdits have a DataSource property so you can reference the underlying data source with that property.

DBEdits also have a DataField property which points to the field in the database for which the value is read and written.

 

Delphi's VCL provides DBEdits as a convenience and watches when the focus leaves the control to update the data set held in memory until the entire dataset is posted back to the database. So you could simply add one line to your ButtonClick event to change the focus after the text is set:

DBEdit86.SetFocus;

assuming there's a DBEdit called DBEdit86. Perhaps just switch focus to the OK button or some other control. (There's also a way to just switch focus to the next control in the window but that escapes me at the moment).

 

But a better way would be to do this programmatically (instead of trying to mimic what the DBEdit has to do), something like this:

procedure TfDesignMaster.NoUPCButtonClick(Sender: TObject);
begin
  DBEdit85.DataSource.Edit;
  DBEdit85.DataSource.FieldByName(DBEdit85.DataField).AsString := 'NO UPC ON ITEM';
  DBEdit85.DataSource.Post;
end;

Now, assuming you know what the DataSource is, you can list it explicitly instead of referencing it with "DBEdit85.DataSource"; same with the FieldName--I just used the information I had here.

 

For example:

procedure TfDesignMaster.NoUPCButtonClick(Sender: TObject);
begin
  InventoryTable.Edit;
  InventoryTable.UPC.AsString := 'NO UPC ON ITEM';
  InventoryTable.Post;
end;

There are a couple of gotchas you need to understand that only you will be able to answer by looking at the code in that form:

  • Is the DataSet already in Edit mode?  If so, you don't need to call .Edit.
  • Will other edit controls or application logic expect the DataSet to still be in Edit mode after that button is clicked?  If so you don't want to call .Post (or call .Edit again immediately after).

There are probably several other considerations but this is a start.

 

  • Like 2
  • Thanks 1

Share this post


Link to post

Remember: is the entered value valid?

Share this post


Link to post

or is the TEdit bound to the data source by LiveBindings ? (Right click on the TEdit and select BindVisually....)

Share this post


Link to post

I noodled around using your suggestions, especially corneliusdavid,  and it turns out to be

dm.ds_dm_presentation_design_links.DataSet.Edit;
DBEdit85.Text := 'NO UPC ON ITEM';
dm.ds_dm_presentation_design_links.DataSet.Post;

 

Thank you all so much!!!!

Share this post


Link to post
On 5/26/2022 at 10:51 AM, Roger Cigol said:

or is the TEdit bound to the data source by LiveBindings ? (Right click on the TEdit and select BindVisually....)

Side note: Considering the DBEdit in question is the 85th of its kind, and remembering the low scalability of LiveBindings I find that highly improbable.

 

Share this post


Link to post

The entered value is valid, this field is able to post any text entered into it (although I am assuming there are length restrictions) so any text is valid. Is that what you meant?

 

There are live bindings, but I was unable to understand what I was seeing, and was never sure if they had anything to do with saving to the db.  When I went to the Live Bindings Designer there were just a lo9t of objects and I couldn't tell how they were connected to anything else.

 

Ignorance is not bliss!

 

I found that I had to add a close statement which I have since added.

 

Thanks Roger!

Share this post


Link to post
1 hour ago, PenelopeSkye said:

When I went to the Live Bindings Designer there were just a lot of objects and I couldn't tell how they were connected to anything else.

By default, the LiveBindings Designer will show all objects, allowing you to hook them up. When you hook them up, lines appear between the connected ones. If there are no connections, your app is not using LiveBindings.

 

In this screenshot, only two components are connected:

LiveBind-edit-label.thumb.png.7be0b2ffc543166ccf47733e818dfd6c.png

 

Hope that helps!

Share this post


Link to post
2 hours ago, PenelopeSkye said:

The entered value is valid, this field is able to post any text entered into it (although I am assuming there are length restrictions) so any text is valid. Is that what you meant?

If you mean me, yes. Your answer is more than weird. E.g. what about aaabbbcccddd ....? I assume you know what you're doing. Text length can be affected by:

  1. by setting a value in the DB component
  2. by defining in the DB table field
  3. used database

I'm in the habit of combing each text string with leading and trailing chars (spaces) before saving it in the DB.

Share this post


Link to post

So there can be large numbers of objects in the Live Bindings designer not connected to anything else.  I don't know why that should be so odd to me, you don't have to connect objects.  I will endure, and I won't keep looking for connections where there are none!

 

And yes, there is text handling such as removing leading and trailing spaces, It was just in there before I ever got my hands on the app 🙂

 

Thanks David and Stano!

Share this post


Link to post

Just an additional tip... You can right+click on an object (or group of objects) and select Hide Element(s) to hide ones you're not interested in. You can also put groups of components into Layers to only show a limited set at a time to make working in the LiveBindings Designer easier to manage.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×