Hi,
Am trying it but getting errors. I'll carry on with it and get back to you.
Many thanks...
Main Topics
Browse All TopicsHi there,
I'm trying to implement some error handling in VB program which essentially creates a set of folders and names them based on a message box input. I specifically want to flag if someone tries to create the same named folder twice.
The code I've attached is stripped down to the significant bits. I know that I may be getting confused as whether err.number is global but the issue I have is that I thought 'on error resume next' would ensure that the code continued.
My debugging tells me that it doesn't seem to do this.
In short, how do I fix it so that it error traps correctly?
Many thanks in advance...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Maybe it's easier to user the fso.FolderExists method to test whether the folder already exists. This way, no error is raised at all because you don't try to CreateFolder when the folder already exists....
However, because you are checking the return code, we Raise our own error event so you can identify what went on.
Regards,
Rob.
Hi there guys,
You've both moved me forward a bit on this one but I currently using Rob's method if that is okay. The error is being checked correctly and I can even raise a msgbox at the time.
The issue I'm still having is that when an error is encountered (or raised in this case), the 'EndProc' Subroutine is not actually executed, so I don't get an error messge in the error handling part. Presumably because the function 'MainProc' (which is passed as a parameter to EndProc) is bombing out completely on encountering an error. It does drop through the routine on a clean run (i.e. no duplicate folders)
This is where I'm struggling. I would love for the error handling to happen every time but is is not at the moment.
I attached the whole code as you need to know that there is processing after folder creation which needs to happen (basically copying and renaming files across)
Am I making sense?
Hi all,
I've kind of come full circle on this one. I don't check for duplicate folders as suggested, I just attempt to create a new folder and just use the native error trapping. Instead of passing parameters, I just use err.number. In fact I don't use functions at all; just subs (I've attached my final version). The problems I was having originally have actually been solved by Rob's last post. It was a simple matter of sequencing the code properly.
I am therefore going to give Rob more of the points. I hope this is okay.
I do have a little question though. The instruction, 'On error resume next' has me confused. I thought it meant that the code would just carry on despite an error. But it appears that it actually causes the containing function or sub to drop out and THEN carry on processing.
Do I have it right?
>> But it appears that it actually causes the containing function or sub to drop out and THEN carry on processing.
No, it shouldn't do that. If you have On Error Resume Next at the top, and then you have three lines of code in your file, when line 2 causes an error, line 3 will still try to execute. Perhaps though, what you're seeing, is only the *last* error from the MainProc sub. The way you have it there, the entire MainProc sub *will* run, but by the time code executes the EndProc sub, it will only report on the *last* error.
What you would need to do is, instead of calling the EndProc sub after the MainProc sub, is call the EndProc sub at *possible* error points throughout the MainProc sub. The EndProc sub does not "automatically" get called upon error. I have posted a revised version below that may work better for you.
Regards,
Rob.
Hmmmm. This is odd.
First off, if things work the way you say, your method would repeatedly show the 'Folder created' message on a clean error free run.
Just to see what was happening I placed msgbox statements in the same places you had calls to EndProc. Sure enough, they all popped up on a clean run, showing err.number of 0.
I then attempted to create a duplicate folder and it jumped straight out and was flagged as an error in EndProc. Don't get me wrong, this is exactly what I want it to do but I'm still confused as to the nature of 'On error resume next'.
I really should look at this at home as I have a VBScript editor/debugger (None at work!!!) but I lose the urge as soon as I walk out of the office!
>> if things work the way you say, your method would repeatedly show the 'Folder created' message on a clean error free run.
That's right, but only because you have a MsgBox saying so. You could either comment out an "error free" message, or you could make the code make a log, say, adding any output to strResult, which you echo at the end of the code. See if this helps any more. What I've done is pass a string to EndProc that details the action that was attempted, so you get more info on where the error occurred as well. The code runs silently and gives you a log at the end, when strResult is output.
On Error Resume Next basically just prevents code from aborting with an error. It's never really helpful to leave On Error Resume Next in, and have no other error checking, because you'll never know if any errors occur.
Regards,
Rob.
Business Accounts
Answer for Membership
by: bluntTonyPosted on 2009-08-27 at 06:11:49ID: 25197630
How about not using error trapping to catch this.
I personally would enumerate the folders into a dictionary object, then check if the user's input exists in this dictionary. Example of this logic below.
You can still use error trapping to catch errors on folder creation, but if you can add your own logic to catch bad user input I would.
Tony
Select allOpen in new window