📄 searchingdatasets.htm
字号:
<html>
<head>
<title>Searching Records</title>
</head>
<!--#include virtual="/inc/header.php"-->
<table width="100%" border="0" cellspacing="0" cellpadding="2" bgcolor="#FFFFFF">
<tr>
<td align="left">
<span style="font-family:Helvetica,Arial; font-size:12pt; color:#000000"><b>Searching Records
<br>
</b></span>
</td>
<td align="right">
<font face="Arial" size="2">
<a href="accessingtableandqueryfiel.htm">Previous</a>
<a href="overview.htm">Top</a>
<a href="creatingfilters.htm">Next</a>
</font>
</td>
</tr>
</table>
<br><br>
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
You can search against any active table or query using the <a href=tdataset_locate.htm>Locate</a> and <a href=tdataset_lookup.htm>Lookup</a> methods. These methods enable you to search on any type of columns in any dataset.
<br>
However, by explicitly using indexes, <a href=tabstable_findkey.htm>FindKey</a>, <a href=tabstable_gotokey.htm>GotoKey</a> and other similar methods of TABSTable can improve over the searching performance provided by the <i>Locate</i> and <i>Lookup</i> methods.
<br>
<br>
<b>Using Locate</b>
<br>
<br>
Locate moves the cursor to the first row matching a specified set of search criteria. In its simplest form, you pass Locate the name of a column to search, a field value to match, and an options flag specifying whether the search is case-insensitive or if it can use partial-key matching. (Partial-key matching is when the criterion string need only be a prefix of the field value.) For example, the following code moves the cursor to the first row in the CustTable where the value in the Company column is "Professional Divers, Ltd.":
<br>
<br>
<span style="font-family:Courier New; font-size:8pt; color:#000000"><b>var</b>
<br>
LocateSuccess: Boolean;
<br>
SearchOptions: TLocateOptions;
<br>
<b>begin</b>
<br>
SearchOptions := [loPartialKey];
<br>
LocateSuccess := CustTable.Locate(<span style="font-family:Courier New; font-size:8pt; color:#000080">'Company'</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">, <span style="font-family:Courier New; font-size:8pt; color:#000080">'Professional Divers, Ltd.'</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">, SearchOptions);
<br>
<b>end</b>;</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
<br>
If Locate finds a match, the first record containing the match becomes the current record. Locate returns True if it finds a matching record, False if it does not. If a search fails, the current record does not change.
<br>
The real power of Locate comes into play when you want to search on multiple columns and specify multiple values to search for. Search values are Variants, which means you can specify different data types in your search criteria. To specify multiple columns in a search string, separate individual items in the string with semicolons.
<br>
<br>
Because search values are Variants, if you pass multiple values, you must either pass a Variant array as an argument (for example, the return values from the Lookup method), or you must construct the Variant array in code using the VarArrayOf function. The following code illustrates a search on multiple columns using multiple search values and partial-key matching:
<br>
<br>
</span><span style="font-family:Courier New; font-size:8pt; color:#000000"><b>with</b> CustTable <b>do</b>
<br>
Locate(<span style="font-family:Courier New; font-size:8pt; color:#000080">'Company;Contact;Phone'</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">, VarArrayOf([<span style="font-family:Courier New; font-size:8pt; color:#000080">'Sight Diver'</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">,<span style="font-family:Courier New; font-size:8pt; color:#000080">'P'</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">]), loPartialKey);
<br>
</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
Locate uses the fastest possible method to locate matching records. If the columns to search are indexed and the index is compatible with the search options you specify, Locate uses the index.
<br>
<br>
<br>
<b>Using Indexes to search for records
<br>
</b>
<br>
TABSTable supports similar indexed-based searches, but use a combination of related methods. The following table summarizes the six related methods provided by TABSTable to support index-based searches:
<br>
<br>
<table border="0" cellpadding="1"><tr><td align="left" valign="top" width="84">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><b><u>Method</u></b>
<br>
</span></td><td align="left" valign="top" width="369">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><b><u>Purpose</u></b>
<br>
</span></td></tr><tr><td align="left" valign="top" width="84">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><a href=tabstable_editkey.htm>EditKey</a>
<br>
</span></td><td align="left" valign="top" width="369">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Preserves the current contents of the search key buffer and puts the dataset into dsSetKey state so your application can modify existing search criteria prior to executing a search.
<br>
</span></td></tr><tr><td align="left" valign="top" width="84">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><a href=tabstable_findkey.htm>FindKey</a>
<br>
</span></td><td align="left" valign="top" width="369">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Combines the SetKey and GotoKey methods in a single method.
<br>
</span></td></tr><tr><td align="left" valign="top" width="84">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><a href=tabstable_findnearest.htm>FindNearest</a>
<br>
</span></td><td align="left" valign="top" width="369">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Combines the SetKey and GotoNearest methods in a single method.
<br>
</span></td></tr><tr><td align="left" valign="top" width="84">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><a href=tabstable_gotokey.htm>GotoKey</a>
<br>
</span></td><td align="left" valign="top" width="369">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Searches for the first record in a dataset that exactly matches the search criteria, and moves the cursor to that record if one is found.
<br>
</span></td></tr><tr><td align="left" valign="top" width="84">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><a href=tabstable_gotonearest.htm>GotoNearest</a>
<br>
</span></td><td align="left" valign="top" width="369">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Searches on string-based fields for the closest match to a record based on partial key values, and moves the cursor to that record.
<br>
</span></td></tr><tr><td align="left" valign="top" width="84">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><a href=tabstable_setkey.htm>SetKey</a>
<br>
</span></td><td align="left" valign="top" width="369">
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Clears the search key buffer and puts the table into dsSetKey state so your application can specify new search criteria prior to executing a search.
<br>
</span></td></tr></table>
<br>
<br>
<i>GotoKey</i> and <i>FindKey </i>are boolean functions that, if successful, move the cursor to a matching record and return True. If the search is unsuccessful, the cursor is not moved, and these functions return False.
<br>
<i>GotoNearest</i> and <i>FindNearest</i> always reposition the cursor either on the first exact match found or, if no match is found, on the first record that is greater than the specified search criteria.<b>
<br>
</b>
<br>
For example, the following code, attached to a button's <i>OnClick</i> event, uses the <i>GotoKey</i> method to move to the first record where the first field in the index has a value that exactly matches the text in an edit box:
<br>
<br>
</span><span style="font-family:Courier New; font-size:8pt; color:#000000"><b>procedure</b> TSearchDemo.SearchExactClick(Sender: TObject);
<br>
<b>begin</b>
<br>
ABSTable1.IndexName:=<span style="font-family:Courier New; font-size:8pt; color:#000080">'idxByLastNameFirstName'</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">;
<br>
ABSTable1.SetKey;
<br>
ABSTable1.Fields[<span style="font-family:Courier New; font-size:8pt; color:#000080">0</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">].AsString := Edit1.Text;
<br>
<b>if</b> <b>not</b> ABSTable1.GotoKey <b>then</b>
<br>
ShowMessage(<span style="font-family:Courier New; font-size:8pt; color:#000080">'Record not found'</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">);
<br>
<b>end</b>;</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
<br>
<i>GotoNearest</i> is similar. It searches for the nearest match to a partial field value. It can be used only for string fields. For example,
<br>
<br>
</span><span style="font-family:Courier New; font-size:8pt; color:#000000">ABSTable1.IndexName:=<span style="font-family:Courier New; font-size:8pt; color:#000080">'idxByLastNameFirstName'</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">;
<br>
ABSTable1.SetKey;
<br>
ABSTable1.Fields[<span style="font-family:Courier New; font-size:8pt; color:#000080">0</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">].AsString := <span style="font-family:Courier New; font-size:8pt; color:#000080">'Sm'</span></span><span style="font-family:Courier New; font-size:8pt; color:#000000">;
<br>
ABSTable1.GotoNearest;</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
<br>
If a record exists with "Sm" as the first two characters of the first indexed field's value, the cursor is positioned on that record. Otherwise, the position of the cursor does not change and <i>GotoNearest </i>returns False.
<br>
</span></span>
<!--#include virtual="/inc/footer.php"-->
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -