Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple search in a datagrid

Status
Not open for further replies.

iara84

Systems Engineer
Oct 25, 2022
13
CO
from a text box you enter the data to search for nombre,identifica,direccion and telefono by means of a button. The button is where I assign the search code, but it does not do the exact search, sometimes it works and other times it does not search according to the entered text.

SELECT clientes2

IF !EMPTY(THISFORM.Text1.Value)
GO TOP
SET ORDER TO
SET ORDER TO nombre
SEEK ALLTRIM(thisform.text1.Value)
GO TOP
SET ORDER TO
SET ORDER TO identifica
SEEK ALLTRIM(thisform.text1.Value)
GO TOP
SET ORDER TO
SET ORDER TO direccion
SEEK ALLTRIM(thisform.text1.Value)
GO TOP
SET ORDER TO
SET ORDER TO telefono
SEEK ALLTRIM(thisform.text1.Value)
thisform.Refresh
ELSE
messagebox('Registro no existe ' + ' ' + THISFORM.Text1.Value)
ENDIF
 
After each individual SEEK you end on the first result fulfilling that index. SEEK always starts from TOP, not from the position you already have, and it doesn't lock into the results that also fit the previous SEEK at all. So what you want to do must be made by a LOCATE that does AND all individual clauses. This LOCATE then would use the indexes that apply to every single seek, too. So you gain from having all four individual indexes, but a SEEK can only combine multiple fields if you create an index that combines all fields. And even then you can only find results that fulfill the whole index expression that combines them.

Chriss
 
And by the way, you seem to expect your last SEEK ends in a record that fulfills all entered criteria,but indeed you explicitly GO TOP for every single SEEK, so even if ou assume SEEKS can be combined, it surely never can work since you move away from the previous result by GO TOP. You only find a record if the telefono is found, and the nombre hasn't have to be found at all, so all your code can be replaced by the last SEEK with order telefono and odes the same as all code, so it only seeks telefono, nothing else. And then you always seek the same value, text1.value, don't you have 4 textboxes? One for each field?

The LOCATE that would work is (assuming index tag names are equal to single field names and you have text1 for nombre up to text4 for telefono, better name the textboxes accordingly, like txtNombre to txtTelefono:

LOCATE FOR nombre=ALLTRIM(thisform.txtNommbre.value) AND identifica=ALLTRIM(thisform.txtIdentifica.value) ...
IF NOT FOUND()
messagebox('Registro no existe')
ENDIF

I don't know, you might want to have just one text1 control and seek the value in all four indexes. Well, but then you should stop once you find a record. You don't stop, you always go on with the next SEEK no matter what FOUND() is. Well, and if you want to do that, use the SEEK() function, that does not only do the same SEEK as the SEEK command, it also returns FOUND() in the same go. And to do that you will end up in a nested IF SEEK(...) ... ELSE IF SEEK(...) ELSE IF SEEK(...) and not just four IFs one after the other, you only do further SEEKS if a SEEK does not find something.

Chriss
 
Yes, Chris has given you the answer. Just to summarise:

You search for Nombre. Then, whether your find it or not, you search for Identifica. And whether you find that or not, you search for Direccion. And so on. Each of those searches will override the result of the previous one. So, at the end of the process, the current record will be the one containing the specified Telefono (because that's the last thing you are searching for). Or, if the specified Telefono doesn't exist, the table will be at end of file. I'm sure that's not what you want.

What you need to do is to test for FOUND() after each SEEK. If that's .T., that means you have found what you want, so you should stop searching. Only if it returns .F. should you proceed to the next search.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Also, the title of your post refers to a data grid. This is puzzling, as there is no evidence of a grid in your code.

Your code indicates that you have a single text box into which the user can enter either a Nombre or an Indetifica or a Direcction or a Telefono. You want to search for a record which contains the specified value in any of those four fields.

Is that correct? If so, it would be better to do something like this:
Code:
SELECT clientes2
IF !EMPTY(THISFORM.Text1.Value)
  lcTerm =ALLTRIM(THISFORM.Text1.Value)
  LOCATE FOR Nombre = lcTerm OR Identifica = lcTerm ;
    OR Direccion = lcTerm OR Telefon = lcTerm
  IF FOUND()
    THISFORM.Refresh
  ELSE
   MESSAGEBOX("El valor especificado no existe")
  ENDIF
ELSE
  MESSAGEBOX("Porfavor introduzca un valor")
ENDIF

(You will of course need to correct my Spanish.)

Even better, instead of testing for !EMPTY(THISFORM.Text1.Value), you should set the search button's Enabled to .F. when you launch the form, and only set it to .T. when the user has entered something in the text box (which you would test for in the text box's InteractiveChange).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top