Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Subscript out of range error

Posted on 2004-11-05
8
Medium Priority
?
496 Views
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")
                  Else
                        Response.Write("Out of range.<br>")
                  End If
                  Response.Write(rsBirth_Day.Fields("Name") & "<br>")
                  rsBirth_Day.MoveNext
                  r = r + 1
            Loop
            ReDim Preserve arrBirth_Names(intDaysInMonth, r)
            rsBirth_Day.Close
      Next
      
      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>")
            Next
      Next
End Sub

subBuildNameArray 11, 2004

Set rsBirth_Day = Nothing
%>

Here is the output.

30==20
0--0
John Smith
1--0
Ron Davids
3--0
Scott Adams
4--0
Rob Fritz
5--0
Max Howard
6--0
Jeana Regal
8--0
Alex Alvero
10--0
Heidi Chalbers
15--0
Mike Krenzy
15--1

Microsoft VBScript runtime error '800a0009'

Subscript out of range: 'd'

/BillingWebDevl/Resources/NewBirthday.asp, line 115
0
Comment
Question by:nataSofCols
  • 6
  • 2
8 Comments
 
LVL 19

Expert Comment

by:peh803
ID: 12505371
instead of this:

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


You probably want this:

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

Hope this helps!

peh803
0
 

Author Comment

by:nataSofCols
ID: 12505492
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")
0
 
LVL 19

Expert Comment

by:peh803
ID: 12505534
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>"
  next
next

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?
Thanks,
peh803
0
Industry Leaders: 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!

 

Author Comment

by:nataSofCols
ID: 12505757
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.
0
 
LVL 19

Expert Comment

by:peh803
ID: 12505794
small world, eh?
0
 
LVL 19

Expert Comment

by:peh803
ID: 12505797
:-)
0
 
LVL 19

Expert Comment

by:peh803
ID: 12505820
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!

Thanks,
peh803
0
 
LVL 19

Accepted Solution

by:
peh803 earned 500 total points
ID: 12507399
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,
peh803

<%
Dim rs
set rs = server.CreateObject("adodb.recordset")
Dim sLastBDay
sSQL = "SELECT * FROM tbl_OCB_Employee_Dates WHERE birthmonth = '" & month(date()) & "' ORDER BY Birthday"
rs.open 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")
  rs.MoveNext
loop
rs.close
set rs = nothing
%>
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 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