Ovde imas primer kako iskoristiti vise od jedne kolone za pretrazivanje:
Citat:
VCL Help:
TCustomADODataSet::Locate
Searches the dataset for a specified record and makes that record the current record.
virtual bool __fastcall Locate(const AnsiString KeyFields, const System::Variant &KeyValues, TLocateOptions Options);
Description
Call Locate to search a dataset for a specific row and make it the current row.
KeyFields is a string containing a semicolon-delimited list of field names on which to search.
KeyValues is a variant that specifies the values to match in the key fields. If KeyFields lists a single field, KeyValues specifies the value for that field on the desired row. To specify multiple search values, pass a variant array as KeyValues, or construct a variant array on the fly using the VarArrayOf routine. For example:
TLocateOptions Opts;
Opts.Clear();
Opts << loPartialKey;
Variant locvalues[2];
locvalues[0] = Variant("Sight Diver");
locvalues[1] = Variant("P");
CustTable->Locate("Company;Contact", VarArrayOf(locvalues, 1), Opts);
Options is a set that optionally specifies additional search latitude when searching on string fields. If Options contains the loCaseInsensitive setting, then Locate ignores case when matching fields. If Options contains the loPartialKey setting, then Locate allows partial-string matching on strings in KeyValues. If Options is an empty set, or if KeyFields does not include any string fields, Options is ignored.
Locate returns true if it finds a matching row, and makes that row the current one. Otherwise Locate returns false.
U gornjem primeru, pretraga se vrsi po poljima: "Company" i "Contact", a trazi se "Sight Driver" i "P", odnosno ono sto pocinje na te sadrzaje, jer je postavljeno "loPartialKey".
Dakle, dinamicki, na osnovu check boxova i njihovog sadrzaja treba da kreiras recimo string:
Kolone = "Ime;Prezime;Adresa"
... i da postavis:
locvalues[0] = Variant(Edit1->Text);
locvalues[1] = Variant(Edit2->Text);
locvalues[2] = Variant(Edit3->Text);
Pretraga (broj 2 znaci da imas 3 elementa:0,1,2):
ADOTable1->Locate( Kolone, VarArrayOf(locvalues, 2), Opts );
Ipak, taj dizajn nije bas najidealniji za pretragu. Bolje je da DBGrid (preko DataSource ili DataSet, zaboravio sam) bude u u vezi sa nekim TADOQuery-jem, i da dinamicki u OnChange dogadjaju kreiras upit koji odgovara sadrzajima Edit polja.
U principu, sve se svodi na dinamicko generisanje upita:
SELECT IDDen, Ime, Prezime, Adresa
FROM TaTabela
WHERE Ime LIKE '%I%' and Prezime LIKE '%C%' and Adresa LIKE '%S%'
Ovime bi pretraga suzavala izbor pred tvojim ocima, dok ne dodjes do rezultata.
[Ovu poruku je menjao X Files dana 07.06.2010. u 13:36 GMT+1]