Link to home
Start Free TrialLog in
Avatar of sbahri
sbahri

asked on

Checking is_required property of ms access table fields

Hi,
Im using the following code for checking the 'nullable' property of ms access table fields with name 'tablename' and field 'fieldname' passed as variables to the function and returning 'NOT NULL' string. The function should check if the field is a 'is_required' field type.
Unfortunately, Im not getting the desired result ie. Primary key fields dont return a 'NOT NULL'. Any idea why this might be? Any help will be much appreciated

function GetRequired(ADOConnection: TADOConnection; tablename:string; fieldname:string):string;
var
fieldreq: TADODataSet;
i: integer;
comparefield: integer;
output: boolean;
begin
  fieldreq := TADODataSet.Create(nil);
  fieldreq.Connection := ADOConnection;
try
ADOConnection.OpenSchema(siColumns,VarArrayOf([Unassigned,Unassigned,tablename,Unassigned]),EmptyParam,fieldreq);
 
for i := 0 to fieldreq.RecordCount-1 do
  begin
comparefield :=AnsiCompareStr(fieldreq.FieldByName('COLUMN_NAME').AsString, fieldname);
if comparefield = 0 then begin
output := fieldreq.FieldByName('IS_NULLABLE').AsBoolean;
  if output
    then fieldreq.Next
  else result := 'NOT NULL';
end
else
fieldreq.Next;
end;
finally
fieldreq.Free;
end;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial