Home > Back-end >  Ask how to implement accesss multi-table query
Ask how to implement accesss multi-table query

Time:09-27




I have 30 structure are the same table (pictured above), then I want to enter a record of a field (for example serviceID) then traverse the entire database to find the record of all the value of the field, and displayed, the following figure


Get the names of all the tables now I have, but how many table query, I now use the method of cycle if not, don't know what went wrong,
Here is my code, because I am not a professional software, make up good, ask the great spirit guide


 
Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ADODB, DB, Grids, DBGrids, StdCtrls;

Type
TForm1=class (TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
Edit1: TEdit;
BtnSeach: TButton;
MmoServiceActionCN: TMemo;
MmoDeventName: TMemo;
DeventName: TLabel;
ServiceActionCN: TLabel;
MmoSYMEnglish: TMemo;
SYMEnglish: TLabel;
MmoLED: TMemo;
LED: TLabel;
Cbb_DataName: TComboBox;
Procedure FormActivate (Sender: TObject);
Procedure BtnSeachClick (Sender: TObject);
Procedure mmoSYMEnglishClick (Sender: TObject);
Procedure GetDataBaseAllTable ();
Procedure FormCreate (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

Var
Form1: TForm1;
Tempclass: string;//used to store temporary data to realize the combination of search
Tempsex: string;
//get all the table
GetDataBaseAllTableobjTables: TStringList;
TableCount: Integer;//table count
Implementation

{$R *. DFM}

Procedure TForm1. BtnSeachClick (Sender: TObject);
Var
A: Boolean;
TableName: string;
IDName: string;
Index: Integer;
Count: Integer;
The begin
A:=form1. Edit1. Text<> "';
Index:=0;

If a, then
The begin


While indexThe begin
Form1. ADOQuery1. Close;
Form1. ADOQuery1. SQL. The Clear;
Cbb_DataName. ItemIndex:=index;
IDName:=Edit1. Text;//input serviceID

TableName:='select * from + cbb_DataName. Items [index];
Form1. ADOQuery1. SQL. The Add (TableName + 'where ServiceID like' + '" %' + IDName + '%' ");//find the record and then jump out of the loop query?
Form1. ADOQuery1. Open;
Index:=index + 1;


end;


MmoServiceActionCN. Text:=form1. ADOQuery1. FieldByName (' ServiceActionCN). AsString;
MmoDeventName. Text:=form1. ADOQuery1. FieldByName (' DeventName). AsString;
MmoSYMEnglish. Text:=form1. ADOQuery1. FieldByName (' SYMEnglish). AsString;
MmoLED. Text:=form1. ADOQuery1. FieldByName (' status lights). AsString;
Form1. Caption:='event source: + form1. ADOQuery1. FieldByName (' source'). AsString;


end;

end;

Procedure TForm1. FormActivate (Sender: TObject);
Var


Field: the string;
The begin
//combox1 field list is saved in

Form1. ADOConnection1. Close;
Form1. ADOQuery1. Close;
Form1. ADOConnection1. The ConnectionString:='. The Provider=Microsoft Jet. The OLEDB. 4.0; Password=""; Data Source='+ getcurrentdir +' \ Data \ UIH_MR_Event_Log MDB. Persist Security Info=True ';
Form1. ADOConnection1. LoginPrompt:=false;
Form1. ADOQuery1. SQL. The Clear;
GetDataBaseAllTable ();
Field:='select * from + cbb_DataName. The Text;
BtnSeach. Caption:=cbb_DataName. Text;
//form1. ADOQuery1. SQL. The Add (' select * from CBB ');
Form1. ADOQuery1. SQL. The Add (field);

Form1. ADOQuery1. Open;
Form1. ADOConnection1. Open;

Tempclass:='0';
Tempsex:='0';

end;

Procedure TForm1. FormCreate (Sender: TObject);
The begin

end;

Procedure TForm1. MmoSYMEnglishClick (Sender: TObject);
The begin
//change the display in both Chinese and English

end;
Procedure TForm1. GetDataBaseAllTable ();

The begin
GetDataBaseAllTableobjTables:=TStringList. Create;
Try
ADOConnection1. GetTableNames (GetDataBaseAllTableobjTables);
Cbb_DataName. Items. AddStrings (GetDataBaseAllTableobjTables);
TableCount:=GetDataBaseAllTableobjTables. Count;//get the total number of table
//ShowMessage (' there are a few tables: + IntToStr (GetDataBaseAllTableobjTables. Count));
The finally
FreeAndNil (GetDataBaseAllTableobjTables);
end;
Cbb_DataName. ItemIndex:=0;
end;

End.

CodePudding user response:

While indexThe begin
Form1. ADOQuery1. Close;
Form1. ADOQuery1. SQL. The Clear;
Cbb_DataName. ItemIndex:=index;
IDName:=Edit1. Text;//input serviceID

TableName:='select * from + cbb_DataName. Items [index];
Form1. ADOQuery1. SQL. The Add (TableName + 'where ServiceID like' + '" %' + IDName + '%' ");//find the record and then jump out of the loop query?
Form1. ADOQuery1. Open;
Index:=index + 1;
end;

This is you want to do well?

In a cycle is only read the database? Do so meaningless, equivalent to you direct your cbb_DataName list of the last data table,

Can be written as
TableName:='select * from % s where ServiceID like' '% s' ");
IDName IDName:='%' + + '%';
TableName:=Format (TableName, [cbb_DataName Items [index], IDName];
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related