hello is it possible for this code to display to a tdbgrid the search results in a list like style? (e.g. if i searched for john, all the data conataining john on a certain field will be displayed to the tdbgrid)
procedure Tspcb.dccolbtnClick(Sender: TObject);
begin
zdctable.First;
while not zdctable.EOF do
begin
if (zdctable.FieldByName('Collector').AsString = dcedit.Text)
then begin
cn.Caption := zdctable.FieldByName('Client_Name').AsString;
col.Caption := zdctable.FieldByName('Collector').AsString;
pay.Caption := zdctable.FieldByName('Daily_Payment').AsString;
date.Caption := zdctable.FieldByName('Date').AsString;
ddate.Caption := zdctable.FieldByName('Due_Date').AsString;
id.Caption := zdctable.FieldByName('ID').AsString;
la.Caption := zdctable.FieldByName('Loan').AsString;
tc.Caption := zdctable.FieldByName('Total_Collectibles').AsString;
end;
ShowMessage('click ok for next profile');
zdctable.Next;
end;
end;
Just add a datasource, set property dataset to your dataset zdctable, add a DBgrid to your form and set the property datasource to the datasource.
The only piece of code you will need is in the OnchangeEvent of dcedit
procedure TForm3.dceditChange(Sender: TObject);
begin
zdctable.FilterOptions:=[foCaseInsensitive]; // if wished
zdctable.Filtered := Length(dcEdit.Text) > 0;
if zdctable.Filtered then
// zdctable.Filter := 'Collector like ' + QuotedStr('%' + dcEdit.Text + '%')
zdctable.Filter := 'Collector like ' + QuotedStr('*' + dcEdit.Text + '*') // Zeos- Syntax
else zdctable.Filter := '';
end;
Related
I am trying to filter an ADOTable for records with a field 'OwnerName' value matches the one in a edit on my GUI component. When entering a name that does not exist, i get the above mentioned error. When there are no names in the database that match the filter, I just want the dbgrid to display nothing, how can this be done? Still a noob, so could anyone enlighten me?
Code used:
procedure TOwners.edt_Name_FilterChange(Sender: TObject);
begin
Filter;
end;
//-----------------------------------------------------------
procedure TOwners.Filter;
begin
if edt_Name_Filter.text = '' then
begin
CarOwners.tbl_Owners.Filtered := false;
exit;
end;
with CarOwners do
begin
tbl_Owners.Filtered := false;
tbl_Owners.Filter := 'OwnerName LIKE ''' + '%' + edt_Name_Filter.text +
'%' + '''';
if tbl_owners['OwnerName'] = null then
begin
tbl_owners.filter := '';
end;
tbl_Owners.Filtered := true;
end;
end;
I have Inherited form and a Ehlib dbgrid on it for selecting-listing records... The form is ready made for a lot of buttons and im using this form with different queries.
Like this...
If Dm.QrTmp.Active then Dm.QrTmp.Active:=False;
Dm.QrTmp.SQL.Clear;
Dm.QrTmp.SQL.Add(' SELECT ');
Dm.QrTmp.SQL.Add(' ch.cari_RECno AS KayitNo ');
Dm.QrTmp.SQL.Add(' FROM CARI_HESAPLAR ch ');
if FrmTmp=nil then FrmTmp:=TFrmTmp.Create(Self);
FrmTmp.StatusBar.Hide;
Dm.QrTmp.Open;
FrmTmp.DbGrid.DataSource:=Dm.DsQrTmp;
This query is cutted down but i have of course use a lot of fields. And Queries changes alot of time in the application.
The problem is column width. Manager wants to set column widths and restore them again. Actually my grid component supports save - restore column properties but as you can see my usage i m not using static columns. also i dont want to use xgrid.columns[0].width percent by percent.
Im using a ini in may app.
I want to add new section on it and named "Gridwidth"...
[Gridname]
Colwidths=x,y,z (where they are width values)
I'm now coding this line by line.
My write procedure is like this.
With dbgridx do
begin
For i:=0 to columns.count-1
begin
widthstr:=widthstr+Column[i].width+',';
end;
end;
Widthstr will be "15,23,45,67" etc...
But i want to know if this is good solution and if somebody know a better way and has some good code.
This should do it:
uses
IniFiles;
const
SETTINGS_FILE = 'Edijus\Settings.ini';
procedure TForm1.LoadDBGridColumnsWidth(const ADBGrid: TDBGrid);
var
_MemIniU: TMemIniFile;
_SettingsPath: string;
i, j: integer;
_ParentClass: TWinControl;
begin
_SettingsPath := GetHomePath + PathDelim + SETTINGS_FILE;
if (not Assigned(ADBGrid)) or (not Assigned(ADBGrid.DataSource)) or
(not Assigned(ADBGrid.DataSource.DataSet)) then
Exit;
_MemIniU := TMemIniFile.Create(_SettingsPath, TEncoding.UTF8);
try
_ParentClass := ADBGrid.Parent;
while not(_ParentClass is TForm) do
_ParentClass := _ParentClass.Parent;
for i := 0 to Pred(ADBGrid.DataSource.DataSet.Fields.Count) do
for j := 0 to Pred(ADBGrid.Columns.Count) do
begin
if (ADBGrid.DataSource.DataSet.Fields[i].FieldName = ADBGrid.Columns[j]
.FieldName) then
ADBGrid.Columns[j].Width :=
_MemIniU.ReadInteger(_ParentClass.Name + '_' + ADBGrid.Name,
ADBGrid.Columns[j].FieldName, 64);
end;
finally
FreeAndNil(_MemIniU);
end;
end;
procedure TForm1.SaveDBGridColumnsWidth(const ADBGrid: TDBGrid);
var
_MemIniU: TMemIniFile;
_SettingsPath: string;
i: integer;
_ParentClass: TWinControl;
begin
_SettingsPath := GetHomePath + PathDelim + SETTINGS_FILE;
if (not Assigned(ADBGrid)) or
(not ForceDirectories(ExtractFilePath(_SettingsPath))) then
Exit;
_MemIniU := TMemIniFile.Create(_SettingsPath, TEncoding.UTF8);
try
_ParentClass := ADBGrid.Parent;
while not(_ParentClass is TForm) do
_ParentClass := _ParentClass.Parent;
for i := 0 to Pred(ADBGrid.Columns.Count) do
if (ADBGrid.Columns[i].FieldName <> '') then
_MemIniU.WriteInteger(_ParentClass.Name + '_' + ADBGrid.Name,
ADBGrid.Columns[i].FieldName, ADBGrid.Columns[i].Width);
_MemIniU.UpdateFile;
finally
FreeAndNil(_MemIniU);
end;
end;
hello i have this code running today but problem is that when there are same data on fields it doesnt cycle but just shows the first data that it filters.
here is the code
ADOTable1.First;
if ADOTable1.Locate('Last', Edit1.Text, []) then
begin
Label1.Caption := ADOTable1.FieldByName('Last').AsString;
Label2.Caption := ADOTable1.FieldByName('First').AsString;
Label3.Caption := ADOTable1.FieldByName('address').AsString;
Next;
end
else
begin
Label1.Caption := '';
Label2.Caption := '';
Label3.Caption := '';
end;
Locate locates the first record that matches the specified criteria in the DataSet.
If a record was found, that record becomes the active/current record.
It cannot be used to locate a "Next" match.
You might want to use a Filter criteria with FindFirst/FindNext e.g.:
DataSet.Filter := 'Last = ''' + Edit1.Text + '''';
if DataSet.FindFirst then
begin
ShowMessage('Found First!');
while DataSet.FindNext do
begin
ShowMessage('Found Next!');
end;
end;
If you want to Filter all records that matches your criteria simply use:
DataSet.Filter := 'Last = ''' + Edit1.Text + '''';
DataSet.Filtered := True; // apply filter for the dataset
Now only those records that meet the filter's conditions are available in the DataSet.
Then iterate the DataSet:
DataSet.First;
while not DataSet.Eof do
begin
// do something with the record
DataSet.Next;
end;
thanks. got this code so far last night
procedure TMain.Button1Click(Sender: TObject);
begin
ADOTable1.First;
while not ADOTable1.EOF do
begin
if (ADOTable1.FieldByName('Last Name').AsString = edit1.Text)
then begin
Label1.Caption := ADOTable1.FieldByName('Last Name').AsString;
Label2.Caption := ADOTable1.FieldByName('First Name').AsString;
Label3.Caption := ADOTable1.FieldByName('MI').AsString;
end;
ShowMessage('click ok for next profile');
ADOTable1.Next;
end;
it maybe same as the code you have given but this code searches for each line on the db grid and thus each line makes me press the ok button once.is it possible to click ok once then the code will search for the next matching data rather than it searching each row?
I have a DB grid which is sorted (the user clicked a few radio buttons and checkboxes to influence the display).
I would like to export all of the data (not just what is visible in the grid), sorted identically, to CSV - how do I do so? The data - not the user settings, just to clarify.
Thanks in advance for any help
[Update] I build sqlQuery bit by bit, depending on the user's settings of checkboxes & radio groups, then, when one of them changes, I
ActivityADQuery.SQL.Clear();
ActivityADQuery.SQL.Add(sqlQuery);
ActivityADQuery.Open(sqlQuery);
That is to say that there isn't a hard coded query, it varies and I want to export the current settings.
I don't know enough if I want to export from the grid or the dataset (I am just not a db guy, this is my first DBgrid), but I suspect that I want the grid, because it has a subset of fields of he dataset.
I guess that TJvDBGridCSVExport is a Jedi component(?) I have tried to avoid them so far, great as they sound, because I prefer discreet, stand-alone, components to installing a huge collection. That may not be the cleverest thing to do, but it's how I feel - ymmv (and prolly does)
Another solution, works also with (multi)selected rows:
procedure TReportsForm.ExportToCSV(const aGrid : TDBGrid; const FileName : String);
Var
I, J : Integer;
SavePlace : TBookmark;
Table : TStrings;
HeadTable : String;
LineTable : String;
First : Boolean;
Begin
HeadTable := '';
LineTable := '';
Table := TStringList.Create;
First := True;
Try
For I := 0 To Pred(aGrid.Columns.Count) Do
If aGrid.Columns[I].Visible Then
If First Then
Begin
// Use the text from the grid, in case it has been set programatically
// E.g., we prefer to show "Date/time" than "from_unixtime(activity.time_stamp, "%D %b %Y %l:%i:%S")"
// HeadTable := HeadTable + aGrid.Columns[I].FieldName;
HeadTable := HeadTable + ActivityReportStringGrid.Columns[i].Title.Caption + ','; // Previous separated wth semi-colon, not comma! (global)
First := False;
End
Else
begin
// HeadTable := HeadTable + ';' + aGrid.Columns[I].FieldName;
HeadTable := HeadTable + ActivityReportStringGrid.Columns[i].Title.Caption + ',';
end;
Delete(HeadTable, Length(HeadTable), 1); // Remove the superfluous trailing comma
Table.Add(HeadTable);
First := True;
// with selection of rows
If aGrid.SelectedRows.Count > 0 Then
Begin
For i := 0 To aGrid.SelectedRows.Count - 1 Do
Begin
aGrid.DataSource.Dataset.GotoBookmark(pointer(aGrid.SelectedRows.Items[i]));
For j := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[J].Visible Then
If First Then
Begin
lineTable := lineTable + aGrid.Fields[J].AsString;
First := False;
End
Else
lineTable := lineTable + ',' + aGrid.Fields[J].AsString;
Delete(LineTable, Length(LineTable), 1); // Remove the superfluous trailing comma
Table.Add(LineTable);
LineTable := '';
First := True;
End;
End
Else
//no selection
Begin
SavePlace := aGrid.DataSource.Dataset.GetBookmark;
aGrid.DataSource.Dataset.First;
Try
While Not aGrid.DataSource.Dataset.Eof Do
Begin
For I := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[I].Visible Then
If First Then
Begin
lineTable := lineTable + aGrid.Fields[I].AsString;
First := False;
End
Else
lineTable := lineTable + ',' + aGrid.Fields[I].AsString;
Delete(LineTable, Length(LineTable), 1); // Remove the superfluous trailing comma
Table.Add(LineTable);
LineTable := '';
aGrid.DataSource.Dataset.Next;
First := True;
End;
aGrid.DataSource.Dataset.GotoBookmark(SavePlace);
Finally
aGrid.DataSource.Dataset.FreeBookmark(SavePlace);
End;
End;
Table.SaveToFile(FileName);
Finally
Table.Free;
End;
End; // ExportToCSV()
You could use a own tiny procedure wich could be adapted to your needs
Procedure Dataset2SeparatedFile(ads: TDataset; const fn: String; const Separator: String = ';');
var
sl: TStringList;
s: String;
i: Integer;
bm: TBookmark;
Procedure ClipIt;
begin
s := Copy(s, 1, Length(s) - Length(Separator));
sl.Add(s);
s := '';
end;
Function FixIt(const s: String): String;
begin
// maybe changed
Result := StringReplace(StringReplace(StringReplace(s, Separator, '', [rfReplaceAll]), #13, '', [rfReplaceAll]), #10, '', [rfReplaceAll]);
// additional changes could be Quoting Strings
end;
begin
sl := TStringList.Create;
try
s := '';
For i := 0 to ads.FieldCount - 1 do
begin
if ads.Fields[i].Visible then
s := s + FixIt(ads.Fields[i].DisplayLabel) + Separator;
end;
ClipIt;
bm := ads.GetBookmark;
ads.DisableControls;
try
ads.First;
while not ads.Eof do
begin
For i := 0 to ads.FieldCount - 1 do
begin
if ads.Fields[i].Visible then
s := s + FixIt(ads.Fields[i].DisplayText) + Separator;
end;
ClipIt;
ads.Next;
end;
ads.GotoBookmark(bm);
finally
ads.EnableControls;
ads.FreeBookmark(bm);
end;
sl.SaveToFile(fn);
finally
sl.Free;
end;
end;
I made a database with paradox 7. The usual search I do is a syntax like this:
Table.Filter := 'Country=' + QuotedStr(Edit.Text);
This returns rows those country field is same as the entered text in edit. when I want to search for countries are beginning by "L" i use this syntax:
Table.Filter := 'Country=' + QuotedStr(Edit.Text + '*');
But how can I search for fields those are finished with "L"? this syntax does not work:
Table.Filter := 'Country=' + QuotedStr('*' + Edit.Text );
Thanks.
You can use the OnFilterRecord event to carry out a customized filtering. With your example, maybe sth. like this:
procedure TForm1.Table1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
var
s: string;
begin
Accept := False;
if not DataSet.FieldByName('Country').IsNull then begin
s := DataSet.FieldByName('Country').AsString;
Accept := Copy(s, Length(s), 1) = 'L';
end;
end;