jupe
asked on
creating a table
I create a table like this:
Procedure CreateTable;
Var Table:TTable;
begin
Table:=TTable.Create(Form1 );
Table.Active:=False;
Table.DatabaseName:='MyDB' ;
Table.TableName:='Position en';
Table.TableType:=ttPARADOX ;
With Table.FieldDefs DO
begin
Clear;
Add('Position',ftAutoInc,0 ,True);
Add('Anzahl',ftFloat,0,Fal se);
Add('Artikel',ftString,80, False);
Add('Preis',ftFloat,0,Fals e);
Add('Rabatt',ftFloat,0,Fal se);
Add('MWSt',ftString,10,Fal se);
end;
With Table.IndexDefs Do
begin
Clear;
Add('','Position',[ixUniqu e,ixPrimar y]);
end;
Table.CreateTable;
Table.Free;
end;
I would like to add an automatic maximum-check for the 'Rabatt'-field (<=50) and a default value of 1 for the 'Anzahl'-field. How can I include this in my 'Procedure CreateTable' ? (Delphi 4)
Procedure CreateTable;
Var Table:TTable;
begin
Table:=TTable.Create(Form1
Table.Active:=False;
Table.DatabaseName:='MyDB'
Table.TableName:='Position
Table.TableType:=ttPARADOX
With Table.FieldDefs DO
begin
Clear;
Add('Position',ftAutoInc,0
Add('Anzahl',ftFloat,0,Fal
Add('Artikel',ftString,80,
Add('Preis',ftFloat,0,Fals
Add('Rabatt',ftFloat,0,Fal
Add('MWSt',ftString,10,Fal
end;
With Table.IndexDefs Do
begin
Clear;
Add('','Position',[ixUniqu
end;
Table.CreateTable;
Table.Free;
end;
I would like to add an automatic maximum-check for the 'Rabatt'-field (<=50) and a default value of 1 for the 'Anzahl'-field. How can I include this in my 'Procedure CreateTable' ? (Delphi 4)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks jsweby and meikl. Your help is very much appreciated.
I hoped I could do the trick when defining/creating the table. I thought it should be possible, as you can do it with dbd32.exe.
At runtime it's just changing the maxvalue property of the field.
I give the points to jsweby and will post another 100 points for meikl.
jupe
I hoped I could do the trick when defining/creating the table. I thought it should be possible, as you can do it with dbd32.exe.
At runtime it's just changing the maxvalue property of the field.
I give the points to jsweby and will post another 100 points for meikl.
jupe
(an older sample)
//Set a Valcheck, valcheck may not exist
procedure SetValCheck(Tbl: TTable; Field: TField; MinVal, MaxVal,
DefVal: Pointer; MinSize,MaxSize,DefSize : Integer; Required: Bool);
var
hDb: hDbiDb;
TblDesc: CRTblDesc;
VChk: pVChkDesc;
Dir: String;
NumVChks: Word;
OpType: CROpType;
begin
NumVChks := 0;
SetLength(Dir, dbiMaxNameLen + 1);
Check(DbiGetDirectory(Tbl.
SetLength(Dir, StrLen(PChar(Dir)));
VChk := AllocMem(sizeof(VChkDesc))
try
FillChar(TblDesc, sizeof(CRTblDesc), #0);
VChk.iFldNum := Field.Index + 1;
Tbl.DisableControls;
Tbl.Close;
Check(DbiOpenDatabase(nil,
nil, 0, nil, nil, hDb));
Check(DbiSetDirectory(hDb,
with VChk^ do
begin
bRequired := Required;
if MinVal <> nil then
begin
Inc(NumVChks);
bHasMinVal := True;
move(MinVal^, aMinVal, MinSize);
end
else
bHasMinVal := False;
if MaxVal <> nil then
begin
Inc(NumVChks);
bHasMaxVal := True;
move(MaxVal^, aMaxVal, MaxSize);
end
else
bHasMaxVal := False;
if DefVal <> nil then
begin
Inc(NumVChks);
bHasDefVal := True;
move(DefVal^, aDefVal, DefSize);
end
else
bHasDefVal := False;
end;
TblDesc.iValChkCount := NumVChks;
TblDesc.pVChkDesc := VChk;
OpType := crADD;
TblDesc.pecrValChkOp := @OpType;
StrPCopy(TblDesc.szTblName
StrCopy(TblDesc.szTblType,
Check(DbiDoRestructure(hDb
finally
Check(DbiCloseDatabase(hDb
FreeMem(VChk, sizeof(VChkDesc));
Tbl.EnableControls;
Tbl.Open;
end;
end;
//Help Function to encode bde-date
Function GetBDEDate(Month, Day : Word; Year : SmallInt; var MyDate : DbiDate) : DbiDate;
begin
Check(DbiDateEncode(Month,
Result := Mydate;
end;
//Table must be opened exclusive,
//a validate check may not exist
procedure TForm1.Button2Click(Sender
var
cmin,cmax,cdef : Double;
lmin,lmax,ldef : Longint;
amin,amax,adef : String;
bdef : Boolean;
dmin,dmax,ddef : dbiDate;
begin
//Set validate for numeric Field
cmin := -2.5;
cmax := 2.5;
cdef := 0.01;
SetValCheck(Table6,Table6.
SizeOf(cMin),SizeOf(cMax),
//Set Validate for Integer Field
lmin := -100;
lmax := 100;
ldef := 1;
SetValCheck(Table6,Table6.
SizeOf(lMin),SizeOf(lMax),
//Set Validate for Alphanumeric Field
amin := 'A';
amax := 'ABCDE';
adef := 'AB';
SetValCheck(Table6,Table6.
length(amin),length(aMax),
//Set Validate for Boolean Field (Default only)
bdef := False;
SetValCheck(Table6,Table6.
0,0,SizeOf(bdef),True);
//Set Validate for DateField
dMin := GetBDEDate(1,1,1999,dmin);
dMax := GetBDEDate(12,31,1999,dmax
dDef := GetBDEDate(7,1,1999,ddef);
SetValCheck(Table6,Table6.
SizeOf(dMin),SizeOf(dMax),
end;
the bde-unit must be added to the uses clause to get it work
meikl ;-)