hi ... Need help
i need code for backup a database..

i want to place the code in a button...

Dani AI

Generated

As asked, and confirmed, this is about Microsoft Access. Two practical patterns work from a button: (A) a simple file copy when the database file is not locked, and (B) a reliable helper-script approach that lets the Access process quit and then copies the file. The helper-script pattern is safest when backing up the running front-end; Microsoft also documents general backup practices for Access here: .

Example: create a small VBS file from VBA that waits for the Access process to release the file, copies the .accdb/.mdb with a timestamped name, then exit. Place this on the button click; it launches the script and then quits Access so the file can be copied.

Sub BackupDatabaseAndQuit()
    Dim sSrc As String, sDst As String, sVBS As String
    Dim sBase As String, sExt As String, iFile As Integer

    sSrc = CurrentProject.FullName
    sBase = Left$(Dir(sSrc), InStrRev(Dir(sSrc), ".") - 1)
    sExt = Mid$(sSrc, InStrRev(sSrc, "."))
    sDst = "C:\Backups\" & sBase & "_" & Format(Now(), "yyyymmdd_hhnnss") & sExt

    sVBS = Environ$("Temp") & "\BackupDB.vbs"
    iFile = FreeFile
    Open sVBS For Output As #iFile
    Print #iFile, "Set fso = CreateObject(""Scripting.FileSystemObject"")"
    Print #iFile, "src = """ & sSrc & """"
    Print #iFile, "dst = """ & sDst & """"
    Print #iFile, "For i = 1 To 20"
    Print #iFile, "  On Error Resume Next"
    Print #iFile, "  fso.CopyFile src, dst, True"
    Print #iFile, "  If Err.Number = 0 Then Exit For"
    Print #iFile, "  Err.Clear"
    Print #iFile, "  WScript.Sleep 500"
    Print #iFile, "Next"
    Print #iFile, "Set fso = Nothing"
    Close #iFile

    Shell "wscript.exe """ & sVBS & """", vbHide
    Application.Quit acQuitSaveNone
End Sub

Notes and tips: ensure the backup folder exists and the running user has write permission; use UNC paths for network shares (\server\share\folder) rather than mapped drives when scheduled or run under different accounts; test the restore process (a backup is only useful if it can be restored); if your database is split, back up the back-end data file frequently rather than only the front-end. If you prefer not to close Access, run the copy from an external process (scheduled task, separate helper DB) that can copy/compact the file when it is not locked.

Recommended Answers

All 2 Replies

You faild to specify what type of database. MySQL, Oracle, Microsoft SQL ????

its access database can u help
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.