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

Printing from an Array 3

Status
Not open for further replies.

mohebk

MIS
Aug 23, 2005
139
US
Hi,
I am loading the array like this:

if not objRS.eof then
ItemsArray = objRS.GetRows()
FirstRec = LBound(ItemsArray, 2)
LastRec = UBound(ItemsArray, 2)
FirstField = LBound(ItemsArray, 1)
LastField = UBound(ItemsArray, 1)
End If

***********
And I am trying to print the array to atext file like this:

for I = FirstRec to LastRec
filetxt.WriteLine(ItemsArray(I,5) &" "& ItemsArray(I,4) &","& ItemsArray(I,6) )
Next

***************
The above part is giving me an array out of bound (5) error. When I tried to use numbers for the second subscritp like '0', I got 4 fields printed.

Where did I go wrong?
Thanks in advance.

Moheb
 
I would do this to try to identify tghe problem:

if not objRS.eof then
WScript.Echo "Records = " & objRS.RecordCount
WScript.Echo "Fields = " & objRS.Fields.Count
ItemsArray = objRS.GetRows()
FirstRec = LBound(ItemsArray, 2)
LastRec = UBound(ItemsArray, 2)
FirstField = LBound(ItemsArray, 1)
LastField = UBound(ItemsArray, 1)
WScript.Echo "FirstRec = " & FirstRec
WScript.Echo "LastRec = " & LastRec
WScript.Echo "FirstField = " & FirstField
WScript.Echo "LastField = " & LastField
End If

***********
And I am trying to print the array to atext file like this:

for I = FirstRec to LastRec
filetxt.WriteLine(ItemsArray(I,5) &" "& ItemsArray(I,4) &","& ItemsArray(I,6) )
Next

***************

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
What are the values of firstrec and lastrec?

You need to remember that array's start at 0 and end at ubound(array)

You really don't need to do the extra code, you could re-write your for loop to read:

for i = 0 to ubound(array)

Which line exactly is giving you the error?

ASP, VB, VBS, Cold Fusion, SQL, DTS, T-SQL, PL-SQL, IBM-MQ, Crystal Reports, Crystal Enterprise
 
This is the line that was causing the error:

filetxt.WriteLine(ItemsArray(I,5) &" "& ItemsArray(I,4) &","& ItemsArray(I,6) )


I had problems using WScripts yesterday but let me try this Tom

Moheb
 
You have to understand what is a dimenision in an array.
FirstRec = LBound(ItemsArray, 2)
you ask for the 2nd dimension
ItemsArray(I,5)
your variable indice is for the 1st dimension

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV nails it as usual.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
The way I picture an array is (LBound(ItemsArray, 2) = rows and he variable "I" is referring to the rows. Is that right?

Moheb
 
Use the code that I suggested and you will be able to see exactly which dimension is the records and which is the fields, but for reference, the second dimension is the records and the first is fields.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Is that right?
Reread carefully my previous post.
We're talking about multidimensional array, and thus about dimensions, not rows nor columns.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you all. This has cleared the picture a lot. Now that I understand that, it is all working now.

Thanks as always.


Moheb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top