adding more than one array to one UBOUND?

WRATH
10-24-2004, 02:01 AM
im trying to add info from fields to arrays, ill let the code to the talking:



Do Until recordset1.EOF
Dim i As Integer
For i = 0 To UBound(CheckOrTransType()) & (DescriptionOfTrans()) & (PaymentDebit()) & (DepositCredit()) & (TransDate()) & (AccountType()) & (AccountNum()) & (TransactionID()) & (Fee()) & (Balance())
CheckOrTransType(i) = recordset1.Fields.Item("CheckOrTransType").Value
DescriptionOfTrans(i) = recordset1.Fields.Item("DescriptionOfTrans").Value
PaymentDebit(i) = recordset1.Fields.Item("PaymentDebit").Value
DepositCredit(i) = recordset1.Fields.Item("DepositCredit").Value
TransDate(i) = recordset1.Fields.Item("TransDate").Value
AccountType(i) = recordset1.Fields.Item("AccountType").Value
AccountNum(i) = recordset1.Fields.Item("AccountNum").Value
TransactionID(i) = recordset1.Fields.Item("TransactionID").Value
Fee(i) = recordset1.Fields.Item("Fee").Value
Balance(i) = recordset1.Fields.Item("Balance").Value
Next i

recordset1.MoveNext
Loop

database1.Close


this doesnt work either...


For i = 0 To UBound(CheckOrTransType() & DescriptionOfTrans() & PaymentDebit() & DepositCredit() & TransDate() & AccountType() & AccountNum() & TransactionID() & Fee() & Balance())

Dennis DVR
10-24-2004, 03:14 AM
Ubound accepts only one array variable as an argument, why do you need to test all your array in a for loop? perhaps using a type variable would do the trick, or to make it simple, you just need a Do loop and eliminate the for loop, since you are only using single dimensional array you can use the redim preserve keyword inside the do loop, but if you already dimesioned your arrays you only need to loop through the recordset and assigned each field to the appropriate array.


Dim i As Integer 'outside the loop default is 0
Do Until recordset1.EOF
CheckOrTransType(i) = recordset1.Fields.Item("CheckOrTransType").Value
DescriptionOfTrans(i) = recordset1.Fields.Item("DescriptionOfTrans").Value
PaymentDebit(i) = recordset1.Fields.Item("PaymentDebit").Value
DepositCredit(i) = recordset1.Fields.Item("DepositCredit").Value
TransDate(i) = recordset1.Fields.Item("TransDate").Value
AccountType(i) = recordset1.Fields.Item("AccountType").Value
AccountNum(i) = recordset1.Fields.Item("AccountNum").Value
TransactionID(i) = recordset1.Fields.Item("TransactionID").Value
Fee(i) = recordset1.Fields.Item("Fee").Value
Balance(i) = recordset1.Fields.Item("Balance").Value
Recordset1.MoveNext
i = i + 1
Loop

WRATH
10-24-2004, 10:21 PM
thanks a lot!

is the i=i+1 the same as next i?

webbone
10-24-2004, 10:27 PM
thanks a lot!

is the i=i+1 the same as next i?
Sort of - Since Duane suggested a Do..While loop (best for using recordsets since you want to loop until the end of the recordset) the i=i+1 just increments the value being used as an index.

As Duane mentioned, the code he posted assumes you have dimensioned (or ReDim'd) your array to the correct size before getting to this point in the code. If not, you'll need to use a dynamic array and use Redim with the Preserve keyword INSIDE the loop.

WRATH
10-24-2004, 10:54 PM
would you be able to help on this code? i cant figure out how to loop through all of these and add them to the colums in the listview :(


Dim j As Integer
For j = 0 To LBound(CheckOrTransType(), DescriptionOfTrans(), PaymentDebit(), DepositCredit(), TransDate(), AccountType(), AccountNum(), TransactionID(), Fee(), (Balance())
Set itmx = ListView1.ListItems.Add(, , CheckOrTransType(j))
itmx.SubItems(1) = TransDate(j)
itmx.SubItems(2) = DescriptionOfTrans(j)
itmx.SubItems(3) = PaymentDebit(j)
itmx.SubItems(4) = Fee(j)
itmx.SubItems(5) = DepositCredit(j)
itmx.SubItems(6) = Balance(j)
itmx.SubItems(7) = AccountType(j)
itmx.SubItems(8) = AccountNum(j)
itmx.SubItems(8) = TransactionID(j)
Next i


For j = 0 To LBound(CheckOrTransType(), DescriptionOfTrans(), PaymentDebit(), DepositCredit(), TransDate(), AccountType(), AccountNum(), TransactionID(), Fee(), (Balance())


this part of course doesnt work

webbone
10-24-2004, 10:59 PM
Firstly, You are misusing LBound() - See MSDN: http://msdn.microsoft.com/library/en-us/vbenlr98/html/vafctLBound.asp

Secondly you are using LBound() - did you mean to be using UBound()?

Thirdly, in your posted code you have itmx.Subitems(8) used twice - if this is not a typo it is definitely a problem.

WRATH
10-24-2004, 11:07 PM
yeah the compilier hasnt gotten that far yet, if im misusing it then how to do check j to the array value? isnt that lbound? to the hightest?

webbone
10-24-2004, 11:13 PM
yeah the compilier hasnt gotten that far yet, if im misusing it then how to do check j to the array value? isnt that lbound? to the hightest?
Ok, as Duane commented earlier, you can only use LBound() on a SINGLE array dimension - if you check the link I gave you there are more details. So you probably need to do something like:

For j = 0 to UBound(MyArray) 'use UBOUND because you want the whole range


Assuming all those arrays have the SAME Bounds (we still don't know how you are creating them), you would be able to just use the first array in your UBound() function as shown. Also, the above example assumes they are single-dimensional arrays (i.e. MyArray(i) NOT MyArray(i,j)). Again, if you have questions about LBound() or UBound please carefully study the MSDN information on the link I posted for you previously.

WRATH
10-25-2004, 07:28 AM
Sort of - Since Duane suggested a Do..While loop (best for using recordsets since you want to loop until the end of the recordset) the i=i+1 just increments the value being used as an index.

As Duane mentioned, the code he posted assumes you have dimensioned (or ReDim'd) your array to the correct size before getting to this point in the code. If not, you'll need to use a dynamic array and use Redim with the Preserve keyword INSIDE the loop.

what exactly does the redim do?

here is the code, could you link me to some redim reading-material? :) i cant find anything on msdn to save my life...



Option Explicit

Private Sub Form_Load()
Dim itmx As ListItem
Dim colx As ColumnHeader

Set colx = ListView1.ColumnHeaders.Add(, , "CHECK#/TRANS TYPE")
Set colx = ListView1.ColumnHeaders.Add(, , "DATE")
colx.Width = 500
Set colx = ListView1.ColumnHeaders.Add(, , "DESCRIPTION OF TRANS")
colx.Width = 4000
Set colx = ListView1.ColumnHeaders.Add(, , "PAYMENT/DEBIT")
Set colx = ListView1.ColumnHeaders.Add(, , "FEE")
colx.Width = 950
Set colx = ListView1.ColumnHeaders.Add(, , "DEPOSIT/CREDIT")
Set colx = ListView1.ColumnHeaders.Add(, , "AccountType")
Set colx = ListView1.ColumnHeaders.Add(, , "AccountNum")
Set colx = ListView1.ColumnHeaders.Add(, , "TransactionID")
Set colx = ListView1.ColumnHeaders.Add(, , "BALANCE")

ListView1.View = lvwReport

Me.Width = 8535
Me.Height = 4320

Dim Balance() As String
Dim CheckOrTransType() As String
Dim DescriptionOfTrans() As String
Dim PaymentDebit() As Integer
Dim Fee() As Integer
Dim DepositCredit() As Integer
Dim TransDate() As String
Dim AccountType() As String
Dim AccountNum() As String
Dim TransactionID() As String

Dim database1 As ADODB.Connection
Dim recordset1 As ADODB.Recordset

Set database1 = New ADODB.Connection
database1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\FinancesFinalProject.mdb;"
database1.Open

Set recordset1 = database1.Execute("SELECT * FROM history WHERE User = '" & user_logged_in & "'")

'need to make arrays so you can loop through all of the transactions for mwild7 and insert each value into each array then re loop it into the listview for display
Dim i As Integer
Do Until recordset1.EOF
CheckOrTransType(i) = recordset1.Fields.Item("CheckOrTransType").Value
DescriptionOfTrans(i) = recordset1.Fields.Item("DescriptionOfTrans").Value
PaymentDebit(i) = recordset1.Fields.Item("PaymentDebit").Value
DepositCredit(i) = recordset1.Fields.Item("DepositCredit").Value
TransDate(i) = recordset1.Fields.Item("TransDate").Value
AccountType(i) = recordset1.Fields.Item("AccountType").Value
AccountNum(i) = recordset1.Fields.Item("AccountNum").Value
TransactionID(i) = recordset1.Fields.Item("TransactionID").Value
Fee(i) = recordset1.Fields.Item("Fee").Value
Balance(i) = recordset1.Fields.Item("Balance").Value
recordset1.MoveNext
i = i + 1
Loop

database1.Close

Dim j As Integer
For j = 0 To UBound(CheckOrTransType())
Set itmx = ListView1.ListItems.Add(, , CheckOrTransType(j))
itmx.SubItems(1) = TransDate(j)
itmx.SubItems(2) = DescriptionOfTrans(j)
itmx.SubItems(3) = PaymentDebit(j)
itmx.SubItems(4) = Fee(j)
itmx.SubItems(5) = DepositCredit(j)
itmx.SubItems(6) = Balance(j)
itmx.SubItems(7) = AccountType(j)
itmx.SubItems(8) = AccountNum(j)
itmx.SubItems(9) = TransactionID(j)
Next j

ListView1.View = lvwReport


End Sub



where exactly would i use the redim? is that sort of re-initializing the array?
thanks for all of your help!

Dennis DVR
10-25-2004, 08:07 AM
what redim does, is it resize your array, either by making your array bigger or smaller.

Ok to explain further, assuming you have declared array Balance i.e.

Dim Balance() As String 'initially the Array is empty and using the ubound will produce an error.
'now use the redim keyword to resize the array
Redim Balance(0) 'now using the Ubound function will return 0
Msgbox Ubound(Balance) '-> will return 0
Redim Balance(5) 'now
Msgbox Ubound(Balance) '-> will return 5
Redim Balance(3)
Msgbox Ubound(Balance) '-> will return 3


but using a redim keyword without the preserve keyword will erase all the data in the array.

WRATH
10-25-2004, 05:32 PM
so it doesnt default to a value of zero when you make an array?

im getting an invalid use of NULL error now... here is how i declared the arrays


Dim Balance() As String
ReDim Balance(0)
Dim CheckOrTransType() As String
ReDim CheckOrTransType(0)
Dim DescriptionOfTrans() As String
ReDim DescriptionOfTrans(0)
Dim PaymentDebit() As Integer
ReDim PaymentDebit(0)
Dim Fee() As Integer
ReDim Fee(0)
Dim DepositCredit() As Integer
ReDim DepositCredit(0)
Dim TransDate() As String
ReDim TransDate(0)
Dim AccountType() As String
ReDim AccountType(0)
Dim AccountNum() As String
ReDim AccountNum(0)
Dim TransactionID() As String
ReDim TransactionID(0)

webbone
10-25-2004, 05:51 PM
Two things:

1) You will need to add to your Do..While loop a set of ReDim statements similiar to this:

Do While....

'your stuff...


i = i + 1

ReDim Preserve CheckOrTransType(i) 'increase your array size and KEEP previous values
'and so on for ALL of your arrays - you need a ReDim statement like the above for each one

Loop


2) Your NULL problem is probably something in your data from the database - if you have a field that was never filled in it will have a value of NULL - your database understands this but you cannot assign NULL to, for example, a string. There are a variety of ways to deal with this, and many posts on the forum regarding this topic. Check this one and I think you'll understand: http://www.visualbasicforum.com/showthread.php?t=186033&highlight=Null

WRATH
10-26-2004, 10:06 AM
ok so ill check each field when adding them to the array and if its null then just add a 0 to the array?

and do you have a link on the redim and preserve key words? im totally lost as to where to put them and why still.. all though you guys have done an awesome job helping me!!

thanks

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum