toancvp > 02-08-16, 11:16 AM
ongke0711 > 02-08-16, 03:50 PM
toancvp > 02-08-16, 06:55 PM
(02-08-16, 03:50 PM)ongke0711 Đã viết: - Code này chỉ chạy cho Access 2003, bạn đang sử dụng Access bao nhiêu?
- File của bạn có chia ra FE - BE không? Cái lệnh Compact nội tại của Accessc chỉ nén file db hiện hành, nếu bạn kết nối với BE thì nó không có tác dụng compact gì cả.
- Bạn tham khảo file demo Compact & Repair của bác MaiDinhDan đi. Compact từ xa...
Link: http://thuthuataccess.com/forum/thread-8731.html
paulsteigel > 02-08-16, 09:44 PM
(02-08-16, 06:55 PM)toancvp Đã viết: ...
Sub CompactDbs()
' Cai nay ap dung voi ung dung co database tach roi (BK - back-end).
' Neu khong co database tach roi thi don gian la that thuoc tinh Compact on Close
' Cai nay chi chay khi thoat khoi ung dung, dong het cac ket noi voi database nhe
On Error Resume Next
Dim tPath As String, tmpFile As String
' duong dan den file du lieu
tPath = CurrentProject.path & "\Data\"
' lap ra mot ten file tam thoi de nen
tmpFile = tPath & Format(Now(), "MMDDYYYYHHMMSS") & "_mp.mdb"
' thuc hien nen du lieu
Application.CompactRepair tPath & "Data.mdb", tmpFile, False
' dua file tam thanh file data chinh thuc
FileCopy tmpFile, tPath & "Data.mdb"
Kill tmpFile
End Sub
toancvp > 03-08-16, 11:14 AM
(02-08-16, 09:44 PM)paulsteigel Đã viết:(02-08-16, 06:55 PM)toancvp Đã viết: ...
Đây là cái mình hay dùng trong ứng dụng của mình!Mã:Sub CompactDbs()
' Cai nay ap dung voi ung dung co database tach roi (BK - back-end).
' Neu khong co database tach roi thi don gian la that thuoc tinh Compact on Close
' Cai nay chi chay khi thoat khoi ung dung, dong het cac ket noi voi database nhe
On Error Resume Next
Dim tPath As String, tmpFile As String
' duong dan den file du lieu
tPath = CurrentProject.path & "\Data\"
' lap ra mot ten file tam thoi de nen
tmpFile = tPath & Format(Now(), "MMDDYYYYHHMMSS") & "_mp.mdb"
' thuc hien nen du lieu
Application.CompactRepair tPath & "Data.mdb", tmpFile, False
' dua file tam thanh file data chinh thuc
FileCopy tmpFile, tPath & "Data.mdb"
Kill tmpFile
End Sub
Minh Tiên > 23-08-16, 05:25 PM
paulsteigel > 24-08-16, 12:19 AM
(23-08-16, 05:25 PM)Minh Tiên Đã viết: Chào bạn pausteigel !Cách làm của tôi thì hơi phức tạp một chút nhưng đảm bảo an toàn tương đối.
Dùng VBA để thay thế tính năng "Compact On Close" trong File thì viết như thế nào bạn ?
Thanks !
Sub DelayCompact()
Dim CmdPath As String
' Duong dan den tap tin thuc thi lenh
CmdPath = Application.CurrentProject.FullName & ".dbCompact.bat"
Dim s As String, sPath As String, AccAppPath As String
Dim AppFullPath As String, AppName As String
' Xac dinh duong dan cua file Access
AccAppPath = SysCmd(acSysCmdAccessDir) & "msaccess.exe"
sPath = CurrentProject.Path
AppFullPath = CurrentDb.Name
AppName = CurrentProject.Name
s = s & ":WAITLOOP" & vbCrLf
' Kiem tra xem Access co dang chay khong?
s = s & "tasklist /FI ""IMAGENAME eq msaccess.exe"" 2>NUL | find /I /N ""msaccess.exe"">NUL" & vbCrLf
s = s & "if ""%ERRORLEVEL%""==""0"" goto RUNNING" & vbCrLf
s = s & "goto NOTRUNNING" & vbCrLf
s = s & "rem===============" & vbCrLf
s = s & ":RUNNING" & vbCrLf
' Doan nay de doi cho Access thoat han
s = s & "ping localhost -w 2000" & vbCrLf
s = s & "goto WAITLOOP" & vbCrLf
s = s & "rem===============" & vbCrLf
s = s & ":NOTRUNNING" & vbCrLf
' Xoa file backup cu
s = s & "del """ & Replace(AppFullPath, AppName, "Backup_" & AppName) & """" & vbCrLf
' Tao 1 file Backup truoc khi compact
s = s & "copy """ & AppFullPath & """ """ & Replace(AppFullPath, AppName, "Backup_" & AppName) & """" & vbCrLf
' Gio thuc hien lenh compact
' Day la cu phap chuan
'"C:\Program Files\Microsoft Office\Office\MSAccess.exe" "C:\MyDatabase.mdb" /compact "C:\Backup.mdb"
s = s & """" & AccAppPath & """ """ & AppFullPath & """ /Compact" & vbCrLf
s = s & "rem===============" & vbCrLf
' Xoa file Bat sau khi thuc hien xong
s = s & "del %0" & vbCrLf
' Ghi ra file Bat
Dim intFile As Integer
intFile = FreeFile()
Open CmdPath For Output As #intFile
Print #intFile, s
Close #intFile
' Xoa bo dem so lan can compact
AppConfig("DB_COMPACT_COUNT") = 1
' Lap duong dan de goi lenh thuc thi file Bat
s = """" & CmdPath & """ """ & AppFullPath & """"
' goi lenh thuc thi file Bat
Shell s, vbHide
' Thoat Access nhe...
Application.Quit acQuitSaveAll
End Sub
:WAITLOOP
tasklist /FI "IMAGENAME eq msaccess.exe" 2>NUL | find /I /N "msaccess.exe">NUL
if "%ERRORLEVEL%"=="0" goto RUNNING
goto NOTRUNNING
rem===============
:RUNNING
ping localhost -w 2000
goto WAITLOOP
rem===============
:NOTRUNNING
del "E:\Users\Paulsteigel\Desktop\GPE\Access\AutoCompact\Backup_dbCompact.mdb"
copy "E:\Users\Paulsteigel\Desktop\GPE\Access\AutoCompact\dbCompact.mdb" "E:\Users\Paulsteigel\Desktop\GPE\Access\AutoCompact\Backup_dbCompact.mdb"
"C:\Program Files (x86)\Microsoft Office\OFFICE11\msaccess.exe" "E:\Users\Paulsteigel\Desktop\GPE\Access\AutoCompact\dbCompact.mdb" /compact
rem===============
del %0