Page 1 of 2 12 LastLast
Results 1 to 40 of 52

Thread: [RESOLVED] DB Backup and Restore

  1. #1

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Resolved [RESOLVED] DB Backup and Restore

    I have come accross of this Thread post #5. It was posted 2004 but it is very helpful and I am going to add this code to my app. Cudos to RhinoBull.

    I have some question though. I have been reading alot from the thread that before backing up the Database is should be closed. Im sorry, I have to ask this question. Is it not I have to put a button on a form and on click the button it will back-up the DB. If so then, the DB is open because the program is running. How can I back-up then?

    Also I want to check whether a file already exist or not and want to auto back-up the DB say before the beginning of each month (should detectwhether at the time of the scheduled back-up there is no work (ex: Holiday, Saturday or Sunday) so the back-up will not take place).

    Any solution for this?
    Last edited by Simply Me; May 14th, 2006 at 04:25 AM.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DB Backup and Restore

    That is because it was assumed that you were connecting to the db using ADO or some other data access object connectivity.

    It sounds like your wanting to do this from withing Access itself (VBA)?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    Quote Originally Posted by RobDog888
    That is because it was assumed that you were connecting to the db using ADO or some other data access object connectivity.

    It sounds like your wanting to do this from withing Access itself (VBA)?
    I am using ADODB. In my program i have a menu Backup Database (using Treeview). When Clicked a form will be shown. I put the code of RhinoBull in a button, it is working fine. In doing this, the DB is open right?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DB Backup and Restore

    Correct, your ADODB connection object(s) and recordsets need to be closed in order to prevent any database corruption and possibility of loosing any recordset data.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    so how do i do back-up then if i risk losing any data or worse DB corruption? I want to back up my database either automatically or when the user find it so to back it up. Here's the code from RhinoBull and i tried it and its working.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     'check for archive folder and create one if it doesn't exist
    5.     If Dir(App.Path & "\Archive", vbDirectory) = "" Then
    6.         MkDir App.Path & "\Archive"
    7.     End If
    8.     'check for error folder and create one if it doesn't exist
    9.     If Dir(App.Path & "\Error", vbDirectory) = "" Then
    10.         MkDir App.Path & "\Error"
    11.     End If
    12. End Sub
    13.  
    14. Private Sub Command1_Click()
    15.     ArchiveDB App.Path & "\MasterDB.mdb", _
    16.               App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb"
    17. End Sub
    18.  
    19. Public Function ArchiveDB(sSource As String, sDestination As String) As Boolean
    20. '===============================================================================
    21. On Error GoTo ErrHandler
    22.    
    23.     FileCopy sSource, sDestination
    24.     Exit Function
    25.  
    26. ErrHandler:
    27. '------------
    28.     Open App.Path & "\Error\erros.txt" For Append As #1
    29.         Print #1, Now() & vbTab & Err.Number & ": " & Err.Description
    30.     Close #1
    31.     Exit Function
    32.  
    33. End Function
    Last edited by RobDog888; May 14th, 2006 at 04:13 AM. Reason: Fixed [vbcode] tags
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DB Backup and Restore

    Before you call ArchiveDB, close your connection(s) like so.

    VB Code:
    1. oCnn.[color=black]Close[/color]
    2. ArchiveDB App.Path & "\MasterDB.mdb", App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb"
    3. oCnn.Open
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    thanks!
    how about this concern?
    Also I want to check whether a file already exist or not and want to auto back-up the DB say before the beginning of each month (should detectwhether at the time of the scheduled back-up there is no work (ex: Holiday, Saturday or Sunday) so the back-up will not take place).
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DB Backup and Restore

    You can use the Dir() function to test for the file existance or a more complex but reliable way like my code here - http://www.vbforums.com/showpost.php...10&postcount=3
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Fanatic Member vivek_master146's Avatar
    Join Date
    Apr 2006
    Location
    Delhi,India
    Posts
    787

    Re: DB Backup and Restore

    If u want to check whether a file exists or not then this is the code:
    VB Code:
    1. Dim afile As String
    2. Private Sub Command1_Click()
    3. afile = Dir$("c:\My Documents\smashing.exe") <> ""
    4. If afile Then
    5. MsgBox "File exist"
    6. Else
    7. MsgBox "File does not exist"
    8.  
    9. End If
    10.  
    11. End Sub
    Dont rely only on your luck. Work hard until You get success.
    vb Code:
    1. Private sub Time_ispassing
    2. While Me.Notgetsuccess
    3. trygain=tryagain+1
    4. Me.workhard
    5. wend
    6. end sub

  10. #10

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    Quote Originally Posted by RobDog888
    You can use the Dir() function to test for the file existance or a more complex but reliable way like my code here - http://www.vbforums.com/showpost.php...10&postcount=3
    Great code RobDog888. I'll just do some modification in such a way that it would give the user an option to use save as function.

    the autoback-up can it be done?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  11. #11

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    I added the code below to let the user save the DB to a new file.
    VB Code:
    1. 'Check if File Exists or Not
    2.   If FileExists(App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb") = 1 Then
    3.     If (MsgBox("File Exists! Save it using New Filename?", vbInformation + vbYesNo) = vbYes) Then
    4.       With CommonDialog1
    5.         'Set Filters
    6.         .Filter = "Access Database Files (*.mdb)|*.mdb|"
    7.         ' Display the Open dialog box
    8.         .ShowSave
    9.           'App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb", _
    10.           'App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb", 0
    11.       End With
    12.     Else
    13.       Exit Sub
    14.     End If
    15.   Else
    16.     'oConn.Close 'Needed to protect the DB from possible corruption
    17.     ArchiveDB App.Path & "\MasterDB.mdb", _
    18.       App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb"
    19.     'oConn.Open
    20.     'MsgBox "File Not Found!"
    21.   End If
    This works, however, I want that when the user will save the DB using another Name the commondialog will open the path \Archive\....
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  12. #12

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    When I try to change the filename and click save button there was no error but there was no file saved at all. How come?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  13. #13

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    I just added this line after .showsave and it worked except that when i clicked cancel without typing any filename i get an error path/file access error.I also need to know is how to put default filename. The default filename should increment like in Microsoft word Document1, document2 and so on.

    VB Code:
    1. Open cdbDialog.FileName For Output As #1
    2. Print #1,
    3. Close #1

    Any suggestions or code snippet?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  14. #14
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: DB Backup and Restore

    If you are interested in compacting the DB also.. here is an example ...
    i use this in my ASP stuff mostly but also works in VB. Basically it takes
    The source file and makes a compacted backup.

    Code:
    Public Function CompactDatabase(source, destination)
    
        Dim objJetEngine
        Set objJetEngine = CreateObject("JRO.JetEngine")
    
        objJetEngine.CompactDatabase _
            "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" _
            & source & ";Jet OLEDB:Database Password=mypass;", _
            "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" _
            & destination & ";Jet OLEDB:Database Password=mypass;"
    
        Set objJetEngine = Nothing
    
    End Function

  15. #15
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: DB Backup and Restore

    Updated ..
    Also Turn on Common Dialog's Cancel Error in Properties.

    Code:
    Private Sub Command1_Click()
    
    Const ProgName = "Export Database"
    Dim sFile As String
    Dim sFileName As String
    
    On Error GoTo err_cancel:
        sFile = Format(Now, "mm_dd_yyyy") & ".mdb"
        If Dir$(App.Path & "\Archive\" & sFile) <> vbNullString Then
            If (MsgBox("File Exists! Save it using New Filename?", vbInformation + vbYesNo, ProgName) = vbYes) Then
                CommonDialog1.InitDir = App.Path & "\Archive\"
                CommonDialog1.Filter = "Mdb Files (*.mdb)|*.mdb|All Files (*.*)|*.*"
                CommonDialog1.FileName = sFile
                CommonDialog1.DialogTitle = "Export Database"
                CommonDialog1.ShowSave
                sFileName = CommonDialog1.FileName
                FileCopy App.Path & "\MasterDB.mdb", sFileName
            Else
                Exit Sub
            End If
        Else
            If Dir$(App.Path & "\Archive\", vbDirectory) = vbNullString Then
                Call MkDir(App.Path & "\Archive")
            End If
            sFileName = App.Path & "\Archive\" & sFile
            FileCopy App.Path & "\MasterDB.mdb", sFileName
        End If
        If Dir$(sFileName) <> vbNullString Then
            MsgBox "Export Completed", vbInformation, ProgName
        Else
           MsgBox "Export Error!", vbInformation, ProgName
        End If
            Exit Sub
    err_cancel:
            Exit Sub  
    End Sub
    Last edited by rory; May 14th, 2006 at 11:45 AM.

  16. #16

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    Great code! thanks rory. Maybe instead of autoback. I would like that VB detect whether its the first day of the month and prompt the user to back up the database(assuming there is work on that day). If however, the first day of the month falls on a non-working day, the program should be able to detect the following day or so that the DB is not yet back up. Can this be Done?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  17. #17
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: DB Backup and Restore

    Here is part of it . .. Updated

    VB Code:
    1. Dim Firstofmonth As Date
    2.     Dim Lastofmonth As Date
    3.     Dim ThisDay As String
    4.     Dim YesterDay As String
    5.    
    6.     Firstofmonth = DateAdd("d", 1, Date - Day(Date))
    7.     Lastofmonth = DateAdd("m", 1, Date - Day(Date))
    8.     ThisDay = Format(Now, "dddd")
    9.     YesterDay = Format(Now - 1, "dddd")
    10.    
    11.     Debug.Print Firstofmonth
    12.     Debug.Print Lastofmonth
    13.     Debug.Print ThisDay
    14.     Debug.Print YesterDay
    15.    
    16.     If Date = Firstofmonth Then
    17.         Debug.Print "Today is first of Month"
    18.     End If
    19.     If ThisDay <> "Saturday" And ThisDay <> "Sunday" Then
    20.         Debug.Print "Today is a Weekday"
    21.     End If
    22.     If YesterDay = "Sunday" Then
    23.         Debug.Print "Today is a First Of Week"
    24.     End If
    Last edited by rory; May 14th, 2006 at 10:00 PM.

  18. #18

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    i just noticed. When do i call the code u posted in #15?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  19. #19
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: DB Backup and Restore

    Thats a button, but you could call it on exit ..?

    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2.  
    3.   Dim sFile As String
    4.   Dim Answer As Integer
    5.   sFile = Format(Now, "mm_dd_yyyy") & ".mdb"
    6.   If Dir$(App.Path & "\Archive\" & sFile) = vbNullString Then
    7.         Answer = MsgBox("Would you like to backup now", _
    8.         vbQuestion + vbYesNo, "My Program")
    9.         If Answer = vbYes Then
    10.             Call BackUpDatabase
    11.             'Cancel = -1
    12.         End If
    13.    End If
    14.  
    15. End Sub
    16.  
    17. Private Sub BackUpDatabase()
    18.  
    19. Const ProgName = "Export Database"
    20. Dim sFile As String
    21. Dim sFileName As String
    22.  
    23. On Error GoTo err_cancel:
    24.     sFile = Format(Now, "mm_dd_yyyy") & ".mdb"
    25.     If Dir$(App.Path & "\Archive\" & sFile) <> vbNullString Then
    26.         If (MsgBox("File Exists! Save it using New Filename?", vbInformation + vbYesNo, ProgName) = vbYes) Then
    27.             CommonDialog1.InitDir = App.Path & "\Archive\"
    28.             CommonDialog1.Filter = "Mdb Files (*.mdb)|*.mdb|All Files (*.*)|*.*"
    29.             CommonDialog1.FileName = sFile
    30.             CommonDialog1.DialogTitle = "Export Database"
    31.             CommonDialog1.ShowSave
    32.             sFileName = CommonDialog1.FileName
    33.             FileCopy App.Path & "\MasterDB.mdb", sFileName
    34.         Else
    35.             Exit Sub
    36.         End If
    37.     Else
    38.         If Dir$(App.Path & "\Archive\", vbDirectory) = vbNullString Then
    39.             Call MkDir(App.Path & "\Archive")
    40.         End If
    41.         sFileName = App.Path & "\Archive\" & sFile
    42.         FileCopy App.Path & "\MasterDB.mdb", sFileName
    43.     End If
    44.     If Dir$(sFileName) <> vbNullString Then
    45.         MsgBox "Export Completed", vbInformation, ProgName
    46.     Else
    47.        MsgBox "Export Error!", vbInformation, ProgName
    48.     End If
    49.     '// End
    50.         Exit Sub
    51. err_cancel:
    52.         Exit Sub
    53. End Sub
    Last edited by rory; May 14th, 2006 at 09:54 PM.

  20. #20

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    Im sorry rory its post #14 not #15. My mistake. I dont need to use the code in Post #15 on exit of a form. I already added did some modification to it to suite my needs. Anyway thanks for the additional info you posted.

    Going back to post #14 when do i call the Function CompactDatabase?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  21. #21
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: DB Backup and Restore

    Instead of using FileCopy you would use this.
    As it makes a compacted copy of the original.

    Ofcourse you want to make sure the Database Objects are closed first.

    VB Code:
    1. CompactDatabase App.Path & "\MasterDB.mdb", sFileName

    Or this would also delete the main file and copy the compacted file as the main file, after compacting.

    VB Code:
    1. CompactDatabase App.Path & "\MasterDB.mdb", sFileName
    2. Kill App.Path & "\MasterDB.mdb"
    3. FileCopy sFileName, App.Path & "\MasterDB.mdb"
    Last edited by rory; May 14th, 2006 at 10:19 PM.

  22. #22

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    Quote Originally Posted by rory
    Ofcourse you want to make sure the Database Objects are closed first.
    I tried putting this line to close the connection to DB but it wont work.
    VB Code:
    1. Oconn.Close
    Not DB back up at all. I have a module which i call to connect to my DB.
    I have also an MDIMain which I put a Treeview as my menu. On load of my MDIMain i have not called yet connection. Also when i click the Backup menu item the form will show where i put those codes. In this form i have not called yet the connection. So is there a need to close the DB Connection?

    Note: Later I will be putting log in form. In case, I will be opening the DB to check for the Correct login name and password.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  23. #23
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: DB Backup and Restore

    Right, if its not open there is nothing to close.

    Though you could always put this in your close connection sub to only close it if it is open.

    VB Code:
    1. If Not Oconn Is Nothing Then
    2.     If Oconn.State = adStateOpen Then Oconn.Close
    3. End If
    4. Set Oconn = Nothing

  24. #24

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    Quote Originally Posted by rory
    Right, if its not open there is nothing to close.

    Though you could always put this in your close connection sub to only close it if it is open.

    VB Code:
    1. If Not Oconn Is Nothing Then
    2.     If Oconn.State = adStateOpen Then Oconn.Close
    3. End If
    4. Set Oconn = Nothing
    this is what i have
    VB Code:
    1. If oConn.State = adStateOpen Then
    2.     oConn.Close
    3.     Set oConn = Nothing
    4.   End If
    I think they're the same?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DB Backup and Restore

    No, they are not the same as the first one is checking if the object is initialized and then its state where the second is only checking the state. If the object is not initialized then you will get a 91 error using the second.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  26. #26

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    so the two should be put in one sub? like
    VB Code:
    1. Public Sub closeConnection()
    2.  
    3.  If Not oConn Is Nothing Then
    4.    If oConn.State = adStateOpen Then oConn.Close
    5. End If
    6.  
    7. Set oConn = Nothing
    8.  
    9.   If oConn.State = adStateOpen Then
    10.     oConn.Close
    11.     Set oConn = Nothing
    12.   End If
    13. end Sub
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  27. #27
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DB Backup and Restore

    No, just this one.
    VB Code:
    1. Public Sub closeConnection()
    2.     If Not oConn Is Nothing Then
    3.         If oConn.State = adStateOpen Then oConn.Close
    4.     End If
    5. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  28. #28

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    ok done!

    I tried the code in Post #17. i just changed the highlighted part with this
    VB Code:
    1. If Date = ThisDay Then
    2.         'call the form where the backup button is located
    3.     End If
    I put it on MDIMain Load but its not working. I tried it too on Activate event. Nothing happened
    VB Code:
    1. If Date = [HL="#80FF80"]Firstofmonth [/HL]Then
    2.         Debug.Print "Today is first of Month"
    3.     End If
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  29. #29
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DB Backup and Restore

    Place a breeakpoint on MDIMain_Load and step through the code and tell me what is actually happening. Is Firstofmonth a valid date? If so whats the value?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  30. #30

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    I tried the breakpoint and step through it. it is not executing the higlighted part. it goes at once to end if
    VB Code:
    1. If Date = ThisDay Then
    2.        [HL="#FFFF80"]MsgBox "test"[/HL]
    3.     End If
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  31. #31
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: DB Backup and Restore

    Quote Originally Posted by Simply Me
    I tried the breakpoint and step through it. it is not executing the higlighted part. it goes at once to end if
    VB Code:
    1. If Date = ThisDay Then
    2.        [HL="#FFFF80"]MsgBox "test"[/HL]
    3.     End If

    ThisDay and Yesterday are Strings - Days of the Week.

    What are you trying to compare?

  32. #32

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    I tested it again and i found out that the firstday of the week in the program is Monday. It should be Sunday. I change this part If YesterDay = "Sunday" Then into If YesterDay = "Saturday" Then and nothing happened. Anyone?

    UPDATE: that part of code above is now ok.
    Last edited by Simply Me; May 23rd, 2006 at 09:59 AM.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  33. #33

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    My mistake. Im testing it with todays date so it will not work. Sorry!
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  34. #34
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DB Backup and Restore

    the Date function returns a date type value consisting of something like this, 05/14/2006, depending on your system settings.

    It will never equal "Sunday".

    You will want to use the WeekDay function instead of the actual Date.

    VB Code:
    1. If Weekday(Date) = vbSunday Then 'vbSunday = 1. vbMonday = 2, etc.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  35. #35

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    thanks again Robdog888.
    here's what's happening now. When i put the the code on MDIForm Load it displays first the msgbox and then when ok button is click it display the frmbackup form.

    How do i let the mdi form load first and then msgbox and then the frmbackup?

    VB Code:
    1. Private Sub MDIForm_Load()
    2.   StatusBar1.Panels(2).Text = "Time: " & Format(Time, "hh:mm:ss AM/PM")
    3.   StatusBar1.Panels(3).Text = "Date: " & Date
    4.  
    5.  'Display the Treeview
    6.   Call makeTreeView
    7.   Call PromptBackup
    8. End Sub
    9.  
    10.  
    11. Private Sub PromptBackup()
    12.  
    13.     Dim Firstofmonth As Date
    14.     Dim Lastofmonth As Date
    15.     Dim ThisDay As String
    16.     Dim YesterDay As String
    17.    
    18.     Firstofmonth = DateAdd("d", 1, Date - Day(Date))
    19.     Lastofmonth = DateAdd("m", 1, Date - Day(Date))
    20.     ThisDay = Format(Now, "dddd")
    21.     YesterDay = Format(Now - 1, "dddd")
    22.    
    23.     If Date = Firstofmonth Then
    24.         MsgBox "You need to Back up your Database"
    25.         frmBackup.Show vbModal
    26.         'Debug.Print "Today is first of Month"
    27.     End If
    28.     If ThisDay <> "Saturday" And ThisDay <> "Sunday" Then
    29.         Debug.Print "Today is a Weekday"
    30.     End If
    31.     If YesterDay = "Saturday" Then
    32.         Debug.Print "Today is a First Of Week"
    33.     End If
    34. End Sub
    Last edited by RobDog888; May 15th, 2006 at 03:24 AM.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  36. #36
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: DB Backup and Restore

    You can place the "Call PromptBackup" in the Activate event but then you will need to create a boolean flag variable to only let it call it once or when the user minimizes or switches apps, when they come back it will active again.

    Or you could use a timer that is set to enabled in the last line of the MDIForm_Load event and an interval of about 500 (30 seconds) to fire its Timer event procedure where you wil have the call to your "Call PromptBackup" call. Disable the timer as the first call as the next line of code should be the one calling the backup call..
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  37. #37

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    Quote Originally Posted by RobDog888
    You can place the "Call PromptBackup" in the Activate event but then you will need to create a boolean flag variable to only let it call it once or when the user minimizes or switches apps, when they come back it will active again.

    Or you could use a timer that is set to enabled in the last line of the MDIForm_Load event and an interval of about 500 (30 seconds) to fire its Timer event procedure where you wil have the call to your "Call PromptBackup" call. Disable the timer as the first call as the next line of code should be the one calling the backup call..
    Got it working!

    One last Problem. What if the first day of the month falls on Saturday or Sunday (no work, computers are off) is it still possible that vb would check if DB was backed-up or not?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  38. #38
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: DB Backup and Restore

    Quote Originally Posted by Simply Me
    Got it working!

    One last Problem. What if the first day of the month falls on Saturday or Sunday (no work, computers are off) is it still possible that vb would check if DB was backed-up or not?
    VB Code:
    1. Dim Firstofmonth As Date
    2.     Dim Lastofmonth As Date
    3.     Dim DateBefore As Date
    4.     Dim DateBeforeThat As Date
    5.     Dim ThisDay As String
    6.     Dim YesterDay As String
    7.    
    8.     Firstofmonth = DateAdd("d", 1, Date - Day(Date))
    9.     Lastofmonth = DateAdd("m", 1, Date - Day(Date))
    10.     ThisDay = Format(Now, "dddd")
    11.     YesterDay = Format(Now - 1, "dddd")
    12.     DateBefore = DateAdd("d", -1, Date)
    13.     DateBeforeThat = DateAdd("d", -2, Date)
    14.    
    15.     Debug.Print Firstofmonth
    16.     Debug.Print Lastofmonth
    17.     Debug.Print DateBefore
    18.     Debug.Print DateBeforeThat
    19.     Debug.Print ThisDay
    20.     Debug.Print YesterDay
    21.    
    22.     If Date = Firstofmonth Then
    23.         Debug.Print "Today is first of Month"
    24.     End If
    25.    
    26.     If ThisDay <> "Saturday" And ThisDay <> "Sunday" Then
    27.         Debug.Print "Today is a Weekday"
    28.     End If
    29.    
    30.     If YesterDay = "Sunday" Then
    31.         Debug.Print "Today is a First Of Week"
    32.     End If
    33.    
    34.     If DateBefore = Firstofmonth And ThisDay = "Monday" Then
    35.         Debug.Print "Day Before Was First of Month"
    36.     ElseIf DateBeforeThat = Firstofmonth And ThisDay = "Monday" Then
    37.         Debug.Print "Day Before That Was First of Month"
    38.     End If
    Last edited by rory; May 15th, 2006 at 04:15 AM.

  39. #39
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: DB Backup and Restore

    so your code would look like this ..

    VB Code:
    1. Private Sub PromptBackup()
    2.  
    3.     Dim Firstofmonth As Date
    4.     Dim DateBefore As Date
    5.     Dim DateBeforeThat As Date
    6.     Dim ThisDay As String
    7.    
    8.     Firstofmonth = DateAdd("d", 1, Date - Day(Date))
    9.     DateBefore = DateAdd("d", -1, Date)
    10.     DateBeforeThat = DateAdd("d", -2, Date)
    11.     ThisDay = Format(Now, "dddd")
    12.    
    13.     If Date = Firstofmonth Or _
    14.        DateBefore = Firstofmonth And ThisDay = "Monday" Or _
    15.        DateBeforeThat = Firstofmonth And ThisDay = "Monday" Then
    16.             MsgBox "You need to Back up your Database"
    17.             frmBackup.Show vbModal
    18.     End If
    19.  
    20. End Sub

  40. #40

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: DB Backup and Restore

    rory where would i put the code in post #38, in a module?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

Page 1 of 2 12 LastLast

Posting Permissions

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



Click Here to Expand Forum to Full Width