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

Displaying the values in an array

Status
Not open for further replies.

GJP55

Technical User
Feb 2, 2003
220
GB
I have code that wrties to an array (I think !) but I would like to have a debug.print to see the values to check. I carn't seem to find how to do this and my code will not work.

Dim db As Database
Dim qry As QueryDef
Dim rs As DAO.Recordset
Dim str As String
Dim i As Integer
Dim MyArray(10) As Currency

Set db = CurrentDb()
Set qry = db.QueryDefs("MyQuery")
Set rs = qry.OpenRecordset

For i = 0 To rs.Fields.Count - 1
str = rs.Fields(i).Name
If str Like "*Price1" Then 'If field name ends with "Price1" then
MyArray(i) = rs.Fields(i).Value
End If
Next
Debug.Print MyArray ' This is to show the values that have been put in the array (Errors on this line)


This is my first time usining arrays so I am a bit lost.

Thanks
 
Sub TestArray()
Dim MyArray(10) As Integer
Dim counter As Integer
For counter = 0 To 9
MyArray(counter) = counter
Next counter
For counter = 0 To 9
Debug.Print "Array(" & counter & ") = " & MyArray(counter)
Next counter
End Sub
 
BeeTee,

Makes bit more sense now thanks.

I have another function called Minimum which looks at the values in an array and returns the lowest value. How would I use this function with the array that I have made ?

Minimum function is -

Function Minimum(ParamArray FieldArray() As Variant)
' Declare the two local variables.
Dim I As Integer
Dim currentVal As Variant

' Set the variable currentVal equal to the array of values.
currentVal = FieldArray(0)

' Cycle through each value from the row to find the smallest.
For I = 0 To UBound(FieldArray)
If FieldArray(I) < currentVal Then
currentVal = FieldArray(I)
End If
Next I

' Return the minimum value found.
Minimum = currentVal

End Function

Thank

 
' you don't want to declare FieldArray as a param array!
' that's more for passing variable numbers of arguments
' to a function

Function Minimum(FieldArray() As Variant)
' Declare the two local variables.
Dim I As Integer
Dim currentVal As Variant

' Set the variable currentVal equal to the array of values.
currentVal = FieldArray(0)

' Cycle through each value from the row to find the smallest.
For I = LBound(FieldArray) To UBound(FieldArray)
If FieldArray(I) < currentVal Then
currentVal = FieldArray(I)
End If
Next I

' Return the minimum value found.
Minimum = currentVal

End Function


' other than that, your code looks OK to me.
' one other small suggestion: use the LBound statement
' good luck!
 
CORRECTION:

' Set the variable currentVal equal to the array of values.
currentVal = FieldArray(LBound(FieldArray))

' Cycle through each value from the row to find the smallest.
For I = LBound(FieldArray) + 1 To UBound(FieldArray)
If FieldArray(I) < currentVal Then
currentVal = FieldArray(I)
End If
Next I
 
BeeTee,

Thanks for that. I will try it out first thing tomorrow morning and get back to you.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top