Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with array

Status
Not open for further replies.

Quasibobo

Programmer
Oct 11, 2001
168
NL
Hi,

I've read the manual, but this won't work! I do not understand why. The array needs to be as big as the RecordCount of the sql-result.

Code:
Set Projecten_query = Server.CreateObject( "ADODB.Recordset" )
Projecten_query.CursorType = adOpenStatic
Projecten_query.ActiveConnection = Con
Projecten_query.Open = "SELECT ID FROM project WHERE DivisieID = " & DivisieID & " AND Niveau = 0 ORDER BY ID DESC"
aantal=Projecten_query.RecordCount

' Create Array with all the Projects with that DivisionID.
Dim myArray(aantal)  'first problem
myArray(0) = "0"   'second problem

FOR i=1 TO aantal

	ProjectIDS = Projecten_query("ID")
	myArray(i)= ProjectIDS

NEXT

I get these errors:
Integer expected (first problem)
Subscript out of range (second problem)

So.... what to do to make the server understand that 'aantal' is an integer? Dim myArray(cInt(aantal)) isn't working... and why doesn't the array accept the value of the first array?

Thanks in advance!

Quasibobo

Don't eat yellow snow!
 
Some recordset.count return -1 as the number of records. It depends on the type of cursor being used

Dim myArray() leaves the size blank....otherwise you set it to 0


Bastien

Cat, the other other white meat
 
Hello Quasibobo,

Further to the notes of bastienk, you should know for however well-defined and well-behaved "variable" aantal, constructions like this will always give you a runtime:
i=10
dim a(i) '[red][_wrong_][/red]
You have to do it like this in this order:
i=10
dim a()
redim a(i)
or, if you want to preserve existing value assigned:
i=10
dim a()
'other operations, if exist
redim preserve a(i)

regards - tsuji
 
On another note, why not just use the built in GetRows method of the recordset object?
GetRows will convert all of the records into a two dimensional array for you:
Code:
Projecten_query.Open = "SELECT ID FROM project WHERE DivisieID = " & DivisieID & " AND Niveau = 0 ORDER BY ID DESC"

myArray = Projecten_query.GetRows

'first index is field, second index is record
'   so in this case we could loop through all the records like so
For i = 0 to UBound(myArray,2)
   Response.Write "ID #" & i & " = " & myArray(0,i) & "<br>"
Next

-T

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
Thanks.... I'll have a go with that!

Quasibobo

Don't eat yellow snow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top