Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   ASP (http://www.programmingforums.org/forum32.html)
-   -   Weird result with Arrays ---> please help (http://www.programmingforums.org/showthread.php?t=7943)

paulchwd Jan 13th, 2006 8:22 PM

Weird result with Arrays ---> please help
 
Hello all.

Simple concept here..i queryed my database (Access) , stored the resultes in a recordset, as usual

Then i created a loop to put the contents of the recordset into an arry, but it only put one element in it. ..code below

:

dim rArray(9)

sql = "select rate from mort"

set recset = adocon.execute(sql)

do while (not recset.eof)

  for i=0 to recset.fields.count-1

  rArray(i)= recset(i)


  next

recset.moveNext

loop

response.write rArray(0)


only the final element of the recordset is in the array

did i set up the array right ?

thanks

xavier Jan 13th, 2006 11:50 PM

you forgot to increment i :)

i++; After rArray(i)= recset(i);

And, i don't think it's recset(i), ....... probably something like recset("mort"). But i may be way off here.

sintaks Jan 14th, 2006 12:31 AM

Yuck... even when doing a for...next loop, you still have to increment "i"? That's a messy language indeed :P... I thought the incrementation of "i" was taken care of by the "next", as it was from my qb days (for i = 1 to 10 ... next i)

That aside... If you're only dimming the array to have 9 elts, yet you're looping through all of the record sets, wouldn't you go out-of-bounds eventually? It's unrelated, but funky stuff happens when you mess with memory.

Then again, I don't know VB, so you can pretty much disregard everything I just wrote :P

Dameon Jan 14th, 2006 12:50 AM

:

dim rArray(9) <-- Assumes that there are always 9 fields

sql = "select rate from mort"

set recset = adocon.execute(sql)

do while (not recset.eof)

  for i=0 to recset.fields.count-1

  rArray(i)= recset(i) <-- Sets rArray(i) to recset(i). This happens, for 0 to recset.fields.count -1, for each record. In other words, rArray in the end contains only the data from the last recordset


  next <-- Yes, i is incremented...handle the next of the fields

recset.moveNext

loop <-- handle the next record

response.write rArray(0) <-- The first field of the last record. See above.


You are copying the data from one array to another for each record and only returning the copied data after it has been overwritten several times.

xavier Jan 14th, 2006 1:41 AM

Damn .. i'm not giving advice on VB ever again :o

paulchwd Jan 14th, 2006 9:42 AM

Why is it overiding the previous element in the rArray ? my logic was:

when i is 0 then rArray(0)=recset(0)
when i is 1 then rArray(1)=recset(1)
when i is 2then rArray(2)=recset(2)
etc...
how can i fix it so that i have an array with all the elements of the recordset???

Ooble Jan 14th, 2006 10:04 AM

Use a two-dimensional array:
:

Dim RecSet()

' find out how many fields and records there are
Dim RecordCount As Integer = abc
Dim FieldCount As Integer = xyz
Redim RecSet(abc, xyz)


paulchwd Jan 14th, 2006 10:56 AM

Why would it not work with a 1d array?

The purpose for the array with the contents of the recordset is... i had 7 fields (entry) in the database of numbers (mortgage rates) and i have a form where i input the new ones, but to do an sql update statement you have to specify what the current data is....ie: UPDATE mort SET rate1 = ' " & newRate &" ' WHERE rate1= oldValue...> thats the problem ...the old value i have to query the database to get that old value but there's 7 of them so..i figured i'd use an array (see my code above)

Ooble Jan 15th, 2006 7:59 AM

Quote:

Originally Posted by paulchwd
Why would it not work with a 1d array?

You have two-dimensional data. Technically, a 2D array (x, y) is simply a 1D array of (x * y), but easier to use in these circumstances.

:

        | FieldA | FieldB | FieldC
--------+--------+--------+--------
Record1 |  1A  |  1B  |  1C
        |        |        |
Record2 |  2A  |  2B  |  3C
        |        |        |
Record3 |  3A  |  3B  |  3C
        |        |        |

Looks 2D to me.

paulchwd Jan 15th, 2006 6:52 PM

Thanks for all your help...but i think you misunderstood what i need...

I have to query my database for one type of information---> mortgage rates...no other info is needed to be in this array, as i need to use it in an update statement.

If i want to update a field in the database (i will be updating all at once) i have to say change this value which you get from my form to this valu e (the one already in the database) ....

thats where the problem is i wanted to do it all in 1 statement , thus i figured i needed an array with the data ...so...each field in my array has a mortgage rate in it

how can i make a loop to copy the info from my recordset into an array ?????


All times are GMT -5. The time now is 9:33 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC