Rar/Zip file in Ms Access

  • Thread starter nghikt2
  • Ngày gửi
N

nghikt2

Guest
31/3/07
29
0
6
TP.HCM
Xin chào cả nhà ! em có vấn đề nho nhỏ mong được chỉ giúp. Em dùng code bên dưới để backup DB xong em muốn Rar/Zip file này lại luôn cho gọn nhưng không biết cách làm thế nào ?

Public Function BackupDB()
Dim strCompactFrom As String
Dim strCompactTo As String
Dim strPath As String

strPath = CurrentProject.path & "\"
strCompactFrom = "Database.mdb"
strCompactTo = Right("0" & Day(Now()), 2) & Right("0" & Month(Now()), 2) & Right("0" & Year(Now()), 2) & Hour(Now()) & Minute(Now()) & Second(Now()) & ".mdb"

If Dir(strPath & "Backup", vbDirectory) = "" Then
MkDir strPath & "Backup"
End If
If Dir(strPath & "Backup\" & strCompactTo, vbDirectory) <> "" Then
Kill strPath & "Backup\" & strCompactTo
End If

DBEngine.CompactDatabase strPath & strCompactFrom, strPath & "Backup\" & strCompactTo

'Name strPath & strCompactTo As strPath & strCompactFrom
MsgBox "Compact Database successful !"
End Function
 
Khóa học Quản trị dòng tiền
hai2hai

hai2hai

VNUNI Makes a difference
29/4/04
2,030
125
63
52
Hà nội
vnuni.net
Có rất nhiều cách từ Advance đến Simple. Cách đơn giản nhất là bạn nghiên cứu file RAR.exe (xem các command parameters) nằm trong thư mục WinRAR. Còn nếu bạn google thì có hàng nghìn chỗ để chọn bạn tham khảo món này.

Hope that helps!
 
N

nghikt2

Guest
31/3/07
29
0
6
TP.HCM
Em làm như sau nhưng bị lỗi bên dưới

Public Function BackupDB()
Dim strCompactFrom As String
Dim strCompactTo As String
Dim strPath As String

strPath = CurrentProject.path & "\"
strCompactFrom = "Database.mdb"
strCompactTo = Right("0" & Day(Now()), 2) & Right("0" & Month(Now()), 2) & Right("0" & Year(Now()), 2) & Hour(Now()) & Minute(Now()) & Second(Now()) & ".mdb"

If Dir(strPath & "Backup", vbDirectory) = "" Then
MkDir strPath & "Backup"
End If
If Dir(strPath & "Backup\" & strCompactTo, vbDirectory) <> "" Then
Kill strPath & "Backup\" & strCompactTo
End If

DBEngine.CompactDatabase strPath & strCompactFrom, strPath & "Backup\" & strCompactTo

'Name strPath & strCompactTo As strPath & strCompactFrom
'MsgBox "Compact Database successful !"

Dim WinRarPath As String
Dim DestRarFileName As String
Dim SourceRarFileName As String
WinRarPath = "C:\Program Files\WinRAR\WinRar.exe"
SourceRarFileName = strPath & "Backup\" & strCompactTo
DestRarFileName = strPath & "Backup\" & Left(strCompactTo, Len(strCompactTo) - 4) & ".rar"
a = Shell(WinRarPath & " a """ & DestRarFileName & """ """ & SourceRarFileName & """", vbNormalFocus)
Kill SourceRarFileName' bao loi cho nay
End Function
 
hai2hai

hai2hai

VNUNI Makes a difference
29/4/04
2,030
125
63
52
Hà nội
vnuni.net
Câu lệnh Shell đang thực hiện thì đã chạy tới câu lệnh xóa ở dưới. (SourceRarFileName đang được Open bởi Winrar.exe)

Hãy Google: ShellAndWait + Visual basic + SourceCode

Hoặc đại loại như vậy.

Nên dùng Rar.exe (DOS file) thay vì WinRar.exe (Win32 file). Và hãy copy duy nhất file rar.exe vào thư mục chương trình của bạn, chạy độc lập mà ko cần cài Winrar

ElseIf strSecondArg = "/b" Then
strRARFile = strAppPath & "Rar.exe"
Call ExtractRAR(strRARFile)
'// Backup DB (full backup)
'>> strBUFilePath = strAppPath & strDBName & "_" & Year(Date) & Format$(Month(Date), "00") & Format$(Day(Date), "00") & ".bak"
strBUFilePath = strAppPath & strDBName & ".bak"

If FileExists(strBUFilePath) Then Kill strBUFilePath
sSQL = "BACKUP DATABASE " & strDBName & vbCrLf & "TO DISK = '" & strBUFilePath & "'"

strYYYYMMDD = "_" & Year(Date) & Format$(Month(Date), "00") & Format$(Day(Date), "00")
strBUFilePathRAR = strAppPath & strDBName & strYYYYMMDD

strMsg = "Database " & strDBName & " Successfully backup."


.....

If strSecondArg = "/b" Then
'//Shell ("command.com /c Rar.exe" & " a -df -m5 -o+ " & strDBName & strYYYYMMDD & " " & strDBName & ".bak"), vbNormalNoFocus ' vbHide
ShellandWait ("command.com /c Rar.exe" & " a -df -m5 -o+ " & strDBName & strYYYYMMDD & " " & strDBName & ".bak")
'//MsgBox "Press <OK> button to continue if backup file was archived completely by winrar!", vbExclamation, "Please Wait...."
End If

....

Ngoài ra, tất cả các công ty có các chương trình nén như WinRAR, WinZip, 7zip, lzma,... đều có các thư viện và SDK (down trên website của họ) để chúng ta có thể sử dụng luôn các thuật toán nén mà ko cần dùng đến file ngoài như ở trên. Bên cạnh đó, ngay trong bản thân Windows cũng đã có sẵn API để thực hiện việc nén file (dạng zip). Tất cả những thứ đó bạn có thể Gúc gờ là ra hết.
 
Sửa lần cuối:

Xem nhiều