Forum Discussion
Problem Creating a usable zip file using VBA. Does anyone have an alternative?
Let’s say you want to make a backup of your database and email a copy to somewhere – as a backup on the same machine isn’t much use.
If you don't want to email it don't add that section of code to your button.
Here’s a simple method I used to use. I don’t anymore as we now have a different system in place, but this will start you off in the right direction.
I use MS Outlook, so if you don’t you’ll need to alter the code to use whatever mailing system you do use.
First
Create a form with a button – call it cmd_backup
Next create a macro – call it mcr_Backup
Next create a module – call it mod_Backup
In the module paste this and save it (I didn’t write this, I’ve changed a few lines from the original which I can't remember where I got it from)
Option Compare Database
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Function SendTheBackUp () As Long
Dim ShellApplication As Object
Dim CurrentProjectFile As String
Dim ZipPath As String
Dim ZipName As String
Dim ZipFile As String
Dim FileNumber As Integer
CurrentProjectFile = CurrentProject.Path & "\" & CurrentProject.Name
ZipPath = CurrentProject.Path & "\_My_backup_created_on_" & Format(Date, "mm-dd-yyyy") & "_at_" & Format(Time, "hh-mm")
ZipName = ".zip"
ZipFile = ZipPath & ZipName
If Dir(ZipPath, vbDirectory) = "" Then
MkDir ZipPath
End If
FileNumber = FreeFile
Open ZipFile For Output As #FileNumber
Print #FileNumber, Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, vbNullChar)
Close #FileNumber
Set ShellApplication = CreateObject("Shell.Application")
With ShellApplication
Debug.Print Timer, "zipping started ..."
.NameSpace(CVar(ZipFile)).CopyHere CVar(CurrentProjectFile)
On Error Resume Next
Do Until .NameSpace(CVar(ZipFile)).Items.Count = 1
'DoEvents
Sleep 100
Debug.Print " .";
Loop
Debug.Print
On Error GoTo 0
Debug.Print Timer, "Zip done."
End With
Set ShellApplication = Nothing
ZipCurrentProject = Err.Number
End Function
Next in the macro call the module (can be done in VBA but simpler to see what is going on if it’s in a macro)
Action = RunCode
Arguments = SendTheBackUp()
Function Name = SendTheBackUp
Next code the button to send the back up somewhere –
Private Sub cmd_backup_Click()
DoCmd.RunMacro "mcr_Backup", , ""
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Dim strpath As String
Dim varMail As Variant
Dim varMailCC As Variant
Dim varSubject As Variant
Dim varcontact As Variant
Dim varBody As Variant
strpath = "This is where the file will go to "
strfile = CurrentProject.Path & "\_My_Back_created_on_" & Format(Date, "mm-dd-yyyy") & "_at_" & Format(Time, "hh-mm") & ".zip"
‘of course you can take the details (mail address, name, etc) from the form controls if you prefer, but I have hard coded them below to show you where the details are.
varMail = "Email address removed"
varMailCC = "Email address removed"
varSubject = "Hey, I've made a backup"
varcontact = "Some name here"
varBody = "<P><font face=calibri color=Black style= font-size:20px> " & varcontact & " <p>A backup has been created." & Chr(13) + Chr(10) & Chr(13) + Chr(10) _
& "Add more information here if you want to.</p>"
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = varName
.CC = varCC
.Subject = varSubject
.HTMLBody = varBody
.Attachments.Add (strfile)
.Display
End With
End Sub
Hope this helps
K
- RBStewartMar 16, 2022Copper Contributor
Thank you karenlorr_uk
While my needs are different, the code I have (with variations to fit my needs) is roughly the same as yours. However, my problem is in the zip file that gets created when the value of 'ZipFile' in the code block below is a path onto a flash drive or MicroSD card.
Open ZipFile For Output As #FileNumber Print #FileNumber, Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, vbNullChar) Close #FileNumber
The created zip file has some sort of access gate that requires an unknown, non-existent disk. This happens on virtually every flash drive and MicroSD on which I have tried to create such a zip file.
My vb code workaround is simply to copy a pre-existing, empty zip file that was created on an internal hard drive onto the portable storage device. Such a copy works fine. I can access it, send files to it extract files from it as needed, and without the pesky gate calling for a non-existent disk.
Thank you for taking the time to offer this suggestion.
- karenlorr_ukMar 17, 2022Copper ContributorYou're right - and I don't know why.
I have just created a small sample database with just 1 module and form and I get the same warning message as you (this was on a new USB - so n hidden folders) if the path is directed to a zip folder. But it works fine on a hard drive.
I get the same problem even after a full format of the USB.
So the VBA works OK and it's not an Access VBA problem.
Sorry I can't be of any more help with this. Maybe it would be a good idea to post in the Windows forum and see if there are any work-arounds. Of course you could just write the backup to your hard drive and then, in the same module, move the folder to the USB, but this starts to get messy and there really shouldn't be a need to do this.- isladogsMar 21, 2022MVP
Two questions about your SendThe Backup function
1. Why are you creating an empty folder with the same name as the zip file?
It seems to be unnecessary
These 3 lines can be removed & the zip file is still created correctly
If Dir(ZipPath, vbDirectory) = "" Then MkDir ZipPath End If
2. The final line doesn't compile so I removed it
ZipCurrentProject = Err.Number
Does it have any purpose?