Solved

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

Posted on 2003-11-19
8
641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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/…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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