Solved

Trap Error Subscript out of range: '[number: 3]'

Posted on 2003-11-19
8
628 Views
Last Modified: 2007-11-27
Hi guys,
I have this code
Function SplitArray( strData )

          Dim arr
          Dim dict
          Dim nCounter

          Set dict = CreateObject("Scripting.Dictionary")
          arr = Split( strData, "/" )
          For nCounter = 0 To UBound(arr) Step 2


          dict(arr(nCounter)) = arr(nCounter+1)
           

Next

          Set SplitArray = dict

     End Function
this will call it
Set objValues = SplitArray( StrKey )

how can i trap the error if StrKey becomes an Odd number ?

Thanx
0
Comment
Question by:Sammy
8 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9782932
ainapure:

> xp_sendmail will work only if the server has MS Exchange installed on the
> machine that hosts the database server.

That is simply not true.  Any valid MAPI client will work just fine.

> Another way is to write a normal asp page that queries the information from
> database and sends out the mail. You can then schedule this asp page through
> a .vbs file which can be scheduled using the OS's task scheduler.

Even more simple would be to use the SQL Server's schedule mechanism.  It can run any program, script, or stored procedure.

In any event, there are several ways to schedule a task, and several ways to send database results via e-mail.  You just have to mix and match the options that help you the most.

Hope That Helps,
Dex*
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9782937
Sorry, wrong question.
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 80 total points
ID: 9782998
sammy1971:

> how can i trap the error if StrKey becomes an Odd number ?

Where did you get that function?  I'm pretty sure I wrote it...  :)
Here is a version that handles the situation you mentioned.  It will simply just ignore the extra value.

      Function SplitArray( strData )
            Dim arr
            Dim dict
            Dim nCounter

            Set dict = CreateObject("Scripting.Dictionary")
            arr = Split( strData, "/" )
            For nCounter = 0 To UBound(arr) Step 2
                  If ( nCounter < UBound(arr) ) Then
                        dict(arr(nCounter)) = arr(nCounter+1)
                  End If
            Next

            Set SplitArray = dict
      End Function

Hope That Helps,
Dex*
0
 
LVL 4

Expert Comment

by:ainapure
ID: 9783067
sammy,

there is a function called Mod in vbscript that returns 0 if number is even or 1 if the number is odd. Here is a small modification to your code

******modified code*********
Function SplitArray( strData )

          Dim arr
          Dim dict
          Dim nCounter

          Set dict = CreateObject("Scripting.Dictionary")
          arr = Split( strData, "/" )
          For nCounter = 0 To UBound(arr) Step 2


          dict(arr(nCounter)) = arr(nCounter+1)
           

Next

          Set SplitArray = dict

     End Function
check=StryKey Mod 2
if check=0 then
Set objValues = SplitArray( StrKey )
else
'Write some message
end if
**********end code**********************

hope this helps
-amit
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 6

Expert Comment

by:Mike_Metro
ID: 9783105
       Set dict = CreateObject("Scripting.Dictionary")
        arr = Split( strData, "/" )
        For nCounter = 0 To UBound(arr) Step 2
            dict(arr(nCounter)) = arr(nCounter+1)

            If Ubound(arr) = nCounter + 2 Then  
               dict(arr(nCounter+2)) = ""
               Exit For
        Next
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9783115
Amit:

strKey is a string, not a numeric value, so you can't MOD on it.  The Asker wanted to know how to trap the error when strKey has an odd number of sections in it.

(I only recognized that because I wrote the original function for some other question...)

Dex*
0
 
LVL 27

Author Comment

by:Sammy
ID: 9783301
i guess i was over excited
now this

----
Function SplitArray( strData )
          Dim arr
          Dim dict
          Dim nCounter

          Set dict = CreateObject("Scripting.Dictionary")
          arr = Split( strData, "/" )
          For nCounter = 0 To UBound(arr) Step 2

               If ( nCounter < UBound(arr) ) Then
response.write "ODD"
else
                    dict(arr(nCounter)) = arr(nCounter+1)
response.write "Even"
               End If
          Next

          Set SplitArray = dict
     End Function

will always write ODD !!

0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9783377
sammy:

Yeah, that's what it would do.  I think you want something more like this:

     Function SplitArray( strData )
          Dim arr
          Dim dict
          Dim nCounter

          Set dict = CreateObject("Scripting.Dictionary")
          arr = Split( strData, "/" )
          If ( ((UBound(arr)-LBound(arr)) MOD 2) = 0 ) Then
                    Response.Write "Even"
          Else
                    Response.Write "Odd"
          End If
         
          For nCounter = 0 To UBound(arr) Step 2
               If ( nCounter < UBound(arr) ) Then
                    dict(arr(nCounter)) = arr(nCounter+1)
               End If
          Next

          Set SplitArray = dict
     End Function

How's that?
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now