I have the following code, all the code needs to do is go through a list of vehicles and remove the spaces in each registration but before changing it, it should check to make sure the ammended registration doesn’t exist. The following code is what I am using:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, cxLookAndFeelPainters, StdCtrls, cxButtons, Gauges, DB,
DBTables, StrUtils;
type
TfrmMain = class(TForm)
prgTotal: TGauge;
btnStart: TcxButton;
tblVeh: TTable;
tblVehRegNo: TStringField;
procedure btnStartClick(Sender: TObject);
private
procedure OpenTable(pTable: TTable);
procedure CloseTable(pTable: TTable; pPost: Boolean);
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain : TfrmMain;
lvRegLst : TStringList;
lvTblSize : Integer;
lvOrigReg : String;
lvNewReg : String;
lvTest : integer;
implementation
{$R *.dfm}
procedure TfrmMain.btnStartClick(Sender: TObject);
begin
btnStart.Enabled := False;
lvRegLst := TStringList.Create;
// Open Tables
tblVeh.Open;
tblVeh.First;
// Set progress
prgTotal.MinValue := 0;
lvTblSize := tblVeh.RecordCount;
prgTotal.MaxValue := tblVeh.RecordCount;
btnStart.Caption := 'Parsing Registration Numbers...';
// Conversion
while not tblVeh.Eof do
begin
lvRegLst.Add(tblVehRegNo.AsString);
tblVeh.Next;
prgTotal.AddProgress(1);
Application.ProcessMessages;
end;
tblVeh.First;
lvTest := lvRegLst.Count;
prgTotal.Progress := 0;
btnStart.Caption := 'Removing Spaces...';
while not tblVeh.Eof do
begin
lvOrigReg := tblVehRegNo.AsString;
lvNewReg := AnsiReplaceStr(lvOrigReg,' ','');
if lvRegLst.IndexOf(lvNewReg) = -1 then
begin
tblVeh.Edit;
tblVehRegNo.AsString := lvNewReg;
prgTotal.AddProgress(1);
tblVeh.Post;
end;
tblVeh.Next;
prgtotal.AddProgress(1);
Application.ProcessMessages;
end;
// Close Tables
tblVeh.Edit;
tblVeh.Post;
tblVeh.Close;
btnStart.Caption := '&Start Conversion';
btnStart.Enabled := True;
end;
I have stepped through the code and all looks fine and it successfuly changes the registration against the vehicle but when looking at the table afterwards it’s not made any changes.
The issue was with the database itself, it turns out ‘RegNo’ is the only key field so it’s the default index. As my conversion was running through it was changing registrations which moved the ‘cursor’ and skipped over a number of registrations.
I have added another index for the purpose of this conversion but making around 50-60 passes over their data would have eventually sorted out all of the registrations.
Thank you for all of the help.