Results 1 to 15 of 15

Thread: backup database using VB

  1. #1
    New Member
    Join Date
    Dec 06
    Posts
    4

    Thumbs down backup database using VB

    Hi all ...
    Iam looking for a code on how to backup a database using VB.İt's urgent.

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 05
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: backup database using VB

    Hi, Welcome to VBF !

    what is the database that you are using...the simplest method would be just copying the database files to your backup folder...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.
    http://ganeshmoorthymc.com

  3. #3
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: backup database using VB

    Welcome to the forums.

    If you are talking about Access, then bare in mind the database needs to be closed before it can be copied.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,415

    Re: backup database using VB

    And of course if this is MS SQL server you cannot copy the MDF or LDF - that would be a very bad practice...

    See this thread

    http://www.vbforums.com/showthread.p...t=backup+funds

    and there are dozens of others that have discussed this as well.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    New Member
    Join Date
    Dec 06
    Posts
    4

    Re: backup database using VB

    Quote Originally Posted by ganeshmoorthy
    Hi, Welcome to VBF !

    what is the database that you are using...the simplest method would be just copying the database files to your backup folder...
    I am using Access.but actually ı need the code.ı know ı have to take a backup but how?ı need the code.

  6. #6
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: backup database using VB

    ganeshmoorthy already told you how.
    Quote Originally Posted by ganeshmoorthy
    the simplest method would be just copying the database files to your backup folder...
    Just bare in mind that the database MUST be closed before you can copy it.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  7. #7
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 05
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: backup database using VB

    vb Code:
    1. Private Sub Command1_Click()
    2.     Cn.Close 'close your connection or database object before copying
    3.     FileCopy SourceFileName, DestinationFileName 'this will copy the db file to your backup folder
    4. End Sub
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.
    http://ganeshmoorthymc.com

  8. #8
    New Member
    Join Date
    Dec 06
    Posts
    4

    Unhappy Re: backup database using VB

    Quote Originally Posted by ganeshmoorthy
    vb Code:
    1. Private Sub Command1_Click()
    2.     Cn.Close 'close your connection or database object before copying
    3.     FileCopy SourceFileName, DestinationFileName 'this will copy the db file to your backup folder
    4. End Sub
    what is cn?is it the name of the database?
    do we just write the name of the source file name or the path of it?because ı have written the code but it didn't take the backup

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,562

    Re: backup database using VB

    cn is a connection object - based on the assumption that your program has code to work with the database. If that is not the case, simply leave that line out - but note that the FileCopy will fail if any program is using the database.

    Whenever you are specifying file names (in FileCopy or anything else), you should specify the full path too, otherwise you are likely to work with the wrong files!

  10. #10
    New Member
    Join Date
    Dec 06
    Posts
    4

    Thumbs down Re: backup database using VB

    Quote Originally Posted by si_the_geek
    cn is a connection object - based on the assumption that your program has code to work with the database. If that is not the case, simply leave that line out - but note that the FileCopy will fail if any program is using the database.

    Whenever you are specifying file names (in FileCopy or anything else), you should specify the full path too, otherwise you are likely to work with the wrong files!
    I have written the code :

    Cn.Close 'close your connection or database object before copying
    FileCopy "d:\MY VİSUAL BASİC PROJECTS\U mObile\Database\db1", "c:\backup" 'this will copy the db file to your backup folder

    where my database is in D in my visual basic projects
    and the backup will be in C
    however it still didn't work
    ı am using database
    but actually ı dınt understand what ıs connection object
    because the form ı am using to take backup no dataobject is defined.So data connection is data ,ado things like that?
    please find the solution its urgent

  11. #11
    New Member
    Join Date
    Feb 07
    Posts
    4

    Re: backup database using VB

    You can use the common dialogue control. Add the Microsoft Common Dialogue Control by right clicking the toolbox --> components --> then tick Microsoft Common Dialogue Control. put the common dialogue on your form Then use the code below:
    Code:
        Dim fso As Object, MyFile As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        CommonDialog.DialogTitle = " Back Up "
        CommonDialog.Filter = "Microsoft Access Database (*.mdb)|*.mdb"
        CommonDialog.ShowSave
        If CommonDialog.FileName = "" Or mdiscreen.dlgCommonDialog.FileName = " " Then
            MsgBox " You Must Type the Name For New Back up "
        Else
            
            Set MyFile = fso.GetFile(App.Path & "YOUR DATABASE PATH")
            MyFile.Copy CommonDialog.FileName
            MsgBox " BACK UP SUCESSFULLY COMPLETED ", vbInformation, "DataBase Backup"
        End If

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,562

    Re: backup database using VB

    I don't think the common dialog is any use here, as it seems that the file name & path are pre-determined (but I could be wrong!).

    I also think using FSO is overkill here - FileCopy is much simpler.
    Quote Originally Posted by guncebertug
    however it still didn't work
    you should give us more info than that - did it give you an error? (if so, which one?), did it copy the file to somewhere else? ....

    It seems to me that you haven't provided enough info to FileCopy, I think it should be like this:
    Code:
    FileCopy "d:\MY VİSUAL BASİC PROJECTS\U mObile\Database\db1.mdb", "c:\backup\db1.mdb"
    ı am using database
    but actually ı dınt understand what ıs connection object
    because the form ı am using to take backup no dataobject is defined.So data connection is data ,ado things like that?
    As you don't know already, you are not using a connection object - but it sounds as if you are using (evil!) data controls on your forms, which makes this (and many other things) much more difficult.

    In this case, it means that you will probably not be able to copy the file while your program is running (or at least while other forms are open), as data controls stay connected to the database as long as the form they are on is loaded.

  13. #13
    PowerPoster
    Join Date
    Feb 06
    Location
    East of NYC, USA
    Posts
    5,692

    Re: backup database using VB

    Quote Originally Posted by si_the_geek
    In this case, it means that you will probably not be able to copy the file while your program is running
    Copy the file or back up the database in any (practical) way. The database must be closed to back it up, so all forms that have controls bound to the database have to be unloaded first. And that makes for a very messy program.

    Just one more reason to never use databound controls.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  14. #14
    New Member
    Join Date
    May 07
    Posts
    1

    Re: backup database using VB

    Quote Originally Posted by Hack
    Just bare in mind that the database MUST be closed before you can copy it.
    hi,
    i have the same prob i can copy the file but i need the code to close the connection can you help me?

    thanks

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,562

    Re: backup database using VB

    Welcome to VBForums

    How you close it depends entirely on your code to declare (Dim) and open it - if you show us what you have, we can tell you what to do.

    Note that if any other program has a connection open, you will not be able to force that connection to close.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •