Jump to content
Columbo

Help with Query at run time

Recommended Posts

BTW Congratulations on your first project, and I have to say it's a beauty. Most first projects are dull and boring. However....

 

Your final project crashes on my computer, it will only run on your system! Reason is a common mistake that has bitten me many times. Your Database path is still hard coded to your system. If you proudly load it on a flash drive (as you should) and show it, it will crash(very embarrassing). See the procedure OpenConnection in my earlier post. You still need it hard coded to work with it in design mode, so the habit I have gotten into is to always set Connected to False and Database to an empty string first thing in the OnCreate event. Then make sure to use runtime function to get Database location before connecting.

 

 

Share this post


Link to post

That was it!  I should have caught that the quote was missing.  I must be blind.  Actually I am going for cataract surgery next month. lol

Thanks Gary.

Share this post


Link to post
27 minutes ago, Gary said:

Your final project crashes on my computer, it will only run on your system! Reason is a common mistake that has bitten me many times. Your Database path is still hard coded to your system.

 

I thought that I had fixed that.  I used:

[procedure TfrmDino.FormCreate(Sender: TObject);  //Form creation
begin
  conPLife.Params.Database := IncludeTrailingPathDelimiter(TDirectory.GetCurrentDirectory) + conPLife.Params.Database;
  conPLife.Connected := True;
  tblPLife.Active := True;
  lbxData.Visible := False;
end;

 

In the conPLife under Params I have Database set as dino.  Should I clear that and do it in the code?

 

Quote

Congratulations on your first project, and I have to say it's a beauty. Most first projects are dull and boring.

Thanks Gary.  It is a program that I wrote a few years back and I used it as a learning process for Purebasic and then again in Python so I now have 3 different versions of this program. 🙂

Edited by Columbo

Share this post


Link to post

I redownloaded the finished program and it works fine! I must have tried one of the others.

Share this post


Link to post

Ok, I was just going to send another link to the latest fixed version.  I won't have to now. 🙂

Thanks for all of your help and encouagement Gary.

 

Merry Christmas and hope you have a great 2024.

Share this post


Link to post

Setting designtime components can be very helpful, try this:

 

Open the connection and table.

Right click table and select FieldsEditor...

Right Click FieldsEditor and select AddAllFields

Notice the field objects are added to the form definition (tblPLifename)

Click the dropdown of the FieldName on your DBEdits and notice you have a list to choose from, no typing in the field name where simple typo can derail you. Also you can now do this:

Quote

procedure TfrmDino.LoadImages;
begin
  dinoImage.Picture.LoadFromFile('dino_images\' + tblPLifename.AsString + '.jpg');
  dinoSize.Picture.LoadFromFile('dino_size\' + tblPLifename.AsString + '_size' + '.jpg');
end;

No more tblPLife.FieldByName('name').AsString

Share this post


Link to post

While this is a simple case and serves to teach, I do suggest one change there. Let it be as it should be.
Create a TDataModule and put a FireDAC on it:

  • WaitCursor
  • Connection
  • Transaction

The TFDTable-s in the whole project will connect to the DB using MyDataModule.MyConnection.
The appropriate code from the form will also be moved to the TDataModule.
I didn't want to write this by the shovelful. Since you didn't get it, here it is exactly:
if TableDino.Locate('record', iSelect, []) then
As you can see, I'm not searching by text, but by number. This is always faster and more certain.

Share this post


Link to post
1 hour ago, Stano said:

 

Thanks Gary,  I will store this away for future use.

Edited by Columbo

Share this post


Link to post
5 hours ago, Gary said:

Setting designtime components can be very helpful, try this:

 

Open the connection and table.

Right click table and select FieldsEditor...

Right Click FieldsEditor and select AddAllFields 

Notice the field objects are added to the form definition (tblPLifename)

Click the dropdown of the FieldName on your DBEdits and notice you have a list to choose from, no typing in the field name where simple typo can derail you. Also you can now do this:

No more tblPLife.FieldByName('name').AsString

Thanks Gary, I will store this away for future use.

Share this post


Link to post
2 hours ago, Stano said:

While this is a simple case and serves to teach, I do suggest one change there. Let it be as it should be.
Create a TDataModule and put a FireDAC on it:

  • WaitCursor
  • Connection
  • Transaction

The TFDTable-s in the whole project will connect to the DB using MyDataModule.MyConnection.
The appropriate code from the form will also be moved to the TDataModule.
I didn't want to write this by the shovelful. Since you didn't get it, here it is exactly:
if TableDino.Locate('record', iSelect, []) then
As you can see, I'm not searching by text, but by number. This is always faster and more certain. 

Thanks Stano, I took your advice and sorted the List alphabetically.  Previously the items in the Listbox was in the same order as the database except that, record 1 in the database was position 0 in the Listbox. When a record was selected I added 1 to the position in the Listbox and that corresponded to the correct record in the database.  When the Listbox was sorted, the positions in the Listbox did not align with the record number in the database, so I used the name selected in the Listbox and searched the name field in the database.  That works fine.   If the Listbox is used there will not be any typo errors.

 

Here is a link to the sorted version.

 

Sorted Version

 

 

Edited by Columbo

Share this post


Link to post

@Columbo  This is such a great sample app! Fine the way it is but simple enough that you can use it to experiment as you move forward.

How about letting your users see your Dino's in the order they want?

 

procedure TfrmDino.Reorder;
begin
  tblPLife.Close;
  if rgSort.ItemIndex = 0 then
    tblPLife.SQL[1] := 'ORDER BY record;'
  else
    tblPLife.SQL[1] := 'ORDER BY name;';

  tblPLife.Open;
  LoadList;
  LoadImages;
end;

OrderBy.png

  • Like 1

Share this post


Link to post

I foresee the next stage of improvement. To make the descriptive text (in Memo) formatted - RTF. 

Share this post


Link to post
On 12/23/2023 at 2:21 PM, Columbo said:

I am not trying to be nasty or anything but what difference does it make if it is in lowercase?  Just curious as to the reasoning.

No difference. It is either personal preference or, in a professional setting, a team policy or design decision. I prefer field names to be "CamelCase". For example, FirstName. I prefer this for variables and class members as well. 

Edited by JonRobertson

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

×