Fixing Microsoft VBScript Runtime Error '800a0009': Subscript Out of Range '[number: 1]'

SummaryI had been preparing for graduate entrance exams since the start of the year and had not updated the blog in a long time. While helping my school maintain a server during the holiday, I encountered an interesting problem. After migrating the server data and building the environment, the school website displayed an error on its home page. I opened the file and found the failing line: '&dmonth&'-'&dday&' | '&rs("dtitle")&' …

I had been preparing for graduate entrance exams since the start of the year and had not updated the blog in a long time. While helping my school maintain a server during the holiday, I encountered an interesting problem worth recording.

Background

After migrating the server data, building the environment, and starting the site, this error appeared on the school website's home page. 1.png

Open the file and locate the failing line:

<%
	sql="select top 10 d_id,d_title,class_id,d_time,is_url,url from a_detail where class_id = 12 or class_id = 18 or class_id = 19 order by d_id desc"
	set rs=server.createobject("adodb.recordset")
	rs.open sql,conn,1,1
	if not rs.eof and not rs.bof then
	Do While Not rs.eof
		dim class_id,d_id,d_time,d_year,d_month,d_day
		class_id = rs("class_id")
		d_id = rs("d_id")
		d_time_text = CStr(rs("d_time"))
		d_time_arr = Split(d_time_text,"-")
		d_year = d_time_arr(0)

		if d_time_arr(1) <= 9 then   // the problem line
			d_month = "0"&d_time_arr(1)
		else
			d_month = d_time_arr(1)
		end if

		if d_time_arr(2) <= 9 then
			d_day = "0"&d_time_arr(2)
		else
			d_day = d_time_arr(2)
		end if

		if rs("is_url") = true then
			Response.Write("<tr><td><img src='pic/row.png' align='absmiddle'><font class='NewsTitledy'>"&d_month&"-"&d_day&"</font>&nbsp;|&nbsp;<a class='NewsTitledy' href='"&rs("url")&"' target='_blank'>"&rs("d_title")&"</a><td></tr>")
		else
			Response.Write("<tr><td><img src='pic/row.png' align='absmiddle'><font class='NewsTitledy'>"&d_month&"-"&d_day&"</font>&nbsp;|&nbsp;<a class='NewsTitledy' href='page/page/"&class_id&"/"&d_year&"-"&d_month&"-"&d_day&"-"&d_id&".html' target='_blank'>"&rs("d_title")&"</a><td></tr>")
		end if
		rs.movenext
	Loop
	end if
	rs.close
%>

The code is straightforward: it reads fields from the top table, obtains each value, and combines them into a directory of links. Nothing looks wrong. The file was copied directly from the original server, where it worked normally. So what changed in the new environment?

Finding the Cause

The message indicated an array index error. I tried changing d_time_arr(1) <= 9 to d_time_arr(0) <= 9:

2.png

Reading that alongside the code made the cause immediately clear.

Consider this line: d_time_arr = Split(d_time_text,"-") The split() method divides a string into an array. Here it splits on a hyphen, but the server's date format is yyyy/M/d.

Solution

Once the cause is known, the fix is simple. The first option is to change the server's date format to yyyy-M-d:

** Choose Run, then enter regedit.

** Navigate to HKEY_USERS.DEFAULT\Control Panel\International.

** Find sDate and sShortDate on the right, and change '/' to '-'.

** Restart IIS.

The simpler second option is to change the code from d_time_arr = Split(d_time_text,"-") to d_time_arr = Split(d_time_text,"/"). This successfully resolves Microsoft VBScript runtime error '800a0009': subscript out of range '[number: 1]'.