Subscript out of range:  Error

Posted on 2006-04-22
Last Modified: 2012-08-13
Hi, I am retreiving values from a database to display them on a page as below

sql = "select genres, commision, bio, longbio, workinprogress, publication, showHidePrefs,otherworkshops from tblProfile where userid = " & Session("userID")
set rs = conn.execute(sql)

setting my array as below
if rs("showHidePrefs") <> "" then
      arrSH = Split(rs("showhideprefs"), ",")
      dim arrSH()
      redim arrSH(10)

      arrSH(0) = "0"
      arrSH(1) = "0"
      arrSH(2) = "0"
      arrSH(3) = "0"
      arrSH(4) = "0"
      arrSH(5) = "0"
      arrSH(6) = "0"
      arrSH(7) = "0"
end if

it is in the below part of the page where i get the error
r1 = ""
r2 = ""
if arrSH(5) = 1 then
 r1 = "checked"
 r2 = "checked"
end if

i am getting a "Subscript out of range: '[number: 5]" at the "ELSE" line
please do suggest how can I solve this error

Question by:Mona020200
    LVL 18

    Expert Comment

    I usually check for a recordset value like this (there are several ways to do this):

    if not(IsNull(rs("showhideprefs")) or rs("showhideprefs") = "") then

    Another thing to do may be to make sure you are getting records back in your recordset before you check the value of a specific field...

    if not(rs.EOF and rs.BOF) then   'you have records being returned...
       'code goes here...
       response.write "No records returned!"
    end if
    LVL 25

    Accepted Solution


    Your showHidePrefs field for that user doesn't contain at least 6 commas.  You could use redim preserve to set your array length to 10 no matter what:

    if rs("showHidePrefs") <> "" then
         arrSH = Split(rs("showhideprefs"), ",")
         redim preserve arrSH(10)
    else ...
    LVL 11

    Expert Comment

    try this

    for i = 0 to ubound(arrSH)

    if arrSH(i) = 1 then
     execute("r"&i+1&" = 'checked'")
     execute("r"&i+2&" = 'checked'")
    end if


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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: and…
    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…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    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