Private Sub yedekle_Click()
Dim dosyaYolu As String
Dim masaustuYolu As String
Dim klasorYolu As String
Dim dosyaAdi As String
Dim tamDosyaYolu As String
Dim sayac As Integer
Dim tarihFormat As String
code
' Masaüstü yolunu al
masaustuYolu = Environ("USERPROFILE") & "\Desktop\Access Veritabanı\"
' Klasör yoksa oluştur
If Dir(masaustuYolu, vbDirectory) = "" Then
MkDir masaustuYolu
End If
' Yedekleme için tarih formatı
tarihFormat = Format(Date, "yyyy.mm.d")
' Access dosyasının bulunduğu yolu ve adını al
dosyaYolu = CurrentDb.Name
dosyaAdi = Dir(dosyaYolu)
' İlk dosya adı (örn. "2024.11.2.accdb")
tamDosyaYolu = masaustuYolu & tarihFormat & ".accdb"
sayac = 1
' Aynı isimde dosya varsa, sayıyı artırarak yeni bir dosya adı oluştur
While Dir(tamDosyaYolu) <> ""
tamDosyaYolu = masaustuYolu & tarihFormat & "." & sayac & ".accdb"
sayac = sayac + 1
Wend
' Hedef dosya yolunu kontrol et (hata ayıklama için)
MsgBox "Kaynak Dosya Yolu: " & dosyaYolu & vbCrLf & "Hedef Dosya Yolu: " & tamDosyaYolu
' Dosyayı kopyala
Application.FollowHyperlink dosyaYolu
FileCopy dosyaYolu, tamDosyaYolu
MsgBox "Yedekleme başarılı. Yedekleme dosyası: " & tamDosyaYolu, vbInformation
End Sub
FileCopy dosyaYolu, tamDosyaYolu burada hata verdi dosya oluşmadı
Assistant
The error you're encountering occurs because you're trying to use the FileCopy function to copy the currently open Access database file (CurrentDb.Name). Microsoft Access doesn't allow copying the database file that's currently open using FileCopy. This results in an error, and the backup file isn't created.
Solution:
To create a backup of the currently open database, you should use the Application.CompactRepair method instead of FileCopy. The CompactRepair method compacts and repairs the database, saving it to a new file. This method can operate on the open database file without causing errors.
Also, the line Application.FollowHyperlink dosyaYolu is unnecessary and may cause issues because it tries to open the current database file, which is already open. You can safely remove this line.
Here's the corrected and updated code:
vb
Private Sub yedekle_Click()
Dim dosyaYolu As String
Dim masaustuYolu As String
Dim klasorYolu As String
Dim dosyaAdi As String
Dim tamDosyaYolu As String
Dim sayac As Integer
Dim tarihFormat As String
' Masaüstü yolunu al
masaustuYolu = Environ("USERPROFILE") & "\Desktop\Access Veritabanı\"
' Klasör yoksa oluştur
If Dir(masaustuYolu, vbDirectory) = "" Then
MkDir masaustuYolu
End If
' Yedekleme için tarih formatı
tarihFormat = Format(Date, "yyyy.mm.d")
' Access dosyasının bulunduğu yolu ve adını al
dosyaYolu = CurrentDb.Name
dosyaAdi = Dir(dosyaYolu)
' İlk dosya adı (örn. "2024.11.2.accdb")
tamDosyaYolu = masaustuYolu & tarihFormat & ".accdb"
sayac = 1
' Aynı isimde dosya varsa, sayıyı artırarak yeni bir dosya adı oluştur
While Dir(tamDosyaYolu) <> ""
tamDosyaYolu = masaustuYolu & tarihFormat & "." & sayac & ".accdb"
sayac = sayac + 1
Wend
' Hedef dosya yolunu kontrol et (hata ayıklama için)
MsgBox "Kaynak Dosya Yolu: " & dosyaYolu & vbCrLf & "Hedef Dosya Yolu: " & tamDosyaYolu
' Dosyayı yedekle
Application.CompactRepair dosyaYolu, tamDosyaYolu, False
MsgBox "Yedekleme başarılı. Yedekleme dosyası: " & tamDosyaYolu, vbInformation
End Sub
Explanation:
Removed Application.FollowHyperlink dosyaYolu: This line attempts to open the current database file, which is unnecessary because it's already open. Removing it prevents potential conflicts.
Replaced FileCopy with Application.CompactRepair: The CompactRepair method creates a compacted copy of the database, which serves as a backup. It operates correctly even when the database is open.
tamDosyaYolu is the path where you want to save the backup.
False indicates that you don't want to create a log file.
Additional Notes:
Permissions: Ensure that the user running this code has sufficient permissions to read the source database and write to the destination folder.
Error Handling: You may want to add error handling to manage situations where the backup might fail due to other reasons (e.g., insufficient disk space).
Access Version Compatibility: The Application.CompactRepair method is available in Access 2000 and later versions.
Conclusion:
By using the CompactRepair method, you can successfully create a backup of your open database without encountering errors. This method is designed for such operations and is the recommended approach for backing up an open Access database programmatically.