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

Procedures and Arrays

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
0
0
US
Guys,

Is it possible to pass an array that has been declared in one procedure to another procedure as a parameter and allow the second procedure to modify some of the array fields.

eg.

Sub Procedure1

Dim myArray
.
.
myArray = recordset.GetRows

Call Procedure2(myArray)

End Sub

Sub Procedure2(inpArray)

Dim i

For i = 0 to UBound(inpArray, 2)
if inpArray(5, i) = date() then
inpArray(4, i) = inpArray(4, i) + 10
end if
Next

End Sub Mise Le Meas,

Mighty :)
 
Yes, an array can be passed


Call initialArray()

' ========
Sub initialArray()
Dim myArray(3)

For i = 0 to 3
myArray(i) = 5 + i
Next
s = "inital array"
for j = 0 to 3
s = s & vbcrlf & myArray(j)
Next

Call changeArray(myArray,s)

msgbox s

End Sub
' ==========
Sub changeArray(someArray,s)

For k = 0 to UBound(someArray)
someArray(k) = someArray(k) + 10
next

for k = 0 to UBound(someArray)
s = s & vbcrlf & someArray(k)
next

End Sub

At least for one dimensional arrays. You might have to worry about subtypes.

Parke
 
Parke,

Thanks for the response. I have a multi-dimensional array and was wondering - how can I pass just one row of the array to the procedure as a parameter. Mise Le Meas,

Mighty :)
 
Mighty:

Arrays are automatically passed byref. So pass the array, change the row as required and it is done.

' tblPractice has fields, startdate (datetime), istudents (number),
' tclassroom (text), ID (autonumber)
' I inserted three records

Dim cn
Dim rst
Dim myarray
Const adStateOpen = 1
Const adOpenForwardOnly = 0
Const adOpenStatic = 3
Const adLockReadOnly = 1
Const adGetRowsRest = -1

Set cn = CreateObject("ADODB.connection")

cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\projects.mdb;"
set rst = createobject("ADODB.RecordSet")

SQLString = "Select startdate,istudents,tclassroom from tblPractice"

rst.open SQLString, cn, adOpenStatic, adLockReadOnly

' If cn.State = adStateOpen Then
' MsgBox "opened Access"
' End If

s = "tblpractice: " & vbcrlf
msgbox s

myarray = rst.GetRows(adGetRowsRest)
For irow = 0 to UBound(myarray,2)
for iCol = 0 to UBound(myarray,1)
s = s & vbcrlf & myArray(iCol,iRow)
Next
Next

msgbox s

Call changeRecordset(myarray)

' just to see the change to the array is permanent.
s = s & vbcrlf & "after changing the number of students"
For irow = 0 to UBound(myarray,2)
for iCol = 0 to UBound(myarray,1)
s = s & vbcrlf & myArray(iCol,iRow)
Next
Next

msgbox s

' ==============
Sub changeRecordset(passarray)

s2 = ""
For irow = 0 to UBound(passarray,1)
passarray(1,irow) = passarray(1,irow) + 100
s2 = s2 & vbcrlf & passarray(1,irow)
Next
msgbox(s2)
msgbox("Finished sub")
End Sub

Hope this helps,

Parke
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top