Can I use an existing table in Access to create code to build an identical table

Greetings!

I have an application that uses an access database with several large tables.  I want to write a program that will dynamically build the database for new users instead of having to provide them with a copy of a blank database.

I prefer not to have to manually type up the code to create each of the tables with the correct field names, type, and sizes.  I figure that I could load the table and go through the fields collection and append each field to a build a create table command.

But is there an automated way that would do this for me from an existing table structure.
lthamesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Are these databases going to be used separately with each user having their own data?
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Simple way:  take a look at the TransferDatabase method, which allows you to copy objects from one DB to another.

Keep a empty table in a "master" DB and then use that to create your new DB.

If the tables are dynamic, then either you can go the DAO route as you described (add a tabledef, fields, indexes, etc) or you can use SQL DML (Data Manipulation Language) statements such as CREATE table

Describe a bit more what it is your doing  and we'll get you started.

Jim.
lthamesAuthor Commented:
imnorte:   This is to set up a new database for someone who buys the the application.  Right now we have to provide a empty database but I would really like for the install to create the database and then build the tables.  Or at least start with an empty database and have the install build the tables.

Jim,  What I actually want to do is have the file built dynamically.  I know how to create the tables from my programming language with the create command. . . . . and I know I can write a program to go through all of the table defs to get what I need to build the tables.   I was just hoping there was a shortcut that would take existing tables and actually create the "CREATE TABLE . . . . " command to use.
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

dqmqCommented:
Supporting Jim....TransferDatabase is the method of choice.  

Probably, I would make an Access application to create the new db, then do a series of import/export steps. In that application, use TransferDatabase to import the tables you want.  You can use the Structure-Only option so you don't need to maintain an empty table.  The use TransferDatabase to export the table to the New database.  

You can use the same approach for forms, queries, reports and other database objects.
Dale FyeOwner, Developing Solutions LLCCommented:
I'm not sure I understand why you want to go to all this trouble, rather than just distributing a blank database?

Do you intend to just send them an mde of accde file?

If you are not going to use any sort of install program, and you are using Access 2007/2010, then you might want to consider adding a table to your application front-end and storing the blank database as an attachment in that table.

Then during the first run of the application, your code could check to see whether the database exists on the users system and if not, extract the blank database from that table.  This would save you A LOT of effort.

I don't use attachments much, but this is the way I deploy occasionally deploy images and templates for various Word or Excel files that go with my Access applications.
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
<<Jim,  What I actually want to do is have the file built dynamically.  I know how to create the tables from my programming language with the create command. . . . . and I know I can write a program to go through all of the table defs to get what I need to build the tables.   I was just hoping there was a shortcut that would take existing tables and actually create the "CREATE TABLE . . . . " command to use.>>

 Not like your thinking no.    Just use TransferDatabase method then or possibly even more simply, do a file copy of a "master template" DB you provide and rename it when you copy it.  Then you have a complete DB ready for them quick and easy.

And since you'd never touch the master copy, you can always create more.

Jim.
rockiroadsCommented:
fyed mentioned distributing a blank db. Personally I think that is the best approach, one with the least number of headaches, very low maintenance as you just need to update your install with the new table, not the scripts itself. The ready made db has everything you need for the setup. Any lookup tables will exist with data already defined etc (if you have such a thing). If your tables change often you can copy the db then create scripts to truncate the tables (or use what Jim said).

Other option is for you to maintain sql scripts that create tables, setup indexes etc. This you run on your installs. Maintenance is higher here though.
If you do not have them you can use a tool like dbwscript to generate your initial scripts and then maintain them or use the sample code that is here. These should be treated as something to get you started with creating the scripts, not the tool to do everything for you.
Remember once the initial scripts have been done, you have to remember to modify them as and when new tables are created or existing tables are modified.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
@rocki,

  Really nice to see you popup...it's been too long!

  Drop me a line at jimdettman"at"earthlink.net.  Would love to hear what you think about the new site.

Jim.
lthamesAuthor Commented:
Although none offered the approach I was looking for, each offered insight into a better solution than I was wanting to implement anyway.

Thanks to all of you for your input!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.