Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Index   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > TechHelp > Directory > Access > Zip Unzip Files 2 < Zip Unzip Files | Billing Statements >
Back to Zip Unzip Files 2    Comments List
Transcript Pinned    Upload Images   Link   Email  
Richard Rost             
2 months ago
Today is part two of my video series on how to zip and unzip files. Part two of two, just two for now. Yesterday, in part one, we learned how to unzip a file and drop all the files that are in it into a folder. Today we're going to go the opposite direction. We're going to take a folder and zip it all up so you can send it to someone else, store it, or put it on a floppy disk. Remember those? All right, here we go. Oh yeah, it's part two, so if you haven't watched part one yet, go do that like first or something and go watch that now.

Today's video is going to be a little more complicated than part one, and one of the things that we're going to be using is a while loop to loop through all the files in the folder and add them to our zip file. So, if you don't know how to use a while loop, go watch this video first. We are also going to need to use the sleep function to make sure that we wait an appropriate amount of time between copying the files into the zip file, so go watch this too. You'll need this. All right, let's go back into our database.

We've got our destination folder here, this is the files that we unzipped out of here so I'm going to just copy this guy, copy and paste, and let's call it source folder so we get the same stuff in there, just want to make sure, I don't want to confuse anybody, it's the same small files. Let's go back in the database. Alright, let's go back to design view, let's copy our unzip button, copy paste, and we'll slide it over here and we'll call this, guess what, this will be the zip button and let's give it a good name. Let's make this zip button. Yeah, I know this guy is still hello world button, that's okay, you can live with it. Build event, let's go.

And I got a bunch of extra space. I sometimes put a bunch of extra space at the bottom of my VB editor because I hate working at the very, very bottom of the window, so I do this a lot. But then if you put something else new in, it tends to come in down here. So that's weird. Anyways. Okay.

This is going to be a little more complicated than unzipping a file. And I've tried to simplify it as much as possible but it's still a little weird. In a nutshell, the first thing we have to do is create an empty zip file shell file, basically. It's an empty zip file that we're going to then copy the files into. It's weird, I know, but it's the easiest way to do it. And we can copy some of our declarations from up here. Let's copy this, paste it down here. It's going to be OSObject again. This is going to be, down here, we're going to be source folder and then destination zip. We're going to just flip those back, okay? We're starting with a source folder and then we're going to put them into a destination zip file. All right, so first up is create an empty zip file. How do we do that? Well, again, this is just a block of code that I'm just going to write for you and you'll see how it works. All right, we're going to open our destination zip file for output as number one. Now, yes, you could have multiple file handles open, number one, two. In my more advanced developer classes, I show you how to check to see if a number is available, but this should work for most instances unless you've got lots of other file I.O. going on in the background. Now this next line, I'm going to copy from my notes. Okay, that is the header information for a zip file. It's characters 80, 75, 5, 6, and then a string of 18 zeros. Why? That's just, that's the header that has to be on the top of the zip file. That's so Windows recognizes it as a valid zip file. You just got to do it. Okay, and then when you're done with that, we can close number one. Okay, so our empty zip file is created at this point. Now we can copy this and put that down here. All right, that's our object shell. We can specify our file names now. In fact, I'm going to just copy these two, copy these, paste them down here with some slight modifications. Whoops. All right, we have a source folder and a destination zip file. So copy that, paste it there. Now the source folder is going to be this guy, source folder. So we'll put that in there. Right here, source folder. And the destination zip is going to be, let's call it new zip dot zip, just like that. Okay?

Alright, so we got our variables all declared and all set up. Next up comes the actual file command, the copy command. So we're just going to take this right here. You don't need that yes to all on the end. Alright? Paste it there. We're just going to flip two things. We're going to copy from the source file. There you go. Okay. Now, here's the weird part. This part up here happens relatively quickly and it'll just go until it's done. Here you want to, if you're going to have more stuff happen after this, you want to wait until it's finished copying because it could take a while in the background. So this is where we need that while loop to check and make sure that the number of files are actually copied to the zip file. And again, I'm just going to copy and paste this code for you. All right, here it is, loop until finished. It's going to say while destination zip items count. That's the number of items in the zip file. While that is less than the number of items in the source folder, we're going to status the word zipping, and we're going to pause a second. Because depending on the size of your files, it could take two, four, 10 seconds. So you want to loop until that's done. And yes, in the extended cut, I'm going to show you how to put a timeout on here. So if it takes more than 60 seconds, you can have it abort. But for now, it's just going to loop until it's done. Now that we're all done with that, we can do this stuff right here. Paste. Set O equals nothing. Status done, and then beep. We didn't put a start status up here. Let's say start. We'll put status unzipping up top on this one here. Okay, so back down here. All right, let's save it. Debug compile. Everything looks good. Let's go ahead and run the zip. And oh, wait a minute. Path file access error. What's going on? I realized this a few minutes ago, but I didn't say anything because I want to see if you guys can figure it out. All right, path file access error. What does that mean? Well, that usually means it can't write the file. OK. All right, what does that mean? Let's hit debug. That's the line it stops at. Now let's examine what destzip is. If you hold your mouse over it, it says empty. Anyone see why? Yeah, look what I did. I caught this earlier as I was typing it. I declared and set up destzip down underneath here, so it doesn't have a value by the time this runs.  All right, so we're literally just going to go like this and move this stuff up here. Right? Declare your variables and initialize them before you actually use them. Alright. Now, see, and that's just to show you a runtime error versus a syntax error. Debug compile catches syntax errors. Like if you had something that wasn't spelled right in here, you know, like output z, for example. And it usually catches it with that. But a runtime error is vicious. Runtime errors can sometimes be very hard to hunt down. But let's give it a shot now. Ready, click, and done. See how it kind of paused a second there? Because in the background, Windows is copying those files into the zip file. Let's check our folder and we've got new zip dot zip right there. Open it up and looks good. Make sure the files are good. There's Kirk and there's Picard. And my text file. Okay. All right. See how that works now? Now that is the simplest version of the code that I can put together for you. Okay. I've got a much more elaborate version where again I'll put a timer on there so that we can say, okay, if it takes more than 60 seconds, abort or you could put in abort box on your form too. That also works. Check that inside the loop. We will check to make sure when it's done if the number of files are correct. So if it's expecting to copy three files and make sure it did copy three files, we can verify that too when we're unzipping as well. So that's all stuff. We're going to cover in the extended cut for the members. We'll verify the number of files zip and unzipped. We'll put a counter on there so you can say okay, you're allowed like 15 retries or whatever. All right, like 15 retries or whatever. If it's going to take more than a minute, then I want you to cancel out. And I'll show you how to extract specific files or file types. So if you want just the text files, that's why that text file is in there. I was playing with it earlier. Or let's say you just want to extract just Kirk.jpg. That's the only file you want. I'll show you how to do it. Silver members and up get access to all of my extended cut videos, so join today. And also, if you want to learn more about working with files and folders and the file system object and all that stuff in Access, I've got a series starting with Access Developer 30, where I cover classic VB file input and output, reading and writing text files. Developer 31 covers VB file and folder navigation, copying files, compacting your back-end files and copying them to a different location. Developer 33 covers relinking your back-end files automatically, the file system object, lots more. But that in a nutshell, folks, is how you zip and unzip files. I hope you learned something. That's going to be your TechHelp video for today. Live long and prosper, my friends. I'll see you next time. See the more link below the video to find additional resources and links. YouTube does a pretty good job of hiding it. It's right down there. See this part of the description here. The name, the videos up here, there's a little show more down there. Down the bottom. It's kind of hard to find. But once you click on that, you'll see a list of other videos, additional information related to the current topic, free lessons, and lots more. And YouTube no longer sends out email notifications when new videos are posted like they used to do. But if you'd like to get an email every time I post a new video, click on the link to join my mailing list. And you can pick how frequently you get emails from me, either as they happen, daily, weekly, or monthly. Now if you'd like to become a paid member of my channel and receive all kinds of awesome perks, click on the join button. You'll see a list of all the different membership levels that are available each with its own special perks including my extended cut videos, access to my code vault, lots of VBA source code in there, template downloads, and lots more. I'll talk more about these perks at the end of the video. Even if you don't want to commit to becoming a paid member and you'd like to help support my work, please feel free to click on the tip jar link. Your patronage is greatly appreciated and will help keep these free videos coming. I've got some puppies to feed. But don't worry, no matter what, these free tech help videos are going to keep coming. As long as you keep watching them, I'll keep making more and they'll always be free. Now if you really want to learn Access and you haven't tried my free Access Level 1 course, check it out now. It covers all the basics of Microsoft Access, including building forms, queries, reports, and more. It's over 4 hours long. You can find it on my website or on my YouTube channel. I'll put a link down below you can click on. And did I mention it's completely free? The whole thing, free, 4 hours, go watch it. And okay, okay, a lot of you have told me that you don't have time to sit through a four-hour course. So I do now have a quicker Microsoft Access for Beginners video that covers all the basics faster in about 30 minutes. No I didn't just put the video on fast forward. But I'll put a link to this down below as well. Now if you like level one, level 2 is just a dollar. That's it, one dollar. That's another whole 90-minute course. Level 2 is also free for paid members of any level, including supporters. So if you're a member, go watch level 2. It's free. Okay, want to get your question answered in a video just like this one? Visit my TechHelp page and send me your question there. Members get priority of course. While I do try to read and respond to all of the comments posted below in the comment section, I only have time to go through them briefly a couple of times a month and sometimes I get thousands of them. So send me your question here on the TechHelp page and you'll have a better chance of getting it answered. And while you're on my website, be sure to stop by my Access Forum. We've got lots of lively conversations about Microsoft Access and other topics. I have a fantastic group of moderators who help me answer questions. Shout out to Alex, Kevin, Scott, Adam, John, Dan, Juan, and everybody else who helps out on the site. I appreciate everything you do. I couldn't do it without you. Be sure to follow my blog. Find me on Twitter and of course on YouTube. Yeah, I'm on Facebook too, but I don't like Facebook. Don't get me started. Now let's talk more about those member perks. If you do decide to join as a paid member, there are different levels: Silver, Gold, Platinum, and Diamond. Silver members and up get access to all of my extended cut TechHelp videos, one free beginner class every month, and some other perks. Gold members get all the previous perks plus access to download the sample databases that I build in my TechHelp videos plus access to my code vault where I keep tons of different functions that I use, the code that I build in most of the videos. You'll also get higher priority if you do submit any TechHelp questions. Now answers are never guaranteed but you do go higher in the list for me to read them. And if I like your question, you've got a good chance of it being answered. You'll also get one free expert-level class each month after you've finished the beginner series. Platinum members get all the previous perks plus even higher priority for TechHelp questions. You get access to all of my full beginner-level courses for every subject. And I cover lots of different subjects like Word, Excel, VBA, ASP, lots of ins and not just Access. These are the full-length courses found on my website, you get all the beginner ones in addition once you finish the expert classes you get one free developer class promote so lots of training. And finally, you can also become a diamond sponsor. You'll have your name or your company name listed on a sponsors page that will be shown on each video as long as you're a sponsor. You'll get a shout out in the video and a link to your website or product in the text below the video and on my website. So that's it. Once again, my name is Richard Rost. Thank you for watching this video brought to you by AccessLearningZone.com. I hope you enjoyed it. I hope you learned something today. Live long and prosper my friends. I'll see you again soon. Live long and prosper my friends. Live long and prosper my friends. I'll see you again soon.

TOPICS:
Creating an empty zip file shell
Writing header information to zip file
Using OSObject in VBA
Declaring and initializing variables in VBA
Copying files into a zip file
Using a while loop to monitor zipping progress
Implementing a sleep function to manage process timing
Handling runtime errors in VBA
Debugging and resolving path file access errors
Successful file zipping demonstration in VBA

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Zip Unzip Files 2.
 

 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2024 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 6/19/2024 11:53:50 PM. PLT: 1s