Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Objects in One Database From Another Database

Status
Not open for further replies.

dpimental

Programmer
Jul 23, 2002
535
US
I would like to (and now be honest, wouldn't everybody) be able to create various objects in one database from another database. Here's the senario.

1. Open Database 1

2. Run a function / method.

3. The Method Creates Database 2.

4. The Method Opens Database 2 (using OpenDatabase Method).

6. The Method sets Database 2 as CurrentDb.

5. The Method creates (from vba code) various objects (i.e. imported tables, creates forms and reports, ect).

6. The Method sets Database1 as CurrentDb

7. The Method closes Database 2 and Sets it to Nothing.

Now, can it be done. Can you create a db on the fly (yes); but can you create objects in that db, without creating them in the original (Database 1) and then exporting them to Database 2
 
Yup.

From database1

Dim db As DAO.Database
Dim td As DAO.TableDef, fld As DAO.Field
Dim qd As DAO.QueryDef

Set db = CreateDatabase("C:\Database2.mdb", dbLangGeneral)
Set td = db.CreateTableDef("tblData")
Set fld = td.CreateField("ID", dbBigInt)
td.Fields.Append fld
Set fld = td.CreateField("Name", dbText)
td.Fields.Append fld
db.TableDefs.Append td

Set qd = db.CreateQueryDef("qryData", "SELECT * FROM tblData;")


and so on.
Infact anything currentdb can be replaced with db for almost anything you like.

HTH

Ben ----------------------------------------
Ben O'Hara
Home: bpo@SickOfSpam.RobotParade.co.uk
Work: bo104@SickOfSpam.westyorkshire.pnn.police.uk
(in case you've not worked it out get rid of Sick Of Spam to mail me!)
Web: ----------------------------------------
 
Oharab, can you create forms and reports? Do you have any examples?

Also, can you run functions from Database 1 that can run on Database 2?

Also, do you use a db.close and a set db = Nothing
when the function is finished?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top