Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 13th, 2006, 8:22 PM   #1
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
Exclamation 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
paulchwd is offline   Reply With Quote
Old Jan 13th, 2006, 11:50 PM   #2
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 355
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
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.
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jan 14th, 2006, 12:31 AM   #3
sintaks
Newbie
 
Join Date: Jan 2006
Location: Programville
Posts: 4
Rep Power: 0 sintaks is on a distinguished road
Send a message via Yahoo to sintaks
Yuck... even when doing a for...next loop, you still have to increment "i"? That's a messy language indeed ... 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
sintaks is offline   Reply With Quote
Old Jan 14th, 2006, 12:50 AM   #4
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 729
Rep Power: 4 Dameon is on a distinguished road
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.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Jan 14th, 2006, 1:41 AM   #5
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 355
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Damn .. i'm not giving advice on VB ever again :o
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jan 14th, 2006, 9:42 AM   #6
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
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???
paulchwd is offline   Reply With Quote
Old Jan 14th, 2006, 10:04 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,435
Rep Power: 8 Ooble is on a distinguished road
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)
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 14th, 2006, 10:56 AM   #8
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
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)
paulchwd is offline   Reply With Quote
Old Jan 15th, 2006, 7:59 AM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,435
Rep Power: 8 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 15th, 2006, 6:52 PM   #10
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
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 ?????
paulchwd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:55 PM.

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