wrichards0
wrichards0

Reputation: 187

Classic ASP: Populating a 3D array. Subscript out of range: 'objRS(...)'

I am working on a Classic ASP organisational chart thingy at work. The problem is I get a "Subscript out of range: 'objRS(...)'" error. If index_one of the fullname array equals the company held in index_one of the department array, and index_two of the fullname array equals index_two of department, then this should, in theory, mean the user is in the department so I will echo out the name

I have the following code

 'Define the AD OU that contains our users
 dim ADUser, department, fullname, index_one, index_two, index_three
 fullname           = Array()
 department     = Array()     
 index_one = 0
 index_two = 0
 index_three = 0
 
 ...
 
 ADUser = "LDAP://OU=Staff,OU=Users,DC=example,DC=internal"
 ' Make AD connection and run query
 Set objCon = Server.CreateObject("ADODB.Connection")
 objCon.provider ="ADsDSOObject"
 objCon.Properties("User ID") = "EXAMPLE\user"
 objCon.Properties("Password") = "Pasword"
 objCon.Properties("Encrypt Password") = TRUE
 objCon.open "Active Directory Provider"
 Set objCom = CreateObject("ADODB.Command")
 Set objCom.ActiveConnection = objCon
 objCom.CommandText ="select company, department, givenName, sn, telephoneNumber, mail, title FROM '"& ADUser &"' where company ='*' ORDER BY department ASC"
 Set objRS = objCom.Execute
 ' Loop over returned recordset and output HTML
 Do While Not objRS.EOF Or objRS.BOF
      'If index_one of the fullname array equals the company held in index_one of the department array, and index_two of the fullname array equals index_two of department, then this should, in theory, mean the user is in the department so I will echo out the name
      department(index_one) = objRS("company")
      department(index_two) = objRS("department")
      fullname(index_one) = objRS("company")
      fullname(index_two) = objRS("department")
      fullname(index_three) = objRS("givenName") & " " & objRS("sn")
      index_one = index_one + 1
      index_two = index_two + 1
      index_three = index_three + 1
      objRS.MoveNext
      Response.Flush
 Loop
 ' Clean up
 objRS.Close
 objCon.Close
 Set objRS = Nothing
 Set objCon = Nothing
 Set objCom = Nothing
 

and I get the error:

Microsoft VBScript runtime error '800a0009'

Subscript out of range: 'objRS(...)'

/activedirectory/ldap2.asp, line 38

Line 38 is the last line of a comment so I deleted that, then I got the error when I tried to define each array element so just defined them as:

department() = objRS("company")
department() = objRS("department")

but I still get the error

Upvotes: 0

Views: 79

Answers (1)

Martha
Martha

Reputation: 3854

I don't see you actually assigning dimensions to your arrays anywhere. The Array() function without any arguments will create a zero-dimensional array; obviously, any index will be out of range for that. You'll need a Redim in there somewhere, or if you're adding to an array that already has values, Redim Preserve.

Also, I have no idea what you think this will do:

department() = objRS("company")
department() = objRS("department")

...but in VBScript, you don't ever use empty parentheses like that.

(Aside: you're actually working strictly with one-dimensional arrays. A three-dimensional array would be Dim my3D(10,15,100): that's a cube 10 columns wide, 15 columns deep, and 100 rows long. Given that database tables are 2D arrays at most, you almost never need to work with 3D arrays. In fact, in most cases, 3D arrays are a symptom of overcomplicated thinking, not of good code.)

Upvotes: 1

Related Questions