Sevn
I trust ME!
- Local time
- Today, 10:38
- Joined
- Mar 13, 2008
- Messages
- 97
Hello All,
I have a standalone DB (2007) that I typically import, compact, ZIP, and email to several people every morning.
This is NOT a frontend/backend DB. Everything is contained within one file (InvoiceAudit...accdb).
I have searched far and wide for some code to automate this process, but keep coming up short on one piece or another. I have pasted the different code snippets that I have tried, and the results.
This one doesn't perform the compact, but does the email. When the DB is closed, it then performs the compact. This won't work, since the ZIP command is looking for something that isn't there yet (...\Database.mdb)
I tried to compact the DB first with this code, but it apparently isn't compatible with 2007.
I tried this one, but it tells me the database is open.
I tried this in conjunction with a function, but the function doesn't get recognized.
I am past frustrated with this, and need to know if what I'm trying to do is even possible. Can someone please guide me in the right direction, or let me know if this is even possible?
Any help will be greatly appreciated.
Thanks,
I have a standalone DB (2007) that I typically import, compact, ZIP, and email to several people every morning.
This is NOT a frontend/backend DB. Everything is contained within one file (InvoiceAudit...accdb).
I have searched far and wide for some code to automate this process, but keep coming up short on one piece or another. I have pasted the different code snippets that I have tried, and the results.
This one doesn't perform the compact, but does the email. When the DB is closed, it then performs the compact. This won't work, since the ZIP command is looking for something that isn't there yet (...\Database.mdb)
Code:
Public Function EmailInvoiceAudit()
Dim fs, currentfile, currentsize, filespec
Dim strProjectPath As String, strProjectName As String
strProjectPath = Application.CurrentProject.Path
strProjectName = Application.CurrentProject.Name
filespec = strProjectPath & "\" & strProjectName
Kill strProjectPath & "\Database.mdb"
Set fs = CreateObject("Scripting.FileSystemObject")
Set currentfile = fs.GetFile(filespec)
currentsize = CLng(currentfile.Size / 1000000)
If currentsize > 5 Then
Application.SetOption ("Auto Compact"), 1 'compact current file
Else
Application.SetOption ("Auto Compact"), 0 'do nothing
End If
Dim olapp As Object
Dim olns As Object
Dim olfolder As Object
Dim olitem As Object
Dim olattach As Object
Dim cp As XZip.Zip '** Archive files
Set cp = New XZip.Zip
cp.Pack "S:\Pricing\InvoiceAudit\Database.mdb", "S:\Pricing\InvoiceAudit\Exports\Temp\InvoiceAudit.zip" ', False, , 9
Set olapp = CreateObject("Outlook.Application")
Set olns = olapp.GetNamespace("MAPI")
Set olfolder = olns.getdefaultfolder(6)
Set olitem = olapp.createitem(0)
Set olattach = olitem.attachments
olitem.To = "youngje@compassminerals.com"
olitem.CC = ""
olitem.Subject = "Invoice Audit for " & Date - 1
olitem.body = "Hello," & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Please find enclosed the daily Invoice Audits" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Thanks," & Chr(13) & Chr(10) & "Jerrod"
olattach.Add "S:\Pricing\InvoiceAudit\Exports\Temp\InvoiceAudit.zip", 1
olitem.display
olitem.send
Set olitem = Nothing
Set rs = Nothing
Set Db = Nothing
Set olfolder = Nothing
Set olns = Nothing
Set olapp = Nothing
Set cp = Nothing
End Function
I tried to compact the DB first with this code, but it apparently isn't compatible with 2007.
Code:
Private Sub cmdEmail_Click()
CommandBars("Menu Bar"). _
Controls("Tools"). _
Controls("Database utilities"). _
Controls("Compact and repair database..."). _
accDoDefaultAction
End Sub
I tried this one, but it tells me the database is open.
Code:
Private Sub cmdEmail2_Click()
Dim SysDataPath As String
Dim SysMDBName As String
SysDataPath = Application.CurrentProject.Path
SysMDBName = Application.CurrentProject.Name
'Compact the database to a temp name
DBEngine.CompactDatabase SysDataPath & "\" & SysMDBName & ".accdb", SysDataPath & "\" & SysMDBName & "Old.accdb"
DoEvents
'Check to see if the file has been created
If Dir(SysDataPath & "\" & SysMDBName & "Old.accdb") <> "" Then
'Delete the original mdb
Kill SysDataPath & "\" & SysMDBName & ".accdb"
'Run the compact again back to its original name
DBEngine.CompactDatabase SysDataPath & "\" & SysMDBName & "old.accdb", SysDataPath & "\" & SysMDBName & ".accdb"
DoEvents
'Delete the old copy
Kill SysDataPath & "\" & SysMDBName & "Old.accdb"
DoEvents
Else
'Did the compact work?
If MsgBox("A problem occured creating the initial compacted file." & vbCrLf & vbCrLf & "click OK to continue without compacting the database or Cancel to exit the backup facility.", vbExclamation + vbOKCancel, "Problem occured") = vbCancel Then
Exit Sub
End If
End If
Dim cp As XZip.Zip
Set cp = New XZip.Zip
'Create the zip file
cp.Pack SysDataPath & "\" & SysMDBName & ".accdb", SysDataPath & "\" & Date - 1 & SysMDBName & ".zip"
Set cp = Nothing
MsgBox "Backup complete, click Ok to continue.", vbInformation + vbOKOnly, "Database Backup Routine"
End Sub
I tried this in conjunction with a function, but the function doesn't get recognized.
Code:
Private Sub Command9_Click()
CompactCurrentFile
Dim olapp As Object
Dim olns As Object
Dim olfolder As Object
Dim olitem As Object
Dim olattach As Object
Dim cp As XZip.Zip '** Archive files
Set cp = New XZip.Zip
cp.Pack "S:\Pricing\InvoiceAudit\Database.accdb", "S:\Pricing\InvoiceAudit\Exports\Temp\InvoiceAudit.zip" ', False, , 9
Set olapp = CreateObject("Outlook.Application")
Set olns = olapp.GetNamespace("MAPI")
Set olfolder = olns.getdefaultfolder(6)
Set olitem = olapp.createitem(0)
Set olattach = olitem.attachments
olitem.TO = "youngje@compassminerals.com"
olitem.CC = ""
olitem.Subject = "Invoice Audit for " & Date - 1
olitem.body = "Hello," & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Please find enclosed the daily Invoice Audits" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "Thanks," & Chr(13) & Chr(10) & "Jerrod"
olattach.Add "S:\Pricing\InvoiceAudit\Exports\Temp\InvoiceAudit.zip", 1
olitem.display
olitem.send
Set olitem = Nothing
Set rs = Nothing
Set Db = Nothing
Set olfolder = Nothing
Set olns = Nothing
Set olapp = Nothing
Set cp = Nothing
End Sub
I am past frustrated with this, and need to know if what I'm trying to do is even possible. Can someone please guide me in the right direction, or let me know if this is even possible?
Any help will be greatly appreciated.
Thanks,