Error Handing for a VBS script

Hi,

I am trying to create a generica VBS script to pull information from the Active directory. the script takes in inputs based on what active directory OU you want to run it against and runs the query based on those inputs. The script works fine but I want to create some error handing in case the inputs are incorrectly formatted or unrecognised. basically if the incorrect OU is chosen, or there is an typo in the input I want the script to flag that and promot for input again. I have tried to do it using if err <>0 but it doesnt pick up the error as being flagged. I suspect the issue is that the command object contains the error but I am unsure as to how to read this error and respond approporatly. I hope this makes sense, let me know if you require further detail. Sorry, I am in the process of teaching myself VBS and I may not be explaining myself quite right.

In the example below I am passing incorrect parameters for the level3OU, Level2OU and LEVEL1OU variables. It thows the error "Provider: Table does not exist." as expected but the "If err.number <> 0 Then" code does not kick in and try to run the chooseOUDirectory subroutine again to prompt for the variables again.
'Target the Search against the specified OU
	Const ADS_SCOPE_SUBTREE = 2
	Set objConnection = CreateObject("ADODB.Connection")
	Set objCommand = CreateObject("ADODB.Command")
	objConnection.Provider = "ADsDSOObject"
	objConnection.Open "Active Directory Provider"
	Set objCOmmand.ActiveConnection = objConnection
	objCommand.CommandText = _
	"Select Name, Location from 'LDAP://" & level3OU & level2OU & level1OU & parentOU & strDNSDomain & "' " _
	& "Where objectCategory='computer'"
	objCommand.Properties("Page Size") = 1000
	objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
	Set objComputerFile = objCommand.Execute
		If err.number <> 0 Then
		strMbox = MsgBox("Cannot connect to that OU please try again", 1, "Formatting Error")
		If strMbox = 1 Then
			WScript.echo "Failed to connect to " 
			chooseOUDirectory
		Else 
		If strMbox = 2 Then
			WScript.exit
		End If
		End If
End If

Open in new window

LVL 4
walshdjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shift-3Commented:
I haven't read through the whole script, but I do see one thing right off the bat.

You need to have an On Error Resume Next at the beginning in order for error handling to work correctly.  Otherwise it will just stop at the first error, as you're experiencing.

See this article for more information.
0
walshdjAuthor Commented:
Thanks for the comments. Here  this i have just attached a code snippit. I do have on error resule at the top of the code.

I have been tuning on and off the on error resume next paramater at the top of the script so that I can troubleshoot the error. Eventually I will put it in the correct place in the code.
0
RobSampsonCommented:
Hi, you basically need to use
On Error Resume Next

and
Err.Clear
On Error GoTo 0

where ever you *expect* that an error might occur.

In general, what I do is use On Error Resume Next to continue if an error occurs, then I use
If Err.Number <> 0 Then
   ' an error occurred, handle this, then do the following
   Err.Clear
   On Error GoTo 0
Else
   ' No error occurred, so I usually turn the error checking back off, in case there is
   ' some other error that I am not expecting, which I want to know about
   On Error GoTo 0
End If


So before your Execute statement, if you place On Error Resume Next, then follow my approach for the If statement after that, you should be handle an expected error.  This will allow your program to behave as designed. The reason I use On Error GoTo 0 after those sections, is so that the script will tell you about any unexpected error (and crash out), which I want to know about, because it usually means I have a logic error somewhere.....

You can of course, just use On Error Resume Next at the top, and never use On Error GoTo 0, but you will need to use Err.Clear at expected error locations, to clear the Error status.

Regards,

Rob.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

walshdjAuthor Commented:
Thanks Rob. I understand what you are saying but my issue is that If Err.Number <> 0 Then isnt picking up my error. It simply continues through the program and exit as normal. It is as f the error isnt being flagged as an error. Any ideas?
0
RobSampsonCommented:
Hmmm, under this
      Set objComputerFile = objCommand.Execute

you could put
MsgBox "Err.Number is " & Err.Number

and see whether it is equal to zero, or something else...that might help....

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
walshdjAuthor Commented:
Thanks Rob I've cracked it now thanks to your help.

BIt of a strange one (to me anyway) maybe you can explain. I had been using one generic "on Error resume next" command at the very top of the script to ignore or flag the errors. When I do this it doesnt pick up an error message (i.e if I use your msgbox idea or if I try to echo it out to the console) If I however put the "on Error resume next" command right before the code "expected" to fail it picks it up. I was planning to put it there anyway when I steped through all the bugs so that the code was neat and clean but it was easier to comment in and out the top line while debugging. Is there some reason that this does not work?
0
RobSampsonCommented:
Hi, well that certainly does seem odd.    If an error is raised, the error number should be able to be displayed....perhaps an error was being raised earlier, and the expected code never executed for some reason....I guess you'd need to throw in some more MsgBox's to show output so you can figure out where it gets up to.

It's hard to say without being able to test your code in your environment, but you may be able to nut it out, or just keep the Error statements around the possible problem areas.

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.