To know that back up is complete (1 Viewer)

hfsitumo2001

Member
Local time
Yesterday, 20:35
Joined
Jan 17, 2021
Messages
290
This is my VBA to create the back up file, when I pressed the botton, the back up is created. I waited for 2 minutes. How can I make it that there is a message to say, "backup complete" when it finished.
Code:
Public Function CreateBackup() As Boolean
Dim Source As String
Dim Target As String
Dim objFSO As Object
Dim Path As String

Source = CurrentDb.Name
'Path = CurrentProject.Path
Path = "C:\EVSInventoryBkup"
Target = Path & "\EVSbkupDB_" & Format(Now, "mm-dd-yy-hh-mm-ss") & ".accdb"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.folderExists(Path) Then
    a = objFSO.copyfile(Source, Target, True)
Else
    objFSO.Createfolder (Path)
    a = objFSO.copyfile(Source, Target, True)
End If
Set objFSO = Nothing
Set a = Nothing

End Function
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 20:35
Joined
Aug 30, 2003
Messages
35,342
How about putting a message box at the end?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 22:35
Joined
Feb 28, 2001
Messages
20,672
To the best of my understanding, and I would be willing for another member to correct me on this, but...

Using FSO, you are using SYNCHRONOUS routines. That is to say, this is a single-threaded operation for which there is no "background operation." Once your code returns from the .CopyFile operation, I believe you are effectively done. At the very least there will be no data transfers remaining. At worst, the folder in the target might not update instantly - but the copy should already be complete. Therefore, just before your "end function" you can put up your message box.

HOWEVER, you are not returning a value to your function and I think that might lead to confusion. I don't know what the program will return in that case. But it won't be because of anything to do with the file copy operation.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 20:35
Joined
Aug 30, 2003
Messages
35,342
But Paul.. are you sure that when the message box will pop up, after the copying is done?

No, but it's worth testing. Test with a file you know takes a few seconds.
 

hfsitumo2001

Member
Local time
Yesterday, 20:35
Joined
Jan 17, 2021
Messages
290
No, but it's worth testing. Test with a file you know takes a few seconds.
I have tested Paul, it seems it works, because it takes a few seconds for the message to pop up.

Thanks to all of you
 

hfsitumo2001

Member
Local time
Yesterday, 20:35
Joined
Jan 17, 2021
Messages
290
No, but it's worth testing. Test with a file you know takes a few seconds.
Paul, if I want to make a message box to pop up when we open the form and when it shows up, then a message box pop up, in what even I put it, because when I put on load even, the message box shows up first, but I want it message box will show up later
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:35
Joined
May 7, 2009
Messages
13,784
you can use the Timer event.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:35
Joined
May 7, 2009
Messages
13,784
add Timer Interval.
the value is in millisec (before the timer is triggered).
1000 msec = 1 sec.

if you want to show the msgbox 5 seconds after the form is shown, put 5000 to the Timer Interval value.

you also need to add Code to the timer event.
when the timer kicks, you immediately disable the timer (so as it only fires once):

Private Sub Form_Timer()
'kill the timer
Me.TimerInterval = 0
'you msgbox/or any code here
End Sub
 

hfsitumo2001

Member
Local time
Yesterday, 20:35
Joined
Jan 17, 2021
Messages
290
add Timer Interval.
the value is in millisec (before the timer is triggered).
1000 msec = 1 sec.

if you want to show the msgbox 5 seconds after the form is shown, put 5000 to the Timer Interval value.

you also need to add Code to the timer event.
when the timer kicks, you immediately disable the timer (so as it only fires once):

Private Sub Form_Timer()
'kill the timer
Me.TimerInterval = 0
'you msgbox/or any code here
End Sub
Thank you Arnel, you are awesome, it works like a chant.
 

apr pillai

Registered User.
Local time
Today, 09:05
Joined
Jan 20, 2005
Messages
714
The FileCopy Source, Destination statement of VBA copies an external file (not the Currentdb) from Source location to Target location.
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 23:35
Joined
Jan 23, 2006
Messages
13,839
Here's a backup routine that puts timed start and finish messages on the immediate window and uses FSO.

Code:
' ----------------------------------------------------------------
' Procedure Name: make_BackUp
' Purpose: Routine to Backup the current database to a target folder and
'          print the start and finsih messages to the immediate window
'
'   ** this routine calls function GetDBName ********************
' Procedure Kind: Sub
' Procedure Access: Public
' Author: Jack
' Date: 15-Feb-21
' ----------------------------------------------------------------
Public Sub make_BackUp()

10        On Error GoTo make_BackUp_Error
          Dim Source As String
          Dim Target As String
          Dim retval As Integer
         
20        Debug.Print Now & " - Starting backup"

30        Source = CurrentDb.Name
         
          'Path of where you want the backup to be saved, by default I set to downloads
40        Target = "C:\Users\jack\downloads\"
50        Target = Target & GetDBName(Source) & "_BKUP_" & Format(Now, "YYYY-mm-dd--hh-nn-ss") & ".accdb"

          ' create the backup
60        retval = 0
          Dim objFSO As Object
70        Set objFSO = CreateObject("Scripting.FileSystemObject")
80        retval = objFSO.CopyFile(Source, Target, True)
90        Set objFSO = Nothing

100       Debug.Print Now & " - Finished backup " _
          & vbCrLf & vbTab & Source _
          & vbCrLf & vbTab & Target
         
110       On Error GoTo 0
make_BackUp_Exit:
120       Exit Sub

make_BackUp_Error:
130       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure make_BackUp, line " & Erl & "."
140       GoTo make_BackUp_Exit
End Sub

' ----------------------------------------------------------------
' Procedure Name: GetDBName
' Purpose: Routine to get the database name ONLY from the currentdb.name without the extension
' Procedure Kind: Function
' Procedure Access: Public
' Parameter fullDBpath (String): currentdb.name
' Return Type: String
' Author: Jack
' Date: 15-Feb-21
' ----------------------------------------------------------------
Function GetDBName(fullDBpath As String) As String
          Dim NameStartPos As Integer
          Dim NameEndPos As Integer
         
10        NameStartPos = InStrRev(fullDBpath, "\") + 1
20        NameEndPos = Len(fullDBpath) - (InStrRev(fullDBpath, "\") - 6)
         
30        GetDBName = Mid(fullDBpath, NameStartPos, Len(fullDBpath) - NameEndPos)
End Function

Sample output:

15-Feb-21 10:35:45 AM - Starting backup
15-Feb-21 10:35:45 AM - Finished backup
C:\Users\Jack\Documents\EPPINGTONHOMESKILL.accdb
C:\Users\jack\downloads\EPPINGTONHOMESKILL_BKUP_2021-02-15-10-35-45.accdb
 

FrankRuperto

Member
Local time
Yesterday, 23:35
Joined
Mar 6, 2021
Messages
182
If you want to get fancy, you can compile this vb.net project into an exe and use a shortcut to pass the source and target folders as arguments. When you run it, it will display the familiar Windows FileCopy Dialog.

UIdialog.PNG


Code:
Imports Microsoft.VisualBasic.FileIO
Imports System.Environment
Module Module1

    Public Sub Main()

        If GetCommandLineArgs().Count <> 3 Then
            Console.WriteLine("Missing from or to folder or too many values")
            Exit Sub
        End If

        Dim strSourceFolder As String = GetCommandLineArgs(1)
        Dim strTargetFolder As String = GetCommandLineArgs(2)

        My.Computer.FileSystem.CopyDirectory(strSourceFolder, strTargetFolder, UIOption.AllDialogs)

    End Sub

End Module
 

Users who are viewing this thread

Top Bottom