PC Review


Reply
Thread Tools Rate Thread

Automatically generate a "create table" statement

 
 
Rob
Guest
Posts: n/a
 
      11th Mar 2010
Has anyone here written up (or know of) some code to automatically generate a
"CREATE TABLE" SQL statement based upon the structure of an existing table?
I'm thinking of a VBA function that will return the appropriate statement
given the name of a table in the db.

Along the same lines, how about the appropriate "ALTER TABLE" statement to
build index(es?) that exist on a table?

MySQL has a cute little feature that does this, I would have thought that
Access does but I haven't seen it....

Thanks.
 
Reply With Quote
 
 
 
 
Marshall Barton
Guest
Posts: n/a
 
      11th Mar 2010
Rob wrote:
>Has anyone here written up (or know of) some code to automatically generate a
>"CREATE TABLE" SQL statement based upon the structure of an existing table?
>I'm thinking of a VBA function that will return the appropriate statement
>given the name of a table in the db.
>
>Along the same lines, how about the appropriate "ALTER TABLE" statement to
>build index(es?) that exist on a table?



Why on earth would you want to do such a thing? It sounds
like a severe violation of the Normalization Rules.

Because it should not be necessary to do that, the examples
for it are few and far between. It's pretty advanced DAO
programming, but doable.

--
Marsh
MVP [MS Access]
 
Reply With Quote
 
Jeff Boyce
Guest
Posts: n/a
 
      11th Mar 2010
Rob

I'm with Marsh, ... why?

If you'll describe a bit more about what business need you figure this
approach will help you solve, folks here may be able to offer alternate
approaches.

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

"Rob" <(E-Mail Removed)> wrote in message
news:38D2EE4C-CCBE-4767-946E-(E-Mail Removed)...
> Has anyone here written up (or know of) some code to automatically
> generate a
> "CREATE TABLE" SQL statement based upon the structure of an existing
> table?
> I'm thinking of a VBA function that will return the appropriate statement
> given the name of a table in the db.
>
> Along the same lines, how about the appropriate "ALTER TABLE" statement to
> build index(es?) that exist on a table?
>
> MySQL has a cute little feature that does this, I would have thought that
> Access does but I haven't seen it....
>
> Thanks.



 
Reply With Quote
 
Rob
Guest
Posts: n/a
 
      11th Mar 2010


"Marshall Barton" wrote:

> Rob wrote:
> >Has anyone here written up (or know of) some code to automatically generate a
> >"CREATE TABLE" SQL statement based upon the structure of an existing table?
> >I'm thinking of a VBA function that will return the appropriate statement
> >given the name of a table in the db.
> >
> >Along the same lines, how about the appropriate "ALTER TABLE" statement to
> >build index(es?) that exist on a table?

>
>
> Why on earth would you want to do such a thing? It sounds
> like a severe violation of the Normalization Rules.
>
> Because it should not be necessary to do that, the examples
> for it are few and far between. It's pretty advanced DAO
> programming, but doable.
>



I think you would want to do this for the same reason one records Excel
macros, then alters that code to fit one's true needs: it's a lot easier to
build a table using the GUI than with a CREATE TABLE statement. And if you
have situations, as I do, where tables are frequently built by importing
Excel data ranges, it would be nice to be able to duplicate the structure of
the data as the TransferSpreadsheet routine understands it.

I just want to be able to store the table structure of built tables in code
so that I can drop and re-create tables easily.

I don't really think the process should be THAT hard: the code should build
a skeleton

CREATE TABLE myTableName ( .......... )

and fill in the details by running through each field in the tableDef and
iteratively adding a

field_i type [(size)] [NOT NULL] [WITH COMP] [,]

clause as necessary. The only thing I don't know is how to find/interpret
the type and size from the tableDef attributes.

Thanks,
 
Reply With Quote
 
Marshall Barton
Guest
Posts: n/a
 
      12th Mar 2010
Rob wrote:
>"Marshall Barton" wrote:
>> Rob wrote:
>> >Has anyone here written up (or know of) some code to automatically generate a
>> >"CREATE TABLE" SQL statement based upon the structure of an existing table?
>> >I'm thinking of a VBA function that will return the appropriate statement
>> >given the name of a table in the db.
>> >
>> >Along the same lines, how about the appropriate "ALTER TABLE" statement to
>> >build index(es?) that exist on a table?

>>
>>
>> Why on earth would you want to do such a thing? It sounds
>> like a severe violation of the Normalization Rules.
>>
>> Because it should not be necessary to do that, the examples
>> for it are few and far between. It's pretty advanced DAO
>> programming, but doable.
>>

>
>
>I think you would want to do this for the same reason one records Excel
>macros, then alters that code to fit one's true needs: it's a lot easier to
>build a table using the GUI than with a CREATE TABLE statement. And if you
>have situations, as I do, where tables are frequently built by importing
>Excel data ranges, it would be nice to be able to duplicate the structure of
>the data as the TransferSpreadsheet routine understands it.
>
>I just want to be able to store the table structure of built tables in code
>so that I can drop and re-create tables easily.
>
>I don't really think the process should be THAT hard: the code should build
>a skeleton
>
> CREATE TABLE myTableName ( .......... )
>
>and fill in the details by running through each field in the tableDef and
>iteratively adding a
>
> field_i type [(size)] [NOT NULL] [WITH COMP] [,]
>
>clause as necessary. The only thing I don't know is how to find/interpret
>the type and size from the tableDef attributes.



You can get the type codes by looking up Type Property (DAO)
in VBA Help. Then use a Select Case to translate the
numeric code to the text you need in the query.

The size for Text fields come directly from the Size
property. Size is not used for number and date type fields.

--
Marsh
MVP [MS Access]
 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      12th Mar 2010
"Rob" <(E-Mail Removed)> wrote in message
news:6099D0A8-26C7-48C3-A7DC-(E-Mail Removed)...
>
> I think you would want to do this for the same reason one records Excel
> macros, then alters that code to fit one's true needs: it's a lot easier
> to
> build a table using the GUI than with a CREATE TABLE statement. And if
> you
> have situations, as I do, where tables are frequently built by importing
> Excel data ranges, it would be nice to be able to duplicate the structure
> of
> the data as the TransferSpreadsheet routine understands it.
>
> I just want to be able to store the table structure of built tables in
> code
> so that I can drop and re-create tables easily.


Access tables and the fields in them have properties that you can't set via
SQL. If you want to copy all attributes of the table, you can do it in a
couple of ways. One simple one is to use TransferDatabase, specifying the
current database as the source for an import. For example:

DoCmd.TransferDatabase _
acImport, "Microsoft Access", CurrentDb.Name, _
acTable, "Table1", "Table1_Copy",
True

It is also possible to copy the table via DAO, but the process is quite
involved. There is code posted in the Microsoft KB somewhere for doing
this.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Programmatically set "automatically generate microsoft exchange views" ? Neal Microsoft Outlook 6 3rd Oct 2007 04:53 PM
Insert Page # as form of 2/12, when "12" is automatically generate =?Utf-8?B?QWxhbg==?= Microsoft Powerpoint 5 31st May 2007 01:19 PM
Looking for a macro to automatically generate numbers in "cut-stack" order... gamouning@gmail.com Microsoft Excel Discussion 5 8th Aug 2006 01:29 AM
Automatically create "Description" and check "Hidden Attribute" Tom Microsoft Access Forms 4 10th Nov 2003 01:51 PM
View "Create table" statement of an existing table =?Utf-8?B?S2ltIFRyYW4=?= Microsoft Access 5 7th Nov 2003 02:29 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:14 PM.