Compare two records in the same table (Views: 27)
Problem/Question/Abstract: Is there any way to compare two records from the same table to know which fields are different on each record? Both my field are the same except that some field may differ, and I want to know which one is different. Answer: I would create a second TTable and then compare using the Fields array. Give the following function a TTable and a field position to start the compare. If it returns false bgnFld will reflect the first field where values are not the same. (This code has not been tested): function compairFields( t: TTable; var bgnFld: Integer ): Boolean; var t2: TTable; cntr: Integer; begin try t2 := ttable.create(nil) with T do begin t2.gotoCurrent(t); {synchronize tables} for cntr := bgnfld to FieldCount - 1 do begin result := (fieldscntr] = T2.fields[cntr]); if not result then Break; end; end; finally t2.free; end; end; or use two TTables one pointing to each of the two records. Then: for I := 0 to Table1.FieldCount - 1 do if Table1.Fields[I].Value <> Table2.Fields[I].Value then ... |