Hi all ...
Iam looking for a code on how to backup a database using VB.İt's urgent.:cry:
Printable View
Hi all ...
Iam looking for a code on how to backup a database using VB.İt's urgent.:cry:
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...
Welcome to the forums. :wave:
If you are talking about Access, then bare in mind the database needs to be closed before it can be copied.
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.
I am using Access.but actually ı need the code.ı know ı have to take a backup but how?ı need the code.:(Quote:
Originally Posted by ganeshmoorthy
ganeshmoorthy already told you how.Just bare in mind that the database MUST be closed before you can copy it.Quote:
Originally Posted by ganeshmoorthy
vb Code:
Private Sub Command1_Click() Cn.Close 'close your connection or database object before copying FileCopy SourceFileName, DestinationFileName 'this will copy the db file to your backup folder End Sub
what is cn?is it the name of the database?Quote:
Originally Posted by ganeshmoorthy
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
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 :Quote:
Originally Posted by si_the_geek
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
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
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.
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? ....Quote:
Originally Posted by guncebertug
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"
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.Quote:
ı 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?
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.
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.Quote:
Originally Posted by si_the_geek
Just one more reason to never use databound controls.
hi,Quote:
Originally Posted by Hack
i have the same prob i can copy the file but i need the code to close the connection can you help me?
thanks
Welcome to VBForums :wave:
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.