|
-
May 14th, 2006, 02:28 AM
#1
Thread Starter
PowerPoster
[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.
-
May 14th, 2006, 02:58 AM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 14th, 2006, 03:36 AM
#3
Thread Starter
PowerPoster
Re: DB Backup and Restore
 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?
-
May 14th, 2006, 03:50 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 14th, 2006, 04:11 AM
#5
Thread Starter
PowerPoster
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:
Option Explicit
Private Sub Form_Load()
'check for archive folder and create one if it doesn't exist
If Dir(App.Path & "\Archive", vbDirectory) = "" Then
MkDir App.Path & "\Archive"
End If
'check for error folder and create one if it doesn't exist
If Dir(App.Path & "\Error", vbDirectory) = "" Then
MkDir App.Path & "\Error"
End If
End Sub
Private Sub Command1_Click()
ArchiveDB App.Path & "\MasterDB.mdb", _
App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb"
End Sub
Public Function ArchiveDB(sSource As String, sDestination As String) As Boolean
'===============================================================================
On Error GoTo ErrHandler
FileCopy sSource, sDestination
Exit Function
ErrHandler:
'------------
Open App.Path & "\Error\erros.txt" For Append As #1
Print #1, Now() & vbTab & Err.Number & ": " & Err.Description
Close #1
Exit Function
End Function
Last edited by RobDog888; May 14th, 2006 at 04:13 AM.
Reason: Fixed [vbcode] tags
-
May 14th, 2006, 04:15 AM
#6
Re: DB Backup and Restore
Before you call ArchiveDB, close your connection(s) like so.
VB Code:
oCnn.[color=black]Close[/color]
ArchiveDB App.Path & "\MasterDB.mdb", App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb"
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 14th, 2006, 04:28 AM
#7
Thread Starter
PowerPoster
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).
-
May 14th, 2006, 04:52 AM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 14th, 2006, 05:11 AM
#9
Fanatic Member
Re: DB Backup and Restore
If u want to check whether a file exists or not then this is the code:
VB Code:
Dim afile As String
Private Sub Command1_Click()
afile = Dir$("c:\My Documents\smashing.exe") <> ""
If afile Then
MsgBox "File exist"
Else
MsgBox "File does not exist"
End If
End Sub
Dont rely only on your luck. Work hard until You get success.
vb Code:
Private sub Time_ispassing
While Me.Notgetsuccess
trygain=tryagain+1
Me.workhard
wend
end sub
-
May 14th, 2006, 05:34 AM
#10
Thread Starter
PowerPoster
Re: DB Backup and Restore
 Originally Posted by RobDog888
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?
-
May 14th, 2006, 06:05 AM
#11
Thread Starter
PowerPoster
Re: DB Backup and Restore
I added the code below to let the user save the DB to a new file.
VB Code:
'Check if File Exists or Not
If FileExists(App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb") = 1 Then
If (MsgBox("File Exists! Save it using New Filename?", vbInformation + vbYesNo) = vbYes) Then
With CommonDialog1
'Set Filters
.Filter = "Access Database Files (*.mdb)|*.mdb|"
' Display the Open dialog box
.ShowSave
'App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb", _
'App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb", 0
End With
Else
Exit Sub
End If
Else
'oConn.Close 'Needed to protect the DB from possible corruption
ArchiveDB App.Path & "\MasterDB.mdb", _
App.Path & "\Archive\" & Format(Now, "mm_dd_yyyy") & ".mdb"
'oConn.Open
'MsgBox "File Not Found!"
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\....
-
May 14th, 2006, 08:53 AM
#12
Thread Starter
PowerPoster
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?
-
May 14th, 2006, 09:59 AM
#13
Thread Starter
PowerPoster
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:
Open cdbDialog.FileName For Output As #1
Print #1,
Close #1
Any suggestions or code snippet?
-
May 14th, 2006, 10:19 AM
#14
PowerPoster
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
-
May 14th, 2006, 10:28 AM
#15
PowerPoster
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.
-
May 14th, 2006, 09:05 PM
#16
Thread Starter
PowerPoster
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?
-
May 14th, 2006, 09:23 PM
#17
PowerPoster
Re: DB Backup and Restore
Here is part of it . .. Updated
VB Code:
Dim Firstofmonth As Date
Dim Lastofmonth As Date
Dim ThisDay As String
Dim YesterDay As String
Firstofmonth = DateAdd("d", 1, Date - Day(Date))
Lastofmonth = DateAdd("m", 1, Date - Day(Date))
ThisDay = Format(Now, "dddd")
YesterDay = Format(Now - 1, "dddd")
Debug.Print Firstofmonth
Debug.Print Lastofmonth
Debug.Print ThisDay
Debug.Print YesterDay
If Date = Firstofmonth Then
Debug.Print "Today is first of Month"
End If
If ThisDay <> "Saturday" And ThisDay <> "Sunday" Then
Debug.Print "Today is a Weekday"
End If
If YesterDay = "Sunday" Then
Debug.Print "Today is a First Of Week"
End If
Last edited by rory; May 14th, 2006 at 10:00 PM.
-
May 14th, 2006, 09:33 PM
#18
Thread Starter
PowerPoster
Re: DB Backup and Restore
i just noticed. When do i call the code u posted in #15?
-
May 14th, 2006, 09:48 PM
#19
PowerPoster
Re: DB Backup and Restore
Thats a button, but you could call it on exit ..?
VB Code:
Private Sub Form_Unload(Cancel As Integer)
Dim sFile As String
Dim Answer As Integer
sFile = Format(Now, "mm_dd_yyyy") & ".mdb"
If Dir$(App.Path & "\Archive\" & sFile) = vbNullString Then
Answer = MsgBox("Would you like to backup now", _
vbQuestion + vbYesNo, "My Program")
If Answer = vbYes Then
Call BackUpDatabase
'Cancel = -1
End If
End If
End Sub
Private Sub BackUpDatabase()
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
'// End
Exit Sub
err_cancel:
Exit Sub
End Sub
Last edited by rory; May 14th, 2006 at 09:54 PM.
-
May 14th, 2006, 09:58 PM
#20
Thread Starter
PowerPoster
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?
-
May 14th, 2006, 10:09 PM
#21
PowerPoster
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:
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:
CompactDatabase App.Path & "\MasterDB.mdb", sFileName
Kill App.Path & "\MasterDB.mdb"
FileCopy sFileName, App.Path & "\MasterDB.mdb"
Last edited by rory; May 14th, 2006 at 10:19 PM.
-
May 14th, 2006, 10:50 PM
#22
Thread Starter
PowerPoster
Re: DB Backup and Restore
 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.
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.
-
May 14th, 2006, 11:02 PM
#23
PowerPoster
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:
If Not Oconn Is Nothing Then
If Oconn.State = adStateOpen Then Oconn.Close
End If
Set Oconn = Nothing
-
May 14th, 2006, 11:17 PM
#24
Thread Starter
PowerPoster
Re: DB Backup and Restore
 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:
If Not Oconn Is Nothing Then
If Oconn.State = adStateOpen Then Oconn.Close
End If
Set Oconn = Nothing
this is what i have
VB Code:
If oConn.State = adStateOpen Then
oConn.Close
Set oConn = Nothing
End If
I think they're the same?
-
May 14th, 2006, 11:51 PM
#25
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 15th, 2006, 12:23 AM
#26
Thread Starter
PowerPoster
Re: DB Backup and Restore
so the two should be put in one sub? like
VB Code:
Public Sub closeConnection()
If Not oConn Is Nothing Then
If oConn.State = adStateOpen Then oConn.Close
End If
Set oConn = Nothing
If oConn.State = adStateOpen Then
oConn.Close
Set oConn = Nothing
End If
end Sub
-
May 15th, 2006, 12:26 AM
#27
Re: DB Backup and Restore
No, just this one.
VB Code:
Public Sub closeConnection()
If Not oConn Is Nothing Then
If oConn.State = adStateOpen Then oConn.Close
End If
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 15th, 2006, 01:19 AM
#28
Thread Starter
PowerPoster
Re: DB Backup and Restore
ok done!
I tried the code in Post #17. i just changed the highlighted part with this
VB Code:
If Date = ThisDay Then
'call the form where the backup button is located
End If
I put it on MDIMain Load but its not working. I tried it too on Activate event. Nothing happened
VB Code:
If Date = [HL="#80FF80"]Firstofmonth [/HL]Then
Debug.Print "Today is first of Month"
End If
-
May 15th, 2006, 01:29 AM
#29
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 15th, 2006, 01:49 AM
#30
Thread Starter
PowerPoster
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:
If Date = ThisDay Then
[HL="#FFFF80"]MsgBox "test"[/HL]
End If
-
May 15th, 2006, 02:11 AM
#31
PowerPoster
Re: DB Backup and Restore
 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:
If Date = ThisDay Then
[HL="#FFFF80"]MsgBox "test"[/HL]
End If
ThisDay and Yesterday are Strings - Days of the Week.
What are you trying to compare?
-
May 15th, 2006, 02:12 AM
#32
Thread Starter
PowerPoster
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.
-
May 15th, 2006, 02:48 AM
#33
Thread Starter
PowerPoster
Re: DB Backup and Restore
My mistake. Im testing it with todays date so it will not work. Sorry!
-
May 15th, 2006, 03:00 AM
#34
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:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 15th, 2006, 03:20 AM
#35
Thread Starter
PowerPoster
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:
Private Sub MDIForm_Load()
StatusBar1.Panels(2).Text = "Time: " & Format(Time, "hh:mm:ss AM/PM")
StatusBar1.Panels(3).Text = "Date: " & Date
'Display the Treeview
Call makeTreeView
Call PromptBackup
End Sub
Private Sub PromptBackup()
Dim Firstofmonth As Date
Dim Lastofmonth As Date
Dim ThisDay As String
Dim YesterDay As String
Firstofmonth = DateAdd("d", 1, Date - Day(Date))
Lastofmonth = DateAdd("m", 1, Date - Day(Date))
ThisDay = Format(Now, "dddd")
YesterDay = Format(Now - 1, "dddd")
If Date = Firstofmonth Then
MsgBox "You need to Back up your Database"
frmBackup.Show vbModal
'Debug.Print "Today is first of Month"
End If
If ThisDay <> "Saturday" And ThisDay <> "Sunday" Then
Debug.Print "Today is a Weekday"
End If
If YesterDay = "Saturday" Then
Debug.Print "Today is a First Of Week"
End If
End Sub
Last edited by RobDog888; May 15th, 2006 at 03:24 AM.
-
May 15th, 2006, 03:27 AM
#36
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 15th, 2006, 03:52 AM
#37
Thread Starter
PowerPoster
Re: DB Backup and Restore
 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?
-
May 15th, 2006, 04:10 AM
#38
PowerPoster
Re: DB Backup and Restore
 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:
Dim Firstofmonth As Date
Dim Lastofmonth As Date
Dim DateBefore As Date
Dim DateBeforeThat As Date
Dim ThisDay As String
Dim YesterDay As String
Firstofmonth = DateAdd("d", 1, Date - Day(Date))
Lastofmonth = DateAdd("m", 1, Date - Day(Date))
ThisDay = Format(Now, "dddd")
YesterDay = Format(Now - 1, "dddd")
DateBefore = DateAdd("d", -1, Date)
DateBeforeThat = DateAdd("d", -2, Date)
Debug.Print Firstofmonth
Debug.Print Lastofmonth
Debug.Print DateBefore
Debug.Print DateBeforeThat
Debug.Print ThisDay
Debug.Print YesterDay
If Date = Firstofmonth Then
Debug.Print "Today is first of Month"
End If
If ThisDay <> "Saturday" And ThisDay <> "Sunday" Then
Debug.Print "Today is a Weekday"
End If
If YesterDay = "Sunday" Then
Debug.Print "Today is a First Of Week"
End If
If DateBefore = Firstofmonth And ThisDay = "Monday" Then
Debug.Print "Day Before Was First of Month"
ElseIf DateBeforeThat = Firstofmonth And ThisDay = "Monday" Then
Debug.Print "Day Before That Was First of Month"
End If
Last edited by rory; May 15th, 2006 at 04:15 AM.
-
May 15th, 2006, 04:26 AM
#39
PowerPoster
Re: DB Backup and Restore
so your code would look like this ..
VB Code:
Private Sub PromptBackup()
Dim Firstofmonth As Date
Dim DateBefore As Date
Dim DateBeforeThat As Date
Dim ThisDay As String
Firstofmonth = DateAdd("d", 1, Date - Day(Date))
DateBefore = DateAdd("d", -1, Date)
DateBeforeThat = DateAdd("d", -2, Date)
ThisDay = Format(Now, "dddd")
If Date = Firstofmonth Or _
DateBefore = Firstofmonth And ThisDay = "Monday" Or _
DateBeforeThat = Firstofmonth And ThisDay = "Monday" Then
MsgBox "You need to Back up your Database"
frmBackup.Show vbModal
End If
End Sub
-
May 15th, 2006, 04:41 AM
#40
Thread Starter
PowerPoster
Re: DB Backup and Restore
rory where would i put the code in post #38, in a module?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|