Board index » delphi » Again..HOWTO Create TTable on the fly....

Again..HOWTO Create TTable on the fly....

Hello all,
I know I have seen this addressed before, but, I have lost all references to it
(I should know better that to clean my office).  I am trying to create and
manipulate tables hooking to an MS SQL server programiatically. Basically I am
using some code I inherited to read from a file that used to be hooked to a
form which had TTable components hooked to local tables.  I have gotten rid of
the forms and am re-directing the files to a table I have on a MS SQL
server6.0.  How do I go about doing a create table?  Where do I declare it in
the unit?  Here is what I am starting with that does not work.  Also, do I have
to declare all of the fields that will be going into the TTAble, and if so
where?  Here is what I am starting with:

var

 tblMERs: TTable;
    tblMERsNumber: TSmallintField;
    tblMERsName: TStringField;
    tblMERsType: TSmallintField;
    tblMERsGroupNumber: TSmallintField;
    tblMERsElementGroups: TStringField;
    tblMERsHeading: TStringField;
............etc

const
   strNetworkMERDataFile =  'o:\inf\mer\';
   strLaptopMERDataFile =  'c:\ales\mer\';

......etc

procedure  DoIt ;
begin
   if FileExists(strNetworkMERDataFile + 'database.dat') then
     begin
       strMERDirectory := strNetworkMERDataFile;
       getDatabaseNames(strNetworkMERDataFile + 'database.dat')
     end
   else
     begin
       strMERDirectory := strLaptopMERDataFile;
       getDatabaseNames(strLaptopMERDataFile + 'database.dat');
     end;

   with tblMERs do
   begin
        Active := False;
        DatabaseName := 'InfoBaseSQL';
        TableName := 'dbo.MasterElement';
        CreateTable;
   end;

Here in the last part in the code is where I am trying to create the table
tblMERs.  When it hits this part, I get the Access Violation.

Any help to get me pointed in the right direction will be greatly appreciated.
 Everything I have done so far has involved using forms and dropping the
components in.

Thanx!!!

Kelly
kgr...@acxiom.com

 

Re:Again..HOWTO Create TTable on the fly....


In article <4n89e5$...@ren.cei.net>, kgr...@acxiom.com says...

Quote

>Hello all,
>I know I have seen this addressed before, but, I have lost all references to
it
>(I should know better that to clean my office).  I am trying to create and
>manipulate tables hooking to an MS SQL server programiatically. Basically I am
>using some code I inherited to read from a file that used to be hooked to a
>form which had TTable components hooked to local tables.  I have gotten rid of
>the forms and am re-directing the files to a table I have on a MS SQL
>server6.0.  How do I go about doing a create table?  Where do I declare it in
>the unit?  Here is what I am starting with that does not work.  Also, do I
have
>to declare all of the fields that will be going into the TTAble, and if so
>where?  Here is what I am starting with:
><snip>

Hi Kelly,

  You need to create the tblMERs object first like this:

     tblMERs := TTable.Create( nil );

  You're getting an access violation because the object doesn't exist yet.

  The parameter to create is for the owner of the new TTable component.
  You can give it some other existing component, like a form, and then
  the form will automatically destroy the table when it is destroyed.
  Otherwise, if you use nil as in the above example, you will have to
  destroy it yourself like this:

    tblMERs.Free;

Hope this helps,
Steve

Re:Again..HOWTO Create TTable on the fly....


Quote
kgr...@acxiom.com (Kelly) wrote:
>Hello all,
>I know I have seen this addressed before, but, I have lost all references to it
>(I should know better that to clean my office).  I am trying to create and
>manipulate tables hooking to an MS SQL server programiatically. Basically I am
>using some code I inherited to read from a file that used to be hooked to a
>form which had TTable components hooked to local tables.  I have gotten rid of
>the forms and am re-directing the files to a table I have on a MS SQL
>server6.0.  How do I go about doing a create table?  Where do I declare it in
>the unit?  Here is what I am starting with that does not work.  Also, do I have
>to declare all of the fields that will be going into the TTAble, and if so
>where?  Here is what I am starting with:
>var
> tblMERs: TTable;
>    tblMERsNumber: TSmallintField;
>    tblMERsName: TStringField;
>    tblMERsType: TSmallintField;
>    tblMERsGroupNumber: TSmallintField;
>    tblMERsElementGroups: TStringField;
>    tblMERsHeading: TStringField;
>............etc
>const
>   strNetworkMERDataFile =  'o:\inf\mer\';
>   strLaptopMERDataFile =  'c:\ales\mer\';
>......etc
>procedure  DoIt ;
>begin
>   if FileExists(strNetworkMERDataFile + 'database.dat') then
>     begin
>       strMERDirectory := strNetworkMERDataFile;
>       getDatabaseNames(strNetworkMERDataFile + 'database.dat')
>     end
>   else
>     begin
>       strMERDirectory := strLaptopMERDataFile;
>       getDatabaseNames(strLaptopMERDataFile + 'database.dat');
>     end;
>   with tblMERs do
>   begin
>        Active := False;
>        DatabaseName := 'InfoBaseSQL';
>        TableName := 'dbo.MasterElement';
>        CreateTable;
>   end;
>Here in the last part in the code is where I am trying to create the table
>tblMERs.  When it hits this part, I get the Access Violation.
>Any help to get me pointed in the right direction will be greatly appreciated.
> Everything I have done so far has involved using forms and dropping the
>components in.
>Thanx!!!
>Kelly
>kgr...@acxiom.com

Hi Kelly...

I've done this before, but since I don't have the source code here (at
home), I'm doing this from memory.  I struggled through this, and
finally was able to create a table using CreateTable.

Before CreateTable, you need to have four things.  DatabaseName and
TableName and TableType are pretty simple, so I won't go into those.
The last one, FieldDefs is the tricky part.  Here's roughly how it's
done:

procedure CreateMyTable;
var
  MyTable: TTable;
begin
  with MyTable do
  begin
    DatabaseName := <your database or alias>;
    TableName := <table to be created>;
    TableType := ttDefault;  {this will go to whatever format
                            DatabaseName supports}
    with FieldDefs do
    begin
      Clear;
      Add('Field1', ftInteger, 0);  {creates an integer field}
      Add('Field2', ftString, 25);  {creates a 25 char string}
      Add('Field3), ftBlob, 0);      {creates a blob}
    end;
    CreateTable;
  end;
end;

If you want to set up indexes, you can do this too.

Good luck!

Mark Fruchtman

Other Threads