Question

pass a function as a parameter in VBScript and error handling

Asked by: JohnSaint

Hi 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...

On error resume next
Dim CompCode, NDSTR
 
'Input Box for student names
NDSTR=inputbox( "Enter Your Full Name","Exam Folder Creation",NDSTR)
 
If NDSTR<>"" then
   EndProc(MainProc)
End if
 
Function MainProc()
'Some variables here...
  testloc  = "x:\" & NDSTR & " (SKL4200 Test)"
  testlocimg  = "x:\" & NDSTR & " (SKL4200 Test)\Images"
 
 Set fso  = CreateObject("Scripting.FileSystemObject")
 
'Create Student Test folder(s)   
 
 Set f = fso.CreateFolder(testloc)
 Set f = fso.CreateFolder(testlocimg)
 
'Check for existing folder
  if err.number <> 0 then
     MainProc = err.number  
     Exit Function 
  end if
 
' Do rest of function...
 
  MainProc = err.number
 
End Function
 
Sub EndProc(CompCode)
 
'Error handling
   msgbox CompCode
   if CompCode <> 0 then
     if CompCode = 58 then
       msgbox "You have already created your test folder. Please check the ExamCompleted drive"
     else
       msgbox err.number & " - " & err.description
     end if
   else  
     msgbox "Folder " & NDSTR & " (SKL4200 Test) created."
   end if
 
End Sub

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-27 at 05:50:50ID24686384
Tags

VBScript

,

Functions

,

error handling

Topic

VB Script

Participating Experts
2
Points
500
Comments
10

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Trapping VBScript Errors
    how can I trap an Error ex. if a database operation fails instead of having vbscript display an error message I would like to trap the error myself. In VB I could use On Error this does not seem to work with VBScript
  2. err.number vs objConn.error
    What is exactly the difference in use ? I know err.number needs the On Error Resume Next statement at the top of the ASP page but does objConn.error also need this as well ? I have code like this that checks for database errors: IF objConn.Errors.count > 0 THEN ...
  3. Err.Number is not being cleared?
    sSel = txtSql.Text On Error Resume Next ' Disabling the trap!! lstg.Open sSel, cn, adOpenKeyset, adLockOptimistic If Err.Number <> 0 Then ' there must be an error Label2.Caption = "Syntax Error" ...
  4. VBScript "On Error Resume Next"
    Hi. I have a page that sends information to the back end of our system. If the back end is down I would like it to store the information in a database. Is there a way to use "on error" to call a function instead of "resume next"? What I would like is...
  5. Err.Number -2147467259
    I have the following code in my vbscript Set sXML = CreateObject("rbxml.XML") sTestXml = sXML.QueryXML(1000, "") if (err.Number = 0) MsgBox("Success") else MsgBox ("Number: " & err.Number & " Description:"...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

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

Set objFolders = CreateObject("Scripting.Dictionary")
Set objFso = CreateObject("Scripting.FileSystemObject")
 
strRoot = "C:\"
 
Set rootFolder = objFso.GetFolder(strRoot)
 
For Each folder In rootFolder.SubFolders	
	objFolders.Add UCase(folder.Name), UCase(folder.Name)
Next
 
NDSTR=inputbox("Enter Your Full Name","Exam Folder Creation",NDSTR)
 
If NDSTR <> "" then
   If Not objFolders.Exists(UCase(NDSTR)) Then
   		'PUT YOUR CODE TO CREATE THE FOLDER HERE
   Else
   		'GIVE USER A MESSAGE THAT THE FOLDER ALREADY EXISTS
   End If
End if

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:

Select allOpen in new window

 

by: JohnSaintPosted on 2009-08-27 at 06:51:29ID: 25198113

Hi,
Am trying it but getting errors. I'll carry on with it and get back to you.

Many thanks...

 

by: bluntTonyPosted on 2009-08-27 at 09:30:25ID: 25200070

If you get stuck post us your code and the error message you're getting with the line number.

Thanks,

Tony

 

by: RobSampsonPosted on 2009-08-27 at 18:24:22ID: 25204336

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.

On error resume next
Dim CompCode, NDSTR
 
'Input Box for student names
NDSTR=inputbox( "Enter Your Full Name","Exam Folder Creation",NDSTR)
 
If NDSTR<>"" then
   EndProc(MainProc)
End if
 
Function MainProc()
'Some variables here...
  testloc  = "x:\" & NDSTR & " (SKL4200 Test)"
  testlocimg  = "x:\" & NDSTR & " (SKL4200 Test)\Images"
 
 Set fso  = CreateObject("Scripting.FileSystemObject")
 
'Create Student Test folder(s)   
 
 Err.Clear
 If fso.FolderExists(testloc) = False Then
 	fso.CreateFolder(testloc)
 Else
 	' the folder already exists, so we return the error that you are catching
 	Err.Raise 58, "Scripting.FileSystemObject", testloc & " already exists."
 End If
 
 If fso.FolderExists(testlocimage) = False Then
 	fso.CreateFolder(testlocimg)
 Else
 	' the folder already exists, so we return the error that you are catching
 	Err.Raise 58, "Scripting.FileSystemObject", testlocimg & " already exists."
 End If
 
 MainProc = err.number
 
End Function
 
Sub EndProc(CompCode)
 
'Error handling
   msgbox CompCode
   if CompCode <> 0 then
     if CompCode = 58 then
       msgbox "You have already created your test folder. Please check the ExamCompleted drive"
     else
       msgbox err.number & " - " & err.description
     end if
   else  
     msgbox "Folder " & NDSTR & " (SKL4200 Test) created."
   end If
   Err.Clear
 
End Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:

Select allOpen in new window

 

by: JohnSaintPosted on 2009-08-28 at 03:47:30ID: 25206284

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?  

On error resume next
Dim CompCode, NDSTR
 
'Input Box for student names
NDSTR=inputbox( "Enter Your Full Name","Exam Folder Creation",NDSTR)
 
If NDSTR<>"" then
   EndProc(MainProc)
End if
 
Function MainProc()
 
'Test Material variables
 
  testmat  = "n:\SKL4200 Test\*.*"
  testmatimg = 	"n:\SKL4200 Test\Images\*.*"
 
'Test Completed variables
 
  testloc  = "x:\" & NDSTR & " (SKL4200 Test)"
  testlocimg  = "x:\" & NDSTR & " (SKL4200 Test)\Images"
  testfileA = "Access Test DB.mdb"  
  testfileE = "Excel raw data.xls"
  markfile = "SKL4200 mark sheet.xls"	
  studfileA = NDSTR & " Access test.mdb"		
  studfileE = NDSTR & " Excel test.xls"		
  studfilEw = NDSTR & " Word test.doc"		
  studmark = "Mark Sheet for " & NDSTR & ".xls"		 
 
 Set fso  = CreateObject("Scripting.FileSystemObject")
 
'Create Student Test folder(s)   
 
 Err.Clear
 If fso.FolderExists(testloc) = False Then
 	fso.CreateFolder(testloc)
 Else
 	' the folder already exists, so we return the error that you are catching
	msgbox "Folder exists"
 	Err.Raise 58, "Scripting.FileSystemObject", testloc & " already exists."
 End If
 
 If fso.FolderExists(testlocimage) = False Then
 	fso.CreateFolder(testlocimg)
 Else
 	' the folder already exists, so we return the error that you are catching
 	Err.Raise 58, "Scripting.FileSystemObject", testlocimg & " already exists."
 End If
 
'Copy test  material from original folder  	  
 
  fso.Copyfile testmat, testloc 	 	
  fso.Copyfile testmatimg, testlocimg
 
 
'Rename test material to student names 	 	
 
  fso.Movefile testloc & "\" & testfileA, testloc & "\" & studfileA	
  fso.Movefile testloc & "\" & testfileE, testloc & "\" & studfileE	
  fso.Movefile testloc & "\" & markfile, testloc & "\" & studmark	
 
'Create blank Word document and name after Student
 
  Set objWord = CreateObject("Word.Application")
 
'note: if you dont want word popping up and displaying as this is built set this next line to False
 
  objWord.Visible = False
  Set objDoc = objWord.Documents.Add()
 
'save the Word Document
 
  objDoc.SaveAs(testloc & "\" & studfileW)
  objWord.Quit
 
 MainProc = err.number
 
End Function
 
Sub EndProc(CompCode)
 
'Error handling
   msgbox CompCode
   if CompCode <> 0 then
     if CompCode = 58 then
       msgbox "You have already created your test folder. Please check the ExamCompleted drive"
     else
       msgbox err.number & " - " & err.description
     end if
   else  
     msgbox "Folder " & NDSTR & " (SKL4200 Test) created."
   end If
   Err.Clear
 
End Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:

Select allOpen in new window

 

by: RobSampsonPosted on 2009-09-02 at 01:06:09ID: 25238837

Hmmmm, shouldn't you change this:
If NDSTR<>"" then
   EndProc(MainProc)
End if

to this
If NDSTR<>"" then
   MainProc
   EndProc(CompCode)
End if

so that both function are run in sequence?

Rob.

 

by: JohnSaintPosted on 2009-09-02 at 03:24:08ID: 25239490

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?  

On error resume next
 
Dim NDSTR, testloc
 
'Input Box for student names
NDSTR=inputbox( "Enter Your Full Name","Exam Folder Creation",NDSTR)
 
If NDSTR<>"" then
   MainProc	
   EndProc
End if
 
Sub MainProc
 
'Test Material variables
 
  testmat  = "n:\SKL4200 Test\*.*"
  testmatimg = 	"n:\SKL4200 Test\Images\*.*"
 
'Test Completed variables
 
  testloc  = "x:\" & NDSTR & " (SKL4200 Test)"
  testlocimg  = "x:\" & NDSTR & " (SKL4200 Test)\Images"
  testfileA = "Access Test DB.mdb"  
  testfileE = "Excel raw data.xls"
  markfile = "SKL4200 mark sheet.xls"	
  studfileA = NDSTR & " Access test.mdb"		
  studfileE = NDSTR & " Excel test.xls"		
  studfilEw = NDSTR & " Word test.doc"		
  studmark = "Mark Sheet for " & NDSTR & ".xls"		 
 
  Set fso  = CreateObject("Scripting.FileSystemObject")
 
'Create Student Test folder(s)   
 
  Set f = fso.CreateFolder(testloc)
  Set f = fso.CreateFolder(testlocimg)
 
'Copy test  material from original folder  	  
 
  fso.Copyfile testmat, testloc 	 	
  fso.Copyfile testmatimg, testlocimg
 
 
'Rename test material to student names 	 	
 
  fso.Movefile testloc & "\" & testfileA, testloc & "\" & studfileA	
  fso.Movefile testloc & "\" & testfileE, testloc & "\" & studfileE	
  fso.Movefile testloc & "\" & markfile, testloc & "\" & studmark	
 
'Create blank Word document and name after Student
 
  Set objWord = CreateObject("Word.Application")
 
'note: if you dont want word popping up and displaying as this is built, set this next line to False
 
  objWord.Visible = False
  Set objDoc = objWord.Documents.Add()
 
'save the Word Document
 
  objDoc.SaveAs(testloc & "\" & studfileW)
  objWord.Quit
 
End Sub
 
Sub EndProc
 
'Error handling
   if err.number <> 0 then
     if err.number = 58 then
       msgbox "You have already created your test folder, '" & testloc & "'. Please check the ExamCompleted drive"
     else
       msgbox err.number & " - " & err.description
     end if
   else  
     msgbox "Folder " & testloc & " created."
   end If
 
   Err.Clear
 
End Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:

Select allOpen in new window

 

by: RobSampsonPosted on 2009-09-02 at 19:32:03ID: 25247429

>> 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.

On error resume next
 
Dim NDSTR, testloc
 
'Input Box for student names
NDSTR=inputbox( "Enter Your Full Name","Exam Folder Creation",NDSTR)
 
If NDSTR<>"" then
   MainProc	
End if
 
Sub MainProc
 
'Test Material variables
 
  testmat  = "n:\SKL4200 Test\*.*"
  testmatimg = 	"n:\SKL4200 Test\Images\*.*"
 
'Test Completed variables
 
  testloc  = "x:\" & NDSTR & " (SKL4200 Test)"
  testlocimg  = "x:\" & NDSTR & " (SKL4200 Test)\Images"
  testfileA = "Access Test DB.mdb"  
  testfileE = "Excel raw data.xls"
  markfile = "SKL4200 mark sheet.xls"	
  studfileA = NDSTR & " Access test.mdb"		
  studfileE = NDSTR & " Excel test.xls"		
  studfilEw = NDSTR & " Word test.doc"		
  studmark = "Mark Sheet for " & NDSTR & ".xls"		 
 
  Set fso  = CreateObject("Scripting.FileSystemObject")
 
'Create Student Test folder(s)   
 
  Set f = fso.CreateFolder(testloc)
  EndProc
  Set f = fso.CreateFolder(testlocimg)
  EndProc
 
'Copy test  material from original folder  	  
 
  fso.Copyfile testmat, testloc 	 	
  EndProc
  fso.Copyfile testmatimg, testlocimg
  EndProc
 
 
'Rename test material to student names 	 	
 
  fso.Movefile testloc & "\" & testfileA, testloc & "\" & studfileA	
  EndProc
  fso.Movefile testloc & "\" & testfileE, testloc & "\" & studfileE	
  EndProc
  fso.Movefile testloc & "\" & markfile, testloc & "\" & studmark	
  EndProc
 
'Create blank Word document and name after Student
 
  Set objWord = CreateObject("Word.Application")
 
'note: if you dont want word popping up and displaying as this is built, set this next line to False
 
  objWord.Visible = False
  Set objDoc = objWord.Documents.Add()
 
'save the Word Document
 
  objDoc.SaveAs(testloc & "\" & studfileW)
  objWord.Quit
  EndProc
 
End Sub
 
Sub EndProc
 
'Error handling
   if err.number <> 0 then
     if err.number = 58 then
       msgbox "You have already created your test folder, '" & testloc & "'. Please check the ExamCompleted drive"
     else
       msgbox err.number & " - " & err.description
     end if
   else  
     msgbox "Folder " & testloc & " created."
   end If
 
   Err.Clear
 
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:

Select allOpen in new window

 

by: JohnSaintPosted on 2009-09-03 at 05:42:30ID: 25250008

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!

 

by: RobSampsonPosted on 2009-09-03 at 19:23:19ID: 25256515

>> 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.

On error resume next
 
Dim NDSTR, testloc, strResults
 
'Input Box for student names
NDSTR=inputbox( "Enter Your Full Name","Exam Folder Creation",NDSTR)
 
If NDSTR<>"" then
   MainProc
   MsgBox strResults
End if
 
Sub MainProc
 
'Test Material variables
 
  testmat  = "n:\SKL4200 Test\*.*"
  testmatimg = 	"n:\SKL4200 Test\Images\*.*"
 
'Test Completed variables
 
  testloc  = "x:\" & NDSTR & " (SKL4200 Test)"
  testlocimg  = "x:\" & NDSTR & " (SKL4200 Test)\Images"
  testfileA = "Access Test DB.mdb"  
  testfileE = "Excel raw data.xls"
  markfile = "SKL4200 mark sheet.xls"	
  studfileA = NDSTR & " Access test.mdb"		
  studfileE = NDSTR & " Excel test.xls"		
  studfilEw = NDSTR & " Word test.doc"		
  studmark = "Mark Sheet for " & NDSTR & ".xls"		 
 
  Set fso  = CreateObject("Scripting.FileSystemObject")
 
'Create Student Test folder(s)   
 
  Set f = fso.CreateFolder(testloc)
  EndProc "Creating " & testloc
  Set f = fso.CreateFolder(testlocimg)
  EndProc "Creating " & testlocimg
 
'Copy test  material from original folder  	  
 
  fso.Copyfile testmat, testloc 	 	
  EndProc "Copying " & testmat & " to " & testloc
  fso.Copyfile testmatimg, testlocimg
  EndProc "Copying " & testmatimg & " to " & testlocimg
 
'Rename test material to student names 	 	
 
  fso.Movefile testloc & "\" & testfileA, testloc & "\" & studfileA	
  EndProc "Moving " & testloc & "\" & testfileA & " to " & testloc & "\" & studfileA	
  fso.Movefile testloc & "\" & testfileE, testloc & "\" & studfileE	
  EndProc "Moving " & testloc & "\" & testfileE & " to " & testloc & "\" & studfileE
  fso.Movefile testloc & "\" & markfile, testloc & "\" & studmark	
  EndProc "Moving " & testloc & "\" & markfile & " to " & testloc & "\" & studmark	
 
'Create blank Word document and name after Student
 
  Set objWord = CreateObject("Word.Application")
 
'note: if you dont want word popping up and displaying as this is built, set this next line to False
 
  objWord.Visible = False
  Set objDoc = objWord.Documents.Add()
 
'save the Word Document
 
  objDoc.SaveAs(testloc & "\" & studfileW)
  objWord.Quit
  EndProc "Creating word document"
 
End Sub
 
Sub EndProc(strAction)
 
'Error handling
   if err.number <> 0 then
     if err.number = 58 then
       'msgbox "You have already created your test folder, '" & testloc & "'. Please check the ExamCompleted drive"
       strResults = strResults & VbCrLf & "You have already created your test folder. Please check the ExamCompleted drive" & VbCrLf & "Err number: " & Err.Number & VbCrLf & "Err Description: " & Err.Description & VbCrLf & "Action: " & strAction
     else
       'msgbox err.number & " - " & err.description
       strResults = strResults & VbCrLf & "Err number: " & Err.Number & VbCrLf & "Err Description: " & Err.Description & VbCrLf & "Action: " & strAction
     end if
   else  
     'msgbox "Folder " & testloc & " created."
     strResults = strResults & VbCrLf & "Command Successful" & VbCrLf & "Action: " & strAction
   end If
 
   Err.Clear
 
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:

Select allOpen in new window

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...