13. september 2005 - 14:32Der er
26 kommentarer og 1 løsning
Copy db
I have a db devided into frontend and backend. From the frontend I want to copy the backend db and be able to make a choice where to save the copy. How do I do this? Is there, by commandline, possible to take the copy, from a disc or somewere else on the computer and replace the existing backend db?
This is to keep things easy, to make a complete solutionwould take some time. The problem is that the backend can be anywhere, so you need code to get the location of the backend. In this example I have just coded it directly into the code. You can see which lines are of importance with this beside '<<<<<<<<<<<<<<<<<<<<<<<<<<
In this example it uses the same name for the source (FROM )and the destination (TO) which just makes a copy as you will see.
FIRST you need to alter the first line where '<<<<<<<<< is beside to contain the path/filename of your backend. If you want to copy the database to another folder/file then you need to alter the line starting with .pTo
Now place a button on a form and in the on Click event you just write
GetBackend
Try it!
'********** 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
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 db As Database Dim hFile As Integer hFile = FreeFile Set db = CurrentDb On Error Resume Next Open db.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 ***************
you can always use the File Open/Save Dialog to choose the location of your baceknd/backup files.
There is also an example on ThomasJepsen's homepage with code to re-link your backend database. This code has a function which can be used to find the backend path/filename. The function is in the module named Attachments and is named GetBackend.
THis code gives you the Path/Filename of the First backend table. If you have more than one then it doesnt work.
Hi Terry! From what I understand, the code has to be stored as an module. Is that right and what name should I give it? If I just write GetBackend as an instruction on a click event, I get an error message saying "Kompile error: An variable or procedure is expected. Not a module." What am I doing wrong?
Now You'r talking. It works greate. Now, insted of saving the copy where the original is, how do I call a dialog form where I can direct where to save the copy? Then I want to be able to fetch a copy and put it back and replace the existing one. The path to the original will always be the same. In total I feel this will be worth far more than the original 30 points.
On a second thought, it seems impossible to owerwrite the existing backend since the frontend is running, or can this be done if a form is open that is'nt using data from the backend?
I havent looked too closely at it but the example (Thomas's) which re-links the backend, could maybe be used to replace the backend with another file. An idea would to be to place the new backend at another path/filename using the File OPEN dialog, which I am sure is included in the example, and then re-link to the new backend. Re-linking to the new backend will be allowed, but if you just try overwriting the original backend with the new the this could very well give problems.
I'm rather busy right now, but I'll take a look once in a while and see if I can help.
Hi Terry! I have solved everything exept for directing a copy to a folder by choice. In the same way as the File Open dialog opens when I want to import data, I want a dialog to open where I can direct a copy taken of the backend. I have tryed Thomas example "FindSpecialPath" but I do'nt fully understand how to use it. He writes that a button with the instruction "?GetSpecialPath" will give the path to Desktop, My documents etc etc, but how. Why is it a questionmark in the beginning? The debugger automaticly changes i to "Print", but otherwise exepts it.
the Save Dialog as far as I know is just the same as the find (file open) dialog. It just allows you to browse to a folder and choose a file name. Once you have the path/file name its your program wich takes over to do the actual saving/opening.
If you can send me what you have then maybe I can alter it so that it uses the Save/open dialog.
eskperten@NOSPAMsanthell.dk remove NOSPAM
I'm rather busy, also over the weekend but I should be able to find a bit of time.
Did'nt You write the mailaddress wrong. Should'nt it be eksperten@..., instead of eskperten@..? I have recent th db again.
Synes godt om
Ny brugerNybegynder
Din løsning...
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.