Control Name | Unit | Class |
Locate Object | DBUtils | TLocateObject |
Description:
Use TLocateObject to
search a dataset specified by DataSet property for a specific
record and position the cursor on it.
After setting DataSet property you can call Locate method which has parameter KeyField specified field name on which to search.
When you search on BDE data source, use TDBLocate class (inherits from TLocateObject) instead.
NOTE. In Delphi 2.0 or higher you can use standard TDataSet.Locate method instead.
Property
DataSet
Declaration:
DataSet: TDataSet;
Specifies the dataset (table or query) for which Locate method will search for specified value. Set this property and IndexSwitch property to appropriate values before call Locate method.
Property
IndexSwitch
Declaration:
IndexSwitch: Boolean;
If the dataset specified by DataSet property is TTable object and search field is indexed, TDBLocate can use the index. This property determines whether or not table will switch to the index corresponding to KeyField parameter of Locate method. When this property is False, index is used only when it's active index.
This property used by TDBLocate class only. Setting this property in TLocateObject class has no effect.
Method
Locate
Declaration:
function Locate(const KeyField, KeyValue: string; Exact,
CaseSensitive: Boolean): Boolean;
Searches the dataset specified by DataSet property for a specific record and position the cursor on it. KeyField parameter is a string specified field name on which to search. KeyValue is a string containing the value to match in the key field.
If CaseSensitive parameter is
False, then Locate ignores case when matching string fields.
If Exact parameter is False, then Locate finds the first record
that fulfills at least some initial part of the KeyValue criteria
for record matching.
Locate returns True if it finds a matching
record, and makes that record the current one. Otherwise Locate
returns False.
Const
ServerDateFmt
Declaration:
ServerDateFmt: string[50] =
'''"''mm''/''dd''/''yyyy''"''';;
ServerDateFmt variable specifies date format used in SQL queries by function FormatSQLDateRange, FormatSQLCondition and FormatAnsiSQLCondition. You can assign another value to this variable according to date format used by your SQL Server.
DBUTILS.PAS unit also contains additional constants that can be assigned to the ServerDateFmt variable:
• sdfStandard16,
sdfStandard32 - date format for STANDARD driver (local SQL);
• sdfOracle - date format for using with Oracle SQL-server;
• sdfInterbase - date format for using with Interbase
SQL-server.
Routine
AssignRecord
Declaration: procedure AssignRecord(Source, Dest:
TDataSet; ByName: Boolean);
AssignRecord copies values of fields from current record of source dataset specified by Source parameter to the current records of dataset Dest. Destination dataset must be in edit or insert mode.
When ByName parameter is True, then field values will be set from Source to Dest based on their names in the both datasets. If ByName is False, field values will be set based on the order in which fields are defined in the Source dataset.
AssignRecord example:
MemoryTable1.Append;
AssignRecord(SourceDataSet, MemoryTable1, True);
MemoryTable1.Post;
Routine CheckRequiredField
Declaration:
procedure CheckRequiredField(Field: TField);
Checks if a field Field has a nonblank value. Calling this function with a null value of Field will cause an exception to be raised.
CheckRequiredField
example:
CheckRequiredField(InventoryTableACCOUNT_NO);
Routine ConfirmDataSetCancel
Declaration:
procedure ConfirmDataSetCancel(DataSet: TDataSet);
The ConfirmDatasetCancel procedure verifies that the dataset's state, and if the dataset's State property is dsEdit or dsInsert displays a message box which asks user for confirmation that the pending changes will be saved to the database or will be cancelled. If user selected "Yse" (save) in a message box, the dataset's Post method is called to post any pending changes, if user selected "No", the dataset's Cancel method is called, otherwise (if the "Cancel" button was selected by user) the standard Abort procedure is called to generate EAbort exception.
ConfirmDataSetCancel example:
procedure
TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
__ConfirmDataSetCancel(Table1);
end;
Routine
ConfirmDelete
Declaration:
function ConfirmDelete: Boolean;
The ConfirmDelete procedure displays a message box which asks user for confirmation that the record from dataset should really be deleted. The function returns True if user selected "Yes" button in a message box. Otherwise the function returns False.
ConfirmDelete example:
if ConfirmDelete then
__Table1.Delete;
Routine DataSetSortedSearch
Declaration:
function DataSetSortedSearch(DataSet: TDataSet; const Value:
string; const FieldName: string; Unique, IgnoreCase: Boolean):
Boolean;
The DataSeetSortedSearch procedure
searches a dataset for value in the field FieldName.
DataSetSortedSearch example:
if not DataSetSortedSearch(DataSet, 'Smit',
'Client_Name', False, True) then
__MessageDlg('Record not found', mtError,
[mbOk], 0);
__...
Routine FormatAnsiSQLCondition
Declaration:
function FormatAnsiSQLCondition(const FieldName, Operator, Value:
string; FieldType: TFieldType; Exact: Boolean): string;
FormatAnsiSQLCondition formats an ANSI SQL condition to the BDE equivalent.
FormatAnsiSQLCondition
example:
rxQuery1.MacroByName('CONTRACT_NO').AsString :=
FormatSQLCondition('CONTRACTS."CONTRACT_NO"', '',
'N%4596*', ftString, True);
Routine FormatSQLCondition
Declaration:
function FormatSQLCondition(const FieldName, Operator, Value:
string; FieldType: TFieldType; Exact: Boolean): string;
FormatSQLCondition formats an SQL condition to the BDE equivalent.
FormatSQLCondition
example:
rxQuery1.MacroByName('CONTRACT_NO').AsString :=
FormatSQLCondition('CONTRACTS."CONTRACT_NO"', '',
'N-4596?', ftString, True);
Routine FormatSQLDateRange
Declaration:
function FormatSQLDateRange(Date1, Date2: TDateTime; const
FieldName: string): string;
FormatSQLDateRange checks a fieldname for a date period within Date1 and Date2.
FormatSQLDateRange
example:
FormatSQLDateRange(0, Date1,
'CONTRACTS."ACTIVE_FROM"')
Date1 = 01.01.95
Result = 'CONTRACTS."ACTIVE_FROM" <
"01/01/1995"'
Routine
RefreshQuery
Declaration:
procedure RefreshQuery(Query: TDataSet);
The RefreshQuery procedure for a dataset Query flushes local buffers and refetches data for an open dataset. You can use this procedure to update the display in data-aware controls if you think that the underlying data has changed because other applications have simultaneous access to the data used in your application.
RefreshQuery example:
RefreshQuery(Query1);
Routine
RestoreFields
Declaration:
procedure RestoreFields(DataSet: TDataSet; IniFile: TIniFile;
RestoreVisible: Boolean);
RestoreFields procedure restores
widths (in pixels) and indexes of dataset's fields from the
INI-file
specified in IniFile parameter, previously stored by SaveFields
procedure. Can be used to save and restore field's parameters
changed by user, for example, in a DBGrid component.
RestoreFields example:
procedure RestoreLayout;
var
__IniFile: TIniFile;
begin
__IniFile :=
TIniFile.Create(FormStorage.IniFileName);
__try
____RestoreFields(DataSet, IniFile);
__finally
____IniFile.Free;
__end;
end;
Routine
RestoreFieldsReg
Declaration:
procedure RestoreFieldsReg(DataSet: TDataSet; IniFile:
TRegIniFile; RestoreVisible: Boolean);
RestoreFieldsReg procedure restores widths (in pixels) and indexes of dataset's fields from the Windows
System Registry key specified in IniFile parameter, previously stored by SaveFieldsReg procedure. Can be used to save and restore field's parameters changed by user, for example, in a DBGrid component.
RestoreFieldsReg example:
uses Registry, Placemnt, DBUtils;
...
procedure RestoreDSLayout;
var
__RegIniFile: TRegIniFile;
begin
__IniFile :=
TRegIniFile.Create(FormStorage.IniFileName);
__try
____RestoreFieldsReg(DataSet, RegIniFile);
__finally
____RegIniFile.Free;
__end;
end;
Routine
SaveFields
Declaration:
procedure SaveFields(DataSet: TDataSet; IniFile: TIniFile);
SaveFields procedure saves widths (in pixels) and indexes of dataset's fields to the INI-file specified in IniFile parameter.
Can be used to store field's parameters changed by user, for example, in a DBGrid component. To restore widths and indexes of fields use RestoreFields procedure.
SaveFields example:
procedure
TGridForm.FormStorageSavePlacement(Sender: TObject);
var
__DataSet: TDataSet; I: Integer;
begin
__for I := 0 to TabSet.Tabs.Count - 1 do
__begin
____DataSet := DataSetByIndex(TabSet.TabIndex);
____if (DataSet <> nil) and
(DataSet.Active) then
______SaveFields(DataSet, FormStorage.IniFile);
__end;
end;
Routine
SaveFieldsReg
Declaration:
procedure SaveFieldsReg(DataSet: TDataSet; IniFile: TRegIniFile);
SaveFieldsReg procedure saves widths (in pixels) and indexes of dataset's fields to the Windows
System Registry in the key
specified in IniFile parameter. Can be used to store field's
parameters changed by user, for example, in a DBGrid component.
To restore widths and indexes of fields use RestoreFieldsReg
procedure.
SaveFieldsReg example:
procedure
TGridForm.FormStorageSavePlacement(Sender: TObject);
var
__DataSet: TDataSet; I: Integer;
begin
__for I := 0 to TabSet.Tabs.Count - 1 do
__begin
____DataSet := DataSetByIndex(TabSet.TabIndex);
______if (DataSet <> nil) and
(DataSet.Active) then
________if FormStorage.UseRegistry then
______SaveFieldsReg(DataSet,
FormStorage.RegIniFile)
____else
______SaveFields(DataSet, FormStorage.IniFile);
__end;
end;
Index Page | About | Download
Creation Date: 4 Feb 1998 | Last Update: 16 Mar 2000