Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 21st, 2004, 11:52 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Making Copy of Current Access Database

I am trying to make a copy of an existing database from within a VB6 program.
I can create a new database with the appropriate name but when it comes to importing tables it falls over and an empty database only is created. All of my reading within
the books I have doesn't point me in any direction.The following code is what creates the db but not the tables and definitions, it comes from online help examples with VB6:D.
Anybody held.
Regards peterf

Public Function CreateDB()
    Dim newdb As Database, mydb As Database
    Dim dbname As String
    Dim tdf As TableDef
    Dim DateSave, TimeSave
    Dim newdb1
    Dim StoreFolder
    DateSave = Format(Date, "ddmmyy")
    TimeSave = Format(Time, "hhmm")
    newdb1 = TimeSave & " " & DateSave & " Database.mdb"
    StoreFolder = "C:\Database\Backup\"

    'Set the path and name for the new database.
    dbname = StoreFolder & newdb1
    'Create the new database and close newdb.
    Set newdb = DBEngine.Workspaces(0).CreateDatabase(dbname, _
    dbLangGeneral)
    newdb.Close

    'Export all non-system tables to the database.
    Set mydb = CurrentDb()


        For Each tdf In mydb.TableDefs
           If (tdf.Attributes And dbSystemObject) = 0 Then
              DoCmd.TransferDatabase acExport, "Microsoft Access", _
              dbname, acTable, tdf.Name, tdf.Name
           End If
        Next tdf
End Function
 
Old March 22nd, 2004, 03:09 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try to do a filecopy instead.

FileSystemObject.CopyFile sourcefilepath, destinationfilepath



Sal
 
Old March 22nd, 2004, 03:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

besides the filecopy (that's sounds perfect unless you cant close it...)

currentDb()... it's not the DB that just you created??? where is the source DB in there???

HTH

Gonzalo
 
Old March 22nd, 2004, 05:20 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Morning or afternoon as the case may be.
Thanks for the information which I shall try shortly.
In relation to your query Gonzalo the database was opened when the programm was fired up by the normal means,ie by connection string, setting workspaces ,setting the path and database names. The programm is database manipulation for product labels and customer orders, and I don't have just yet a suitable command button solution (automated), I do a copy and paste outside of the programm to backup.
Regards peterf
 
Old March 22nd, 2004, 07:57 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

With a bit of messing around I came up with the following solution but as you said Gonzalo about closing the current database it only takes a second to perform and the end user would/should not be able to tell if the database was open or not. Unless someone can come up with a solution that can do the save with the current database open I think the solution will do as it is not critical if the db is open or not when the save is done

Sub CreateDatabase()

Dim wrkDefault As Workspace
Dim dbsNew As Database
Dim DateSave, TimeSave
Dim newdb
Dim StoreFolder
Dim dbname As String

    DateSave = Format(Date, "ddmmyy")
    TimeSave = Format(Time, "hhmm")
    newdb = TimeSave & " " & DateSave & " Mould.mdb"
    StoreFolder = "C:\Mould\Backup\"

    ' Get default Workspace.
    Set wrkDefault = DBEngine.Workspaces(0)

    ' Make sure there isn't already a file with the name of the new database.
    If Dir(newdb) <> "" Then Kill newdb

    ' Create a new encrypted database with the specified collating order.

    Set dbsNew = wrkDefault.CreateDatabase(StoreFolder & newdb, _
    dbLangGeneral, dbEncrypt)

    With dbsNew
    .Close
    End With

    'Close the current database
    With dbMould
    .Close
    End With

    FileCopy gsDatabase, StoreFolder & newdb

    'reopen the database after copying
    gsConnect = ""
    Set dbWorkspace = DBEngine.Workspaces(0)
    Set Mould = dbWorkspace.OpenDatabase(gsDatabase)

    GetTheDatabase

    MsgBox "The Database has been saved in the Backup Folder", vbCritical, "Save Database"

End Sub

Regards & Thanks
peterf:)
 
Old November 23rd, 2004, 07:16 PM
Registered User
 
Join Date: Nov 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The first code works really well - Thanks!. I am now looking to encode a restore system. For example, I will click a 'restore' button on my form, it will give me a form generating a list of the 'backup' folder contents. I can click on what ever date I choose to back up from, it will then pull in all of the tables from the backed up database trashing out my current ones and I have restored my database. I have hunted the net for hours for a solution to this problem. I know that it is easy to do manually but i would like to offer it from a form in my DB. Any ideas!??!

Dan

Any ideas would be greatly appreciated.






Similar Threads
Thread Thread Starter Forum Replies Last Post
copy an access database and then rename it . yeetesh VB Databases Basics 4 December 27th, 2007 09:26 AM
copy table from one access database to another acc fabdulla SQL Language 5 November 21st, 2007 11:13 AM
Copy all contents except current node in XSLT 2BOrNot2B XSLT 2 December 19th, 2006 06:44 PM
copy an access database and then rename it . yeetesh Beginning VB 6 1 January 7th, 2005 04:40 PM
Restarting the current database GandolftheWhite Pro VB Databases 1 December 27th, 2004 07:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.