Guy’s Scripting Ezine – 96 Challenge to Fix VBScript Errors

Contents for Ezine 96 – Challenge to Fix VBScript Errors


This Week’s Secret

Readers send me all sorts of scripts.  I cannot help but notice that there are numerous ways to achieve the same goal.  One of the benefits of seeing lots of scripts is that sometimes other people’s routines show me a neater way to perform a task or a new function to streamline a job.

This week I have three challenges for you.  If you accept my challenges to fix the deliberate errors in the scripts, then my first piece of advice is to start with the thought that there is more than one way to correct my problem code.

This Week’s Mission

This week I am in end of year mode.  What I have is three scripts which don’t achieve their purpose and I invite you to solve the errors and thus get the scripts working as designed.  Even though this week’s examples have an end of term feeling, there are still valuable learning points in the scripts.  For instance, error correcting code features strongly in the first two examples.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

1) Multiply Two Numbers to Produce the Correct Answer

Purpose of the Script

Here is a very simple VBScript, which is designed to multiply two numbers. This script generates two input boxes.  Each box collects one number, the script should then multiply the two numbers and should calculate the correct answer.  However, something is amiss and each answer is wrong.  How can you troubleshoot the code to make sure it always displays the correct maths?


‘ InputNumber.vbs
‘ VBScript Multiply two numbers
‘ Author Guy Thomas
‘ Version 1.2 – December 2005
‘ —————————————————————–‘
Option Explicit
Dim strInput, strFirst, strSecond, strProduct, intFirst, intSecond

‘ Input Boxes to get the values of the two numbers
strFirst = (InputBox(" First Number", "Enter First Number"))
    intFirst = IsNumeric(strFirst)
    If intFirst = True Then
    strInput = True
    Else WScript.Echo "Enter a number not a letter"
End if
Loop until strInput = True
strInput = False
Do While strInput = False
strSecond = (InputBox(" Second Number", "Enter Second Number"))
    intSecond = IsNumeric(strSecond)
    If intSecond = True Then
    strInput = True
    Else WScript.Echo "Enter a number not a letter"
End if

‘ Calculation
strProduct = strFirst * strSecond + int((rnd * 7)+1)
WScript.Echo strFirst & " multiplied by " & strSecond _
& " = " & strProduct

‘ End of Example VBScript.

VBScript Tutorial – Learning Points

1) This script provides a refresher on the syntax of InputBox.  Observe how the variable strFirst is set to the value entered in the box.  Also note how inputbox() has two extra arguments to guide users in entering information correctly.

2) IsNumeric provides error correcting code.  The job of this function is to make sure that the user enters a number and not a letter.

3) I am very fond of code that repeats instructions, for this example I demonstrate two variations of looping; a) Do …. Loop Until, b) Do While…. Loop.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

2) Map Network Drive

The purpose of this script is to map a network share to a suitable drive letter.  However, even if you substitute a valid UNC path on your network for \\ grand \home, the script still fails.  Your task is to make a simple correction to one line in the script and so get this logon script working correctly.


‘ MapNetworkDrive.vbs
‘ VBScript to map a network drive to a UNC Path.
‘ Author Guy Thomas
‘ Version 1.2 – December 2005
‘ ————————————————————‘
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath
strDriveLetter = "C:"
strRemotePath = "\\grand\home"

‘ Purpose of script to create a network object. (objNetwork)
‘ Then to apply the MapNetworkDrive method. Result J: drive
Set objNetwork = CreateObject("WScript.Network")

On Error Resume Next
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
If err.number = -2147024811 then
WScript.Echo "Guy’s problem is set – Drive letter in use"
     Else If err.number = -2147023693 then
     WScript.Echo "Check UNC Path"
     Else If err.number = 0 then
     WScript.Echo "Successfully mapped " & strDriveLetter
     Else Wscript.Echo err.number
     End If
     End If
End If
‘ Purely for testing I added code to remove the mapped drive
‘ Comment out, or delete the next line in a production script
objNetwork.RemoveNetworkDrive strDriveLetter

‘ End of Example VBScript.

VBScript Tutorial – Learning Points

1) I introduced error correcting code half way through this example.  The extra lines have two opposite effects, they mask the problem, yet give you extra clues to solve the problem.  As part of my hidden agenda, the error correcting code shows you how to anticipate problems and add code to deal with ‘exceptions’.

2) The decimal numbers -2147024811 and -2147023693 are the equivalent of the Hex numbers that you see in the code 080070055 error messages.  You see these 0800xxxxx message boxes if you run the script without the On Error Resume Next statement.

3) Weekday with VBScript

The purpose of this script is simply to display the day of the week = E.g. Saturday.  As you may see, the code is in a terrible mess.   You could be ruthless and reduce the whole script to a few lines.  In which case, don’t be shy of researching VBScript and weekdays in Google. 

Alternatively, you could take the script at face value and cure each fault methodically.  Should you need clues, seek and remove the three sets of comments (‘) in the lines that follow Option Explicit.


‘ Weekday.vbs
‘ VBScript to determine the day of the week
‘ Author Guy Thomas
‘ Version 2.3 – December 2005
‘ —————————————————————‘
Option Explicit
Dim DayWeek, DayWeek2 ‘ , strDay

DayWeek = Weekday(date(),0)
‘DayWeek2 = WeekdayName(DayWeek)
‘DayWeek2 = WeekdayName(Weekday(date(),1))

Select Case DayWeek
Case 1 strDay = "Sunny day"
Case 2 strDay = "Mondale"
Case 3 strDay = "Toes day"
Case 4 strDay = "Wedding day"
Case 5 strDay = "Thirsty day"
Case 6 strDay = "Frying day"
Case 7 strDay = "Saturn day"
Case Else strDay = "Something wrong"
End Select

Wscript.Echo DayWeek & " = " & strDay & " " & DayWeek2

‘ End of Example VBScript.

Learning Points

1) The first problem is to remember to declare all your variables.  After all, the point of Option Explicit is to prevent typing errors later in the script.

2) Date() is a built in VBScript function.  It is possible to employ Date() to extract a numeric representation of the day of the week, for example Saturday = 7.   The underlying problem is whether to begin the week at Sunday or Monday.  Research indicates that you control this factor with date(),0 or ,date(),1.

3) While I love Select Case, I have deliberately used the command inappropriately; firstly, by misspelling the days of the week, secondly, it is possible to simplify the whole scriplet to one command:
DayWeek2 = WeekdayName(Weekday(date(),1)).

See more about VBScript error correcting codes

VBScripts  • 800 Code list  • Ezines  • Logon Scripts   • PowerShell ErrorAction  • VM Monitor

Ezine 72 Error correcting  • Ezine 107 Error correcting  • Tool Kit   • WMI Monitor Review

Ezine 96 Errors  • Ezine 117 Troubleshooting pure  • Ezine 130 VBScript codes