Subscript out of range error

Posted on 2004-11-05
Last Modified: 2012-06-27
I'm having trouble tracking down why this error message is popping up.  I'm probably doing something simple and stupid but I've been stairing at it too long.  Below is the code and the results I get from it.

Sub subBuildNameArray(intMonth, intYear)

      Dim intDaysInMonth, arrBirth_Names(), d, r
      intDaysInMonth = funcDaysInMonth(intMonth, intYear)
      ReDim arrBirth_Names(intDaysInMonth, 20)
      Response.Write(UBound(arrBirth_Names) & "==" & UBound(arrBirth_Names, 2) & "<br>")
      For d = 0 To intDaysInMonth - 1
            sqlBirth_Day = "SELECT Name FROM tbl_OCB_Employee_Dates WHERE Birth_Month = " & intMonth &_
                  " And Birth_Day = " & d + 1
            rsBirth_Day.Open sqlBirth_Day, conn
            r = 0
            Do Until rsBirth_Day.EOF
                  Response.Write(d & "--" & r & "<br>")
                  If d < UBound(arrBirth_Names) Then
                        arrBirth_Names(d, r) = rsBirth_Day.Fields("Name")
                        Response.Write("Out of range.<br>")
                  End If
                  Response.Write(rsBirth_Day.Fields("Name") & "<br>")
                  r = r + 1
            ReDim Preserve arrBirth_Names(intDaysInMonth, r)
      For d = 0 To intDaysInMonth - 1
            Response.Write(Ubound(arrBirth_Names, 2))
            For r = 0 To UBound(arrBirth_Names, 2)
                  Response.Write(arrBirth_Names(d, r) & "<br>")
End Sub

subBuildNameArray 11, 2004

Set rsBirth_Day = Nothing

Here is the output.

John Smith
Ron Davids
Scott Adams
Rob Fritz
Max Howard
Jeana Regal
Alex Alvero
Heidi Chalbers
Mike Krenzy

Microsoft VBScript runtime error '800a0009'

Subscript out of range: 'd'

/BillingWebDevl/Resources/NewBirthday.asp, line 115
Question by:nataSofCols
    LVL 19

    Expert Comment

    instead of this:

    Response.Write(arrBirth_Names(d, r) & "<br>")

    You probably want this:

    Response.Write(arrBirth_Names(r,d) & "<br>")

    Hope this helps!


    Author Comment

    I'm sorry, I wasn't specific enough.  I pulled this code from a large set, so the line number is not accurate.  The line that is causing the problem is: arrBirth_Names(d, r) = rsBirth_Day.Fields("Name")
    LVL 19

    Expert Comment

    Okay, let me back up a bit.. What are you trying to do here?  I ask because I've reviewed your code further and I'm not sure my solution will help you out because you're doing some unconventional things with arrays, loops, etc.

    Typically, users will loop through arrays like this:

    for i=0 to ubound(arr,2)
      for o=0 to ubound(arr,1)
        response.write arr(o,i)&"<BR>"

    Notice how the second dimension of the array is typically the outer-most loop (corresponding to the number of "rows" in a 2d array), and the first dimension is the inner-most loop (corresponding to the number of "columns" in a 2d array).  You seem to be doing it a bit differently, which may be why you're having trouble.  Generally, I think of 2d arrays like this:

    0,0   1,0   2,0
    0,1   1,1   2,1
    0,2   1,2   2,2
    0,3   1,3   2,3
    0,4   1,4   2,4
    0,5   1,5   2,5
    0,6   1,6   2,6

    So, the array above contains 7 columns (0-->6) and 3 rows (0-->2), and ubound(aboveArray,1) = 2, and ubound(aboveArray,2)=6.  Indeed, if you fetch a recordset's contents into an array, this is the kind of structure you find.  

    Does this clear things up at all?

    Author Comment

    OK, I'm not sure how well that would apply to my situation.  Basically, I have a db table that contains a name, a birth month, and a birth day.  The purpose of all of this is to build a calendar with the appropriate names on the appropriate days.  In this section of code, I'm going through the db table, pulling all names from the particular month and day and then incrementing the day.  The reason for the inner loop is for the case where there are multiple people with the same birth day, so the second loop is not always being used.  This is the reason I'm using a do loop instead of a for loop.  Thanks for working with me on this.
    LVL 19

    Expert Comment

    small world, eh?
    LVL 19

    Expert Comment

    LVL 19

    Expert Comment

    actually, fyi, I've gotta run to an 11:00 meeting (which I'm late to, obviously), so if you could bear with me for a bit, that'd be great...I'll get to it asap!

    LVL 19

    Accepted Solution

    you shouldn't need to mess around with arrays to accomplish this; please take a look at the following code, which will loop through your recordset / table structure and display all names of people with birthdays, grouped by date.  I can continue to help further if you have any other questions.

    Thank you,

    Dim rs
    set rs = server.CreateObject("adodb.recordset")
    Dim sLastBDay
    sSQL = "SELECT * FROM tbl_OCB_Employee_Dates WHERE birthmonth = '" & month(date()) & "' ORDER BY Birthday" sSQL, connection, 1, 3, 1

    Response.Write "Here are the birthdays for the month of <b>" & monthName(month(date())) & "</b>:<BR>"
    sLastBDay = ""
    do while not rs.EOF
      if sLastBDay <> rs("birthday") then
        Response.Write "<B><u>"&rs("birthday")&"</u></B><BR>"
      end if
      Response.Write "Person: "&rs("name")&"<BR>"
      Response.Write "Birthday: "&rs("birthday")&"<BR>"
      sLastBDay = rs("birthday")
    set rs = nothing

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    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…
    I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    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…

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now