Jump to content
Henry Olive

Locate

Recommended Posts

I wish everyone a healthy day.

My Table

AAAA-01X

BBBB-02X

CCCC-03X

 

Is there a way to Search & Locate a record like with just '02' searchtext ( that is i want to locate BBBB-02X )

I tried below code but no success

SearchText:='02';

if not Dataset.Locate(SERIALNO, SearchText, [LoCaseInsensitive,LoPartialKey]) then

ShowMessage ('...........')

I need something like Sql's  CONTAINING 
( I dont want to Filter or SQL, just i need is Locate )

Thank You
 

 

Share this post


Link to post
Guest

A locate is a lookup + a move. If you want a DB aware lookup-like control to accept keystrokes from the user to locate that would be very tricky to code.

Depending on what database and layers you are using, consider creating a calculated field at the most logical level and use that as a primary key.

The "algo" would be to put the digits in position 6 and 7 first and then the rest of the information after that, 02X-BBBB or 02BBBB-X depending on the most logical order for your case. It will still be tricky (i guess have not tested) to make it "logical" for the end user if character locate is desired.

In some use cases i have, the result of a calculated field can actually be better to show the end-user.

 

Edit: When re-reading the post, i know too little. It sounds like you should use SQL, fire of a simple stand-alone query (if (qry.Fields[X].isNull) ShowMessage) where X would be the PK.

Edited by Guest

Share this post


Link to post
On 8/30/2021 at 2:36 PM, Henry Olive said:

I wish everyone a healthy day.

My Table

AAAA-01X

BBBB-02X

CCCC-03X

 

Is there a way to Search & Locate a record like with just '02' searchtext ( that is i want to locate BBBB-02X )

 I tried below code but no success

 SearchText:='02';

if not Dataset.Locate(SERIALNO, SearchText, [LoCaseInsensitive,LoPartialKey]) then

 ShowMessage ('...........')

I need something like Sql's  CONTAINING 
 ( I dont want to Filter or SQL, just i need is Locate )

Thank You
 

 

You don't want to do what you have to do because you don't want filter and sql. With Locate, you can refer to a specific value in a specific field.

If you want to be a crazy developer you can try the code below.

 

However, this code will affect you very badly in terms of performance and it is a method that I do not recommend at all. I pity the user if there are too many records... 😞

 

I created such a function by blending it with your request. There may be times when it does not give accurate results. It's a context dependent situation.

Here's an example from UniDAC:

 

 

 

function SearchAndLocate(DataTable: TUniTable; Column, Value: String): Boolean;
begin
 with DataTable do
 begin
   Result := False;
   if not DataTable.Active then DataTable.Active := True;
   if DataTable.RecordCount <= 0 then raise exception.Create('record not found lmao');
   First;
   while not Eof do
   begin
     if ContainsStr(FieldByName(Column).Value, Value) then
     begin
      Result := True;
      break;
     end;
     Next;
   end;
 end;
end;

 

Usage:

 

SearchAndLocate(UniTable1, 'value', '04')

MY ADVICE: USE SQL OR FILTER!

Edited by Halil Han Badem
remove application.proccessmessages

Share this post


Link to post
Guest

Any, any solution with that call. No. Just do not.

It will bite back. It goes against.... everything.

If you have to put that in your code... you have a much bigger problem.

 

Anyone who thinks they have a solution when putting Application.ProcessCrap into app-code are WRONG.

Just having to type that is a bell telling you you are going in the wrong direction.

Edited by Guest

Share this post


Link to post
40 minutes ago, haentschman said:

Hi :)

 

This is the first time I've heard of this from you. I've had no problems with with..do since the past. So it was others who lived (reading the code) and had problems with the compiler. Thanks for the info mate.

Share this post


Link to post
9 hours ago, Dany Marmur said:

Any, any solution with that call. No. Just do not.

It will bite back. It goes against.... everything.

If you have to put that in your code... you have a much bigger problem.

 

Anyone who thinks they have a solution when putting Application.ProcessCrap into app-code are WRONG.

Just having to type that is a bell telling you you are going in the wrong direction.

"Application.ProccessMessages" yes WRONG in terms of usage but...For some reason, it was hard to quit using this command in the past when I found out what it actually did. I was seeing this command when I looked at the examples with while loops. Sometimes, even if we see it on the internet i realized that we had to investigate. I understood the issue with the first person who warned us that we should research that code.  Thank you for the information.

Edited by Halil Han Badem

Share this post


Link to post

:classic_tongue: Gladly...but the most important thing is not to resolve the variable in debugging mode.

FieldByName(Column).Value

...at the breakpoint...the debugger says:  Value = ??? :classic_sad:

Edited by haentschman

Share this post


Link to post
12 hours ago, Halil Han Badem said:

if ContainsStr(FieldByName(Column).Value, Value) then

This is a bad idea for two reasons:

 

1.) What will happen when the value is null? It will fail with message that Delphi cannot convert the null to string.
2.) Why would you like to find the field for every record. You should find it before wile and store it in a local variable and use that instead.
 

 

 

  • Confused 1

Share this post


Link to post
27 minutes ago, haentschman said:

:classic_tongue: Gladly...but the most important thing is not to resolve the variable in debugging mode.


FieldByName(Column).Value

...at the breakpoint...the debugger says:  Value = ??? :classic_sad:

Yes, here the compiler may not give correct information to the developer. For this, when adding a value to the Watch list, we need to prefix the value defined in with...do. And of course the field value.

 

7 minutes ago, Lajos Juhász said:

This is a bad idea for two reasons:

  

1.) What will happen when the value is null? It will fail with message that Delphi cannot convert the null to string.
 2.) Why would you like to find the field for every record. You should find it before wile and store it in a local variable and use that instead.
  

 

 

you need to re-read my reply. I stated that it was a bad idea, BUT I CREATED THE FUNCTION THAT HE WANTED, BECAUSE IT AGAINST SOLUTIONS IN THE QUESTION THAT ASKED. Before copy-pasting, you need to make your own edits.

Edited by Halil Han Badem

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

×