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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rockiroadsConnect With a Mentor Commented:
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.
0
 
NorieVBA ExpertCommented:
Are these databases going to be used separately with each user having their own data?
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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.
0
 
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.
0
 
Dale FyeConnect With a Mentor Commented:
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.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)Connect With a Mentor PresidentCommented:
<<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.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
@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.
0
 
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!
0
All Courses

From novice to tech pro — start learning today.