Solved

Checking is_required property of ms access table fields

Posted on 2008-10-26
1
518 Views
Last Modified: 2013-11-29
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

0
Comment
Question by:sbahri
1 Comment
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 125 total points
ID: 22810450
sbahri,

Primary keys, by design, cannot be null.
So even if you set the Required property to No, you will not be able to leave it blank.
So you may want to somehow exclude/ignore them in your code.

JeffCoachman
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question