Friday, 15 February 2013

delphi - Search database table by record fields and display record on cxgrid -


What is the best way to find a record in a database table by typing a test by clicking a user in a test box A button then results will be displayed on a TCXgrid.

I have a tedchorage / datasite which is looking at a table that contains different fields. It would be good if the user could search more than one field in the table.

Thank you.

You can use TDataSet.Locate for this, match To match an array of delimited list and consecutive field values ​​from a semicolon. Generally, this is simple:

  DataSet.Locate ('field1; field2', ['value1', 'value2'], [loparty]);  

However, as you do not know how many columns are next time, you will need to handle the array differently, using VarArrayCreate and each Set the array value separately

This example takes a list of fields from Edit1 (separated by semicolons), the values ​​list to match Edit2 (again, different By the semicolon, with the string values ​​surrounded by the ' characters, so that they fit properly in the array). It divides the contents of Edit1 into an array, to find out how many elements allocate a different array of appropriate size, populate it, and then field lists and values The array of TDataSet.Locate . This Edit1.Text: = 'Customer_No; Name '; and Edit2.Text: = '1;' 'Smith' ''; .

was tested with.
  Process TForm5.Button1Click (Sender: Tubbed); Var temp: string; Field: TARRA & lt; String & gt; Walls: TARRA & lt; String & gt; Fieldviews: variants; I: integer; Start / Break a copy so that we can divide it into different values ​​temp: = Edit1.Text; Field: = Temp.Split ([';']); // Create an array of variants to capture the values ​​of the field: Field Value: = VarArrayCreate ([0, High (field)], Variable); // Temporary Copy: To allow partitioning in different values: Temp: = Edit2.Text; Wall: = Temp.Split ([';']); I: FieldValues ​​for fields = 0 [i]: = Vals [i]; // Use the original field list from Edit1 for Locate operation DataSet1.Locate (Edit1.Text, FieldValues, [loCaseInsensitive]); End; Before joining   

TStringHelper for versions of Delphi (as you are using XE2, simply type and StrUtils and SplitString and TStringDynArray instead:

  Process TForm5.Button1Click (Sender: TObject); Temperature: String; Fields: TStringDynArray; Vals: TStringDynArray; FieldValues: Version; I: Integer; Start Temp: = Edit1.Text; Fields: = SplitString (Temporary, ';'); FieldValues: VarArrayCreate ([0, Length (Fei ); VarVariant; temporary: = Edit2.Text; Vals: = SplitString (for temporary, ';'); for I: = 0 high for Fields Field Values ​​[i]: = Vals [i] ]; DataSet1.Locate (Temp, FieldValues, [loCaseInsensitive]); end;  

No comments:

Post a Comment