Usage :
---------------------------------------------------------------------
procedure TForm1.Button2Click(Sender: TObject);
var
aFieldName:String;
begin
//GivenString = Edit1 Generated Basic FieldName is aFieldName
aFieldName := GetFieldName(Edit1.Text);
//Give to field the fieldname count extension '1' and
//Check in the dataset if this fieldname allready exists then inc count
exam field1---> field2
//the finalresult is in Edit2.Text
//we suppose that the target dataset wich gonna added the new field is
ClientDataSet1 then
Edit2.Text := aFieldName+GetFieldCnt(aFieldName,ClientDataSet1);
end;
----------------------------------------------------------------------
function GetFieldName(aStr: String): String;
var
i:Integer;
begin
Result := EmptyStr;
aStr := Trim(LowerCase(aStr));
For i:=1 to length(aStr) do
begin
If (Ord(aStr[i]) in [97..122]) or (Ord(aStr[i]) in [48..57])then
Result := Result+aStr[i];
end;
If Result = EmptyStr then
Result := 'field';
end;
function GetFieldCnt(aFieldName: String;aDataSet: TDataSet): String;
var
i:Integer;
aCnt:Integer;
begin
aCnt := 1;
If Assigned(aDataSet) then
begin
For i:=0 to Pred(aDataSet.Fields.Count) do
begin
If
IsSameFieldName(aFieldName,LowerCase(aDataSet.Fields[i].FieldName)) then
Inc(aCnt);
end;
end;
Result := IntToStr(aCnt);
end;
function IsSameFieldName(aFieldName1, aFieldName2: String): Boolean;
var
j:Integer;
begin
Result := False;
If Length(aFieldName1) <= Length(aFieldName2) then
begin
Result := True;
For j:=1 to Length(aFieldName1) do
begin
Result := (Result) and (aFieldName1[j] = aFieldName2[j])
end;
If Result then
begin
If Length(aFieldName1) < Length(aFieldName2) then
begin
For j:= Length(aFieldName1)+1 to Length(aFieldName2) do
Result := Result and (Ord(aFieldName2[j]) in [48..57])
end;
end;
end;
end;
P.s.
Afcource the 2 function GetFieldName and GetFieldCnt Could be one
Post by Alejandro CastroIs there a function to create field names of a Table?
I have a string "My name", I want to generate a field name like "myname1"
(Removing all the invalid characters)
Thanks
--
Alejandro Castro
www.alfra.info