Solved

creating a table

Posted on 2001-08-18
3
223 Views
Last Modified: 2010-04-04
I create a table like this:

Procedure CreateTable;  
Var Table:TTable;
begin
      Table:=TTable.Create(Form1);
       Table.Active:=False;
       Table.DatabaseName:='MyDB';
       Table.TableName:='Positionen';
       Table.TableType:=ttPARADOX;        
        With Table.FieldDefs DO
         begin
          Clear;
          Add('Position',ftAutoInc,0,True);
          Add('Anzahl',ftFloat,0,False);
          Add('Artikel',ftString,80,False);
          Add('Preis',ftFloat,0,False);
          Add('Rabatt',ftFloat,0,False);
          Add('MWSt',ftString,10,False);
         end;
       With Table.IndexDefs Do
        begin
          Clear;
          Add('','Position',[ixUnique,ixPrimary]);
        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)
0
Comment
Question by:jupe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
jsweby earned 100 total points
ID: 6399902
I don't think you can do this using the TTable.CreateTable method; in fact, you are hard pushed to do it even using more complex SQL unless you're using a solid database such as MS SQL Server. Paradox is a very limited database and the CreateTable method doesn't cater for these extra constraints.

However, once created, you can set contraints against individual fields in Delphi. With your TTable component, double-click on it to open the Fields Editor. Right-click in that, and select Add All Fields. Now you can select the fields individually and add constraints to them that are handled by Delphi at run-time, but I'm almost positive that you can't get Paradox to handle them for you.

J.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6400535
nothing is impossible :-)
(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.DBHandle, False, PChar(Dir)));
  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, dbiReadWrite, dbiOpenExcl,
               nil, 0, nil, nil, hDb));
    Check(DbiSetDirectory(hDb, PChar(Dir)));
    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, Tbl.TableName);
    StrCopy(TblDesc.szTblType, szParadox);
    Check(DbiDoRestructure(hDb, 1, @TblDesc, nil, nil, nil, False));
  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, Day, Year, MyDate));
  Result := Mydate;
end;


//Table must be opened exclusive,
//a validate check may not exist
procedure TForm1.Button2Click(Sender: TObject);
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.FieldByName('Numeric'),@cmin,@cmax,@cdef,
              SizeOf(cMin),SizeOf(cMax),SizeOf(cdef),True);

  //Set Validate for Integer Field
  lmin := -100;
  lmax := 100;
  ldef := 1;
  SetValCheck(Table6,Table6.FieldByName('Integer'),@lmin,@lmax,@ldef,
              SizeOf(lMin),SizeOf(lMax),SizeOf(ldef),True);

  //Set Validate for Alphanumeric Field
  amin := 'A';
  amax := 'ABCDE';
  adef := 'AB';
  SetValCheck(Table6,Table6.FieldByName('Alpha'),@amin[1],@amax[1],@adef[1],
              length(amin),length(aMax),length(adef),True);

  //Set Validate for Boolean Field (Default only)
  bdef := False;
  SetValCheck(Table6,Table6.FieldByName('Boolean'),Nil,nil,@bdef,
              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.FieldByName('Date'),@dmin,@dmax,@ddef,
              SizeOf(dMin),SizeOf(dMax),SizeOf(ddef),True);

end;


the bde-unit must be added to the uses clause to get it work

meikl ;-)

0
 

Author Comment

by:jupe
ID: 6400658
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

623 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