Discussion:
Finding if a field name exists...
(too old to reply)
Glenn
2004-09-27 17:13:14 UTC
Permalink
Hi Al

I need to ascertain whether or not a field exists in a paradox table. I am
using delphi 4, and this is proving to be harder than I thought (my fault I
know).... From the help files and google I have pieced together the
following that I have built into a function

function FieldExist(DataSet: TDataSet; FieldName: String): Boolean;
var
findField: TField;
begin
findField := nil;
findField := DataSet.FindField(FieldName);
if Assigned(findField) then CreateField:=False//if field exists do nothing
else CreateField:=True;//else create the field
end;

Where dataset is TTable, and FieldName is the name of the field I am looking
for...

I would expect the CreateField variable (boolean) to be true if the field is
not found and false if the field is found. However I get the same result
whether the filed is in the database or not?

Help much appreciated.


Cheers

Glenn
--
Kind Regards

Glenn Greatwood
Key-Data Systems
www.key-data.co.uk
Bill Todd
2004-09-27 18:03:02 UTC
Permalink
Make sure the case of the field name used in the search matches the
case of the field name in the table. FindField is case sensitive.
--
Bill (TeamB)
TeamB cannot answer questions received via email
Roman Krejci
2004-09-28 07:44:18 UTC
Permalink
Post by Bill Todd
Make sure the case of the field name used in the search matches the
case of the field name in the table. FindField is case sensitive.
Bill, I almost got a heart attack when I read your post.
Fortunately, I reassured myself later that FindField is NOT case sensitive.
VCL code of tDataset.FindField from D2 through D7 uses
AnsiCompareText - which means case non-sensitivity.

Roman
mail: ***@rksolution.cz
URL: www.rksolution.cz
Bill Todd
2004-09-28 15:16:16 UTC
Permalink
Thanks for the correction Roman.
--
Bill (TeamB)
TeamB cannot answer questions received via email
Roman Krejci
2004-09-28 07:50:39 UTC
Permalink
Post by Glenn
Hi Al
I need to ascertain whether or not a field exists in a paradox table. I am
using delphi 4, and this is proving to be harder than I thought (my fault I
know).... From the help files and google I have pieced together the
following that I have built into a function
function FieldExist(DataSet: TDataSet; FieldName: String): Boolean;
var
findField: TField;
begin
findField := nil;
findField := DataSet.FindField(FieldName);
if Assigned(findField) then CreateField:=False//if field exists do nothing
else CreateField:=True;//else create the field
end;
Where dataset is TTable, and FieldName is the name of the field I am looking
for...
I would expect the CreateField variable (boolean) to be true if the field is
not found and false if the field is found. However I get the same result
whether the filed is in the database or not?
Help much appreciated.
Glenn,
I do not think the case you describe is possible.
Where does the CreateField variable come from?
I cannot see it neither in the function local variables,
nor in the argument list. Moreover, your function does not
assign the result, which is suspicious.
Are you sure you are watching the result of FindField function?

Roman
mail: ***@rksolution.cz
URL: www.rksolution.cz
Glenn
2004-09-28 10:29:50 UTC
Permalink
Hello Roman

Thanks for your comments

I have re-written the function;

function FieldExist(DataSet: TDataSet; FieldName: String): Boolean;
var
findField: TField;
begin
findField := nil;
If DataSet.FindField(FieldName)=nil then CreateField:=True//if field not
found create it
else CreateField:=False;//else do nothing
end;


CreateField is a variable (boolean) defined elsewhere in the unit and if
true then some other code adds the field, if false the add field code is
"Moreover, your function does not assign the result, which is suspicious"
means....

Have I missed something? I have not used functions much to date.

Thanks

Glenn
Post by Glenn
Hi Al
I need to ascertain whether or not a field exists in a paradox table. I am
using delphi 4, and this is proving to be harder than I thought (my
fault
I
Post by Glenn
know).... From the help files and google I have pieced together the
following that I have built into a function
function FieldExist(DataSet: TDataSet; FieldName: String): Boolean;
var
findField: TField;
begin
findField := nil;
findField := DataSet.FindField(FieldName);
if Assigned(findField) then CreateField:=False//if field exists do
nothing
Post by Glenn
else CreateField:=True;//else create the field
end;
Where dataset is TTable, and FieldName is the name of the field I am
looking
Post by Glenn
for...
I would expect the CreateField variable (boolean) to be true if the
field
is
Post by Glenn
not found and false if the field is found. However I get the same result
whether the filed is in the database or not?
Help much appreciated.
Glenn,
I do not think the case you describe is possible.
Where does the CreateField variable come from?
I cannot see it neither in the function local variables,
nor in the argument list. Moreover, your function does not
assign the result, which is suspicious.
Are you sure you are watching the result of FindField function?
Roman
URL: www.rksolution.cz
Roman Krejci
2004-09-28 13:29:30 UTC
Permalink
Post by Glenn
Hello Roman
Thanks for your comments
I have re-written the function;
function FieldExist(DataSet: TDataSet; FieldName: String): Boolean;
var
findField: TField;
begin
findField := nil;
If DataSet.FindField(FieldName)=nil then CreateField:=True//if field not
found create it
else CreateField:=False;//else do nothing
end;
CreateField is a variable (boolean) defined elsewhere in the unit and if
true then some other code adds the field, if false the add field code is
"Moreover, your function does not assign the result, which is suspicious"
means....
Have I missed something? I have not used functions much to date.
Thanks
Glenn
Please show us the code where you do call
the FieldExist function an inspect the value
of CreateField variable.

roman
Glenn
2004-09-28 14:24:12 UTC
Permalink
Hi Roman

CheckFields is a TTable
CreateField is a boolean variable

procedure TWelcome.FormCreate(Sender: TObject);
Var
Data, S1, S2: String;
Begin
Screen.cursor:=crHourglass;
//Check necessary fields and letters exist if not create them...

CreateField:=False;
CheckFields.TableName:='Prefs.DB';
CheckFields.open;
FieldExist(CheckFields,'CheckAppId');//sends the params to the function that
checks field exists
CheckFields.close;

If CreateField=True then
begin
//the field doesn't exist so create it
Data:='Prefs.DB';
With UpdateTables do
begin
Sql.Clear;
s1:='Alter Table '+Chr(39)+Data+Chr(39);
Sql.Add(S1);
s2:='Add Column CheckAppId Boolean';
Sql.Add(S2);
ExecSql;
end;
With SetDefault do//and set the default
begin
Sql.Clear;
SQL.ADD('Update Prefs');
SQL.ADD('Set CheckAppId=False');
SQL.ADD('WHERE');
SQL.ADD('CheckAppId is null');
ExecSql;
end;
end;
Screen.cursor:=crdefault;
end;

Regards

Glenn
Post by Roman Krejci
Post by Glenn
Hello Roman
Thanks for your comments
I have re-written the function;
function FieldExist(DataSet: TDataSet; FieldName: String): Boolean;
var
findField: TField;
begin
findField := nil;
If DataSet.FindField(FieldName)=nil then CreateField:=True//if field not
found create it
else CreateField:=False;//else do nothing
end;
CreateField is a variable (boolean) defined elsewhere in the unit and if
true then some other code adds the field, if false the add field code is
"Moreover, your function does not assign the result, which is suspicious"
means....
Have I missed something? I have not used functions much to date.
Thanks
Glenn
Please show us the code where you do call
the FieldExist function an inspect the value
of CreateField variable.
roman
Roman Krejci
2004-09-28 17:00:45 UTC
Permalink
Post by Glenn
Hi Roman
CheckFields is a TTable
CreateField is a boolean variable
procedure TWelcome.FormCreate(Sender: TObject);
Var
Data, S1, S2: String;
Begin
Screen.cursor:=crHourglass;
//Check necessary fields and letters exist if not create them...
CreateField:=False;
CheckFields.TableName:='Prefs.DB';
CheckFields.open;
FieldExist(CheckFields,'CheckAppId');//sends the params to the function that
checks field exists
CheckFields.close;
OK, the picture is slowly emerging...
You wrote that CreateField is a boolean variable.
From your code I would conclude that
CreateField is a global variable -
definitely CreateField is NOT field of
TWelcome form (otherwise your code for
FieldExist function would not compile).
Is it possible that you have declared more
than one variable named CreateField ?

roman

Continue reading on narkive:
Loading...