Transferdatabase

QUINABA

Board Regular
Joined
Jul 18, 2002
Messages
127
Hi All,

Is there a way to transfer a database object (table, query...) to a new database (non-existing) with a database name I can specify using macro or module? I have task that I do weekly to create a table from a master table and put it in a new database. I find it cumbersome to always have to create a blank database and then importing the new table. I hoping to automate this process.

Any help is greatly appreciated.

Thanks. (y)
 
Thanks, I guess I can use your solution on another project. I'm looking to make it work in a macro any clues?
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Is it possible to have a user specify the save location from a text box on a form. I know you can do it in Excel by specifying the Range and cell. Something like Form name and text box 1 or what ever, instead of C:\…….
 
Upvote 0
If you change it form a Sub to a function the you should be able to call it from the Macro then.

Code:
Function CreateNewDB() 

Dim wrk As Workspace 
Dim dbNew As Database 
    
    Set wrk = DBEngine.Workspaces(0) 
        
    Set dbNew = wrk.CreateDatabase("C:\My Documents\NewDatabase.mdb", dbLangGeneral, dbEncrypt) 

    Set dbNew = Nothing 
End Function

How were you planning on running the Macro?

Peter
 
Upvote 0
Thanks Bat17 and to all that pitched in! It works great. I'll be integrating the code with a macro that pulls out a weekly data by a specific region from a flat file, create the blank database then transfer the table. Then, I'll manually change the name of the database to a more descriptive name.

:pray:
 
Upvote 0
I missed the bit obout naming! You can change the name of the db in the code as you create it. You could either pop up an input box to ask for user input or contruct from date etc, or even combine both methods.

Code:
Function CreateNewDB()
Dim wrk As Workspace
Dim dbNew As Database
Dim strName As String
Dim strDefault As String

strDefault = "Accounts" & Format(DATE, "YYYYMMDD")
strName = InputBox("Enter name for new Database", "Database Name", strDefault)
If strName = "" Then Exit Function ' action canceled
Set wrk = DBEngine.Workspaces(0)
Set dbNew = wrk.CreateDatabase("C:\My Documents\" & strName & ".mdb", dbLangGeneral, dbEncrypt)
Set dbNew = Nothing
End Function

If you need to go further it is possible to use the common dialog controls to get a standard windows file save dialog box up.
 
Upvote 0
Once the New database is created and have imported the table how can I rename the database in following format "NewDatabase_yyyymmdd_hhnnss")?
 
Upvote 0
No need to rename it, just create it with the name that you want
Code:
Function CreateNewDB()
Dim wrk As Workspace
Dim dbNew As Database
Dim strName As String
strName = "NewDatabase_" & Format(Now, "yyyymmdd_hhnnss")
Set wrk = DBEngine.Workspaces(0)
Set dbNew = wrk.CreateDatabase("C:\My Documents\" & strName & ".mdb", dbLangGeneral, dbEncrypt)
Set dbNew = Nothing
Set wrk = Nothing
End Function

Peter
 
Upvote 0
Thanks, I still think I need to rename, here's the sequence of what I've trying to do in the macro:

- Create table from flat file
- Create blank database
- transfer table from master database to the new blank database
- Rename the database with transferred table

If I create the blank database already formatted in "NewDatabase_yyyymmdd_hhnnss" i won't be able to accurately put the name in the Destination argument in the TransferDatabase action. Let me know if I'm wrong in my thinking or if I theres a better way.
 
Upvote 0
How about the Name statement

Name oldpathname As newpathname
 
Upvote 0

Forum statistics

Threads
1,221,771
Messages
6,161,847
Members
451,723
Latest member
Rachetsely

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top