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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Array 1

Status
Not open for further replies.

Gatorajc

MIS
Mar 1, 2002
423
US
I am trying to set a dynamic array with the rows retrieved from a table.

I tried this

sqlString = "SELECT Count(Distinct Date) total"
sqlString = sqlString & " From ProjectNotes"

SET RS = Conn3.Execute(sqlString)

' Used to keep track of Array placement.

total = RS("total")
dim timearray((total))

I get the error

Microsoft VBScript compilation error '800a0402'

Expected integer constant

/HTMLpages/Projects/searches/month_metrics.asp, line 35

dim timearray("total")
--------------^

Is it possible to do this and if not how can I do it another way.

[pc]
 
why don't you try writing the total variable to the page to see what it's giving you? You may then have a better idea of what's going on...

total = RS("total")

Response.write(total)

dim timearray((total))
-Ovatvvon :-Q
 
I did that already and I get the correct number I want but it does not work.

I also tried this but it does not work

dim timearray(RS("total")

should it be working?
 
Also, I may be wrong, but to assign "total" as the value you want...don't you have to use "as" in the statement?

SELECT Count(Distinct Date) as total

I may be wrong there...but that seems like it's right.

-Ovatvvon :-Q
 
No as is not needed. Because I was able to write the total to the page. I also wrote the query in an analyzer and it worked as well.
 
You can't use AS in VBscript.
Now to the problem.

dim timearray((total))
What are those extra parentheses for. AND ...
Dim needs a constant BUT...

REDIM does not. So ...

dim timearray()
.......
ReDim timearray(total)



Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
By the way,
ReDim timearray(total)
gives you one more element than you need IF you start at element 0. If you want only as many elements as are in total then
If total > 0 then
ReDim timearray(total-1)
End if Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks to you both.

John actually after some research I got it to work with the Redim, before I read you post but you did give me some more information.

Sometimes the best thing is to step away from a problem and come back to it then to fight it.
 
Heh, you're right...Sorry. Did that off the top of my head at work without checking resources cause I didn't have time.

Good you got it working though. -Ovatvvon :-Q
 
I have two questions:
[ol][li]I use the as in my select statement and it works. In fact it didn't work without it. Is that my bad or should I not be concerned?
[/li]
[li]I am trying to create a dynamic 2 dimensional array. What would need to be different? Could I still just go
Code:
Dim arMYARRAY()
...
Select statement
...
Redim arMYARRAY(RS("users"),RS("records"))
Would that work?[/li][/ol]
Thanks in advance, Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
No, you don't have anything to worry about. The only time you should worry is if somthing doesn't work....then you need to find out if it's not supposed to, or if you did somthin wrong. ;-)

The AS should work with a sql statement through ADO...however will not work in the ASP page in VBScript as it would with Visual Basic or in ASP.NET. But I believe you can choose to use the AS or not in ADO as with these pages.

If rs("users") is the number of total users and rs("records") is the number of records per user, then yes, that is how you would define the multi-dimentional array. You can get a sample of it at

Hope this helps.
-Ovatvvon :-Q
 
Einstein
1. My mis-reading. I meant that you can not use AS in a declaration in VBScript as in (pun intended)
Dim dim timearray() AS String

2. Redim arMYARRAY(RS("users"),RS("records"))
is OK but... rememmber that arrays are 0-based so
Redim arMYARRAY(1, 1)
will give you 4 elements instead of 2. All will be well if you never use elements (0,RS("records")) Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thank you John,

I was a C programmer for years, and that zero-th element bites me sometimes. Now using VBscript I think that I will make more mistakes when I go back to C :)

In the page I'm doing - I will use the zero-th elements. Always good to know if they are there or not. Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top