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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I backup ADO mdb database 1

Status
Not open for further replies.

Nikosis

Programmer
Aug 30, 2010
16
US
Hi
I tried to find something on the internet, but there is no much about backing up .mdb database in c++ builder.
How can I accomplish such a task ?

Thx
Andy
 
The easiest way is to stop Access and use the copy API to copy the files.


James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
I tried, everything compiles without errors but nothing happend, no files were copied.
Here is my code
Code:
#include <vcl.h>
#include<shlobj.h>
#include<SHELLAPI.H>
#include<cstdio>
#include<fstream>
using namespace std;
#pragma hdrstop

#include "Exercise.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
  //    SaveDialog = new TSaveDialog(Form1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    delete SaveDialog;
    SaveDialog = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
if (OpenDialog->Execute() == true) {
     ShowMessage("File:" + OpenDialog->FileName);
     Edit1->Text =  OpenDialog->FileName;
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
    if (SaveDialog->Execute() == true) {
        Edit2->Text = SaveDialog->FileName;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    char FileFrom[MAX_PATH], FileTo[MAX_PATH];
    Edit1->Text = "C:\\Users\\Andrew\\Desktop\\zzz.txt";
    Edit2->Text = "C:\\Users\\Andrew\\Desktop\\back.txt";
    const String fileFrom = Edit1->Text.c_str();
    const String fileTo = Edit2->Text.c_str();
    CopyFile(FileFrom, FileTo, false ) ;
}
 
Try using the SHFILEOPSTRUCT. I've had much better luck with it. Something like this.

Code:
    // Set up
    Screen->Cursor = crHourGlass;

    // From files
    AnsiString TempStr = Edit1->Text.c_str() + '\0';  // Double null termination
    AnsiString FromFilesStr = TempStr;

    // To location
    TempStr = Edit2->Text.c_str() + '\0';    // Double null termination
    AnsiString ToFilesStr = TempStr;

    try
    {
        SHFILEOPSTRUCT FileOp;
        ::ZeroMemory(&FileOp, sizeof(FileOp));

        FileOp.wFunc = FO_COPY;         // copy
        FileOp.pFrom = FromFilesStr.c_str();    // Local DB files
        FileOp.pTo = ToFilesStr.c_str();        // Network file location
        FileOp.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
        //FileOp.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
        SHFileOperation(&FileOp);
    }
    __finally
    {
     Screen->Cursor = crDefault;
    };

NOTE: I "kludged" this together from your files and one of mine so watch out for bugs if you copy this directly. Look at your help files for more info.

James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Hey it worked perfect, now I'm gonna try it with .mdb file.
Thanks a lot 2ffat
 
Hmmm...This is the Windows API. It is the same as if you went to Explorer and copied the file through that. What happens if you do that?

It suspect that something is locking or reading the file every so often.

James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Have you tried copying the file through Explorer? Does it have the same results?

If so, you need to find out what is holding the file open. You may need to use something like Handle or Process Explorer. If you can't stop the process that is holding the files open, then you have two options that I can see. One) you have use something like MoveFiles or Two) open the file and copy the data to another file in C++ record by record. That usually ends up in a text or CSV file instead of a .mdb file, though.

James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
I'll try
Thanks for advice, let you know how it went.

Thx
Andy
 
You were right, something was holding it.
It did work out at last. It was this admin permission thing to copy a file that resides in Program files. It was kind a strenge, because I didn't get any message.
Well anyway it worked out. After I reinstalled W7 even without admin permission.
One question thou, is there any way to get these shiny W7 progress bars to work.

Thanks for your help.
Andy
 
Please disregard last question, suddenly after reinstall, progress bar works as well.

Thx
Andy
 
Cool! :) BTW, if you look at the help file for the SHFILEOPSTRUCT API, you can have several options as to what you see and when.


James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
I'll look it up.
Can you tell me why sometimes it creates directory instead a file.Right now I'm getting a directory named dbbackup.mdb instead a file?

Thx
Andy
 
Look at the options you're using. The API must be confused. That would be my first guess.

James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top