26. januar 2005 - 19:52Der er
33 kommentarer og 1 løsning
Automatisk backup-funktion
Vi har en database, der bruges af ca. 5 personer samtidigt. Jeg vil gerne indbygge en funktion, der gør at databasen hver dag kl. 12 laver en kopi af hele filen og gemmer på c: med navnet "Backup-" og så det normale navn. Backup'en fra dagen før skal bare overskrives. Det er kun maskiner, der har databsen åben på det pågældende tidspunkt, der skal lave backup. Kan det lade sig gøre? Jeg ved godt at det i princippet betyder, at der laves 5 backup'er (hvis alle 5 personer har den åben samtidig), men det bør vel ikke være et problem? Vi har både .mdb og .mde-fil. TAK
My database says: Database kan ikke finde funktionen.
This is what I did: 1. copy the whole code from your link (sugestion no. 2). 2. in my database, in module window, I pressed Ny. Pasted all the copied code and closed (saved as "module1"). 3. In a form I made a button. In the "ved klik" I wrote fMakeBackup().
and it may be an idea to replace the function fDBExclusive with this
Function fDBExclusive() As Integer Dim hFile As Integer hFile = FreeFile Set db = CurrentDb On Error Resume Next Open CurrentDb.Name For Binary Access Read Write Shared As hFile Select Case Err Case 0 fDBExclusive = False Case 70 fDBExclusive = True Case Else fDBExclusive = Err End Select Close hFile On Error GoTo 0 End Function
Åbn formularen i designvisning > Højreklik på din knap > Egenskaber > Fanen hændelser > Sæt markøren på linien vedKlik > Tryk på de 3 små i højre side.
Du kommer nu ind i VBA-editoren hvor du har skrevet din kode. Sle linien:
Module1.fMakeBackup
Nu skriver du igen:
Module1.
Hvad sker der på skærmen efter dit . (punktumnotation)?
Function fDBExclusive() As Integer Dim hFile As Integer hFile = FreeFile On Error Resume Next Open CurrentDb.Name For Binary Access Read Write Shared As hFile Select Case Err Case 0 fDBExclusive = False Case 70 fDBExclusive = True Case Else fDBExclusive = Err End Select Close hFile On Error GoTo 0 End Function
Hej, jeg mener ikke det betyder så meget hvad modulet hedder, måske hvis det er et klassemodul ellers ikke? har bestemt ikke kloget mig på nogen måde :o), skal der ikke stå noget med Call fMakeBackup()
If I place a button on the form this code works. If it doesnt then you need toi make sure that module1 is saved and thet there are no errors in the code. Make sure that you havent copied other texts than the necessary code.! You should NOT have RED text. You can also try compiling the program (Debug+Compile ...) menu
Terry; jeg har indarbejdet dine ændringer. Det virker. TAK. mugs + aandersen; det virker nu. TAK.
Sidste ønske; hvis navnet på backup-filen skal hedde tidspunktet (YYMMDD-HH:MM) efterfulgt af "-" og så databasenavnet, hvad skal jeg så ændre i koden?
aandersen > Jeg bruger normalt både modulnavnet OG funktionsnavnet. Så er jeg sikker på med det samme at opfange en stavefejl. Og jeg synes det er nemmere at fejlfinde ændre senere hen.
alternativt - hvis jeg bruger den første model - skal den overskrive den kopi, der er lavet sidst. Nu tilføjer den "Kopi (2) af " i navnet, når jeg laver backup anden gang. Hvordan får jeg den til at overskrive? TAK
but this also include the path. So you are going to have to split it up to add the extra information. But ther is already a date time the fiel was created!
'********** Code Start ************* ' This code was originally written by Dev Ashish. ' It is not to be altered or distributed, ' except as part of an application. ' You are free to use it in any application, ' provided the copyright notice is left unchanged. ' ' Code Courtesy of ' Dev Ashish ' Private Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAnyOperationsAborted As Boolean hNameMappings As Long lpszProgressTitle As String End Type
Private Const FO_MOVE As Long = &H1 Private Const FO_COPY As Long = &H2 Private Const FO_DELETE As Long = &H3 Private Const FO_RENAME As Long = &H4
Private Const FOF_MULTIDESTFILES As Long = &H1 Private Const FOF_CONFIRMMOUSE As Long = &H2 Private Const FOF_SILENT As Long = &H4 Private Const FOF_RENAMEONCOLLISION As Long = &H8 Private Const FOF_NOCONFIRMATION As Long = &H10 Private Const FOF_WANTMAPPINGHANDLE As Long = &H20 Private Const FOF_CREATEPROGRESSDLG As Long = &H0 Private Const FOF_ALLOWUNDO As Long = &H40 Private Const FOF_FILESONLY As Long = &H80 Private Const FOF_SIMPLEPROGRESS As Long = &H100 Private Const FOF_NOCONFIRMMKDIR As Long = &H200
Private Declare Function apiSHFileOperation Lib "Shell32.dll" _ Alias "SHFileOperationA" _ (lpFileOp As SHFILEOPSTRUCT) _ As Long
Function fMakeBackup() As Boolean Dim strMsg As String Dim tshFileOp As SHFILEOPSTRUCT Dim lngRet As Long Dim strSaveFile As String Dim lngFlags As Long Const cERR_USER_CANCEL = vbObjectError + 1 Const cERR_DB_EXCLUSIVE = vbObjectError + 2 On Local Error GoTo fMakeBackup_Err
If fDBExclusive = True Then Err.Raise cERR_DB_EXCLUSIVE
strMsg = "Are you sure that you want to make a copy of the database?" If MsgBox(strMsg, vbQuestion + vbYesNo, "Please confirm") = vbNo Then _ Err.Raise cERR_USER_CANCEL
lngFlags = FOF_SIMPLEPROGRESS Or _ FOF_FILESONLY Or _ FOF_RENAMEONCOLLISION strSaveFile = "c:\YourBackup.mdb" 'CurrentDb.Name Kill strSaveFile With tshFileOp .wFunc = FO_COPY .hwnd = hWndAccessApp .pFrom = CurrentDb.Name & vbNullChar .pTo = strSaveFile & vbNullChar .fFlags = lngFlags End With lngRet = apiSHFileOperation(tshFileOp) fMakeBackup = (lngRet = 0)
fMakeBackup_End: Exit Function fMakeBackup_Err: fMakeBackup = False Select Case Err.Number Case cERR_USER_CANCEL: 'do nothing Case cERR_DB_EXCLUSIVE: MsgBox "The current database " & vbCrLf & CurrentDb.Name & vbCrLf & _ vbCrLf & "is opened exclusively. Please reopen in shared mode" & _ " and try again.", vbCritical + vbOKOnly, "Database copy failed" Case Else: strMsg = "Error Information..." & vbCrLf & vbCrLf strMsg = strMsg & "Function: fMakeBackup" & vbCrLf strMsg = strMsg & "Description: " & Err.Description & vbCrLf strMsg = strMsg & "Error #: " & Format$(Err.Number) & vbCrLf MsgBox strMsg, vbInformation, "fMakeBackup" End Select Resume fMakeBackup_End End Function
Private Function fCurrentDBDir() As String 'code courtesy of 'Terry Kreft Dim strDBPath As String Dim strDBFile As String strDBPath = CurrentDb.Name strDBFile = Dir(strDBPath) fCurrentDBDir = Left(strDBPath, InStr(strDBPath, strDBFile) - 1) End Function
Function fDBExclusive() As Integer Dim hFile As Integer hFile = FreeFile 'Set db = CurrentDb On Error Resume Next Open CurrentDb.Name For Binary Access Read Write Shared As hFile Select Case Err Case 0 fDBExclusive = False Case 70 fDBExclusive = True Case Else fDBExclusive = Err End Select Close hFile On Error GoTo 0 End Function '************* Code End ***************
-its doing great. However can I change the code, so that it doesnt show the message "Are you sure that you want to make a copy of the database?", and just perform the backup? I transfer points, when it is done, tonight. THANKS for your help.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.