Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Function Parameter Array Passing

Last week we have explored the usage of ByVal (By Value) and ByRef (By Reference),  in the Function Parameter, to pass the value from  a Variable from the calling function or the Location Address of a Variable to the Called Function.  If you have not visited the earlier page the link is given below:

  • Function Parameter ByVal and ByRef usage.

Now, we will learn:

  1. how to pass the location address of a single element of an Array  to the called Function and change it’s value
  2. how to pass the Location Address of an array and sort the values in the array in Descending Order.

First, let us write two small programs for the first example. The first Program Code is given below.

Public Function ArrayArg_Test1()
Dim NumArray(1 To 5) As Integer, j As Integer

'load array with numbers 1 to 5
For j = 1 To 5
NumArray(j) = j
Next

'pass a single element value
Call ArrayArg_Test2(NumArray(4))

'Print the Array values after change
For j = 1 To 5
Debug.Print j, NumArray(j)
Next

End Function

In the first line of code we have defined an Array Variable (NumArray()) with five elements to store Integer type values. Variable j is a control variable defined for For...Next Loop.

Inside the For....Next loop the Array is loaded with values 1 to 5, i.e. NumArray(1) = 1, Numarray(2)=2 and so on up to 5 elements.

Next, we call the function ArrayArg_Test2() function with the NumArray(4) element passed as parameter.  The number 4 within brackets is the index number of the element not the value itself.  But, we have the value 4 in that element too.  The called function ArrayArg_Test2() receives the passed value/location depends on the parameter definition there. If we use ByRef  or omit the ByRef specification before the Variable Name and Data Type then the called function takes the passed variable's location address to work with the value stored in the original Variable.  We will go with the second method and will not use the ByRef specification in the next function’s  parameter definition.

Within the next For...Next loop the array contents are printed in the Debug Window.  If any change done by the function ArrayArg_Test2() will show up in the printed list.  Since, we already knew that the array elements 1 to 5 contains the values 1,2,3,4,5 we have not printed those values before calling the second function.

The ArrayArg_Test2() Function VBA Code is given below:

Public Function ArrayArg_Test2(NA As Integer) 'The word ByRef is omited
'multiply NumArray(4) value * 5 = 20
NA = NA * 5
End Function

The Variable NA is assigned with the NumArray’s 4th element location address.  The ArrayArg_Test2() picks the value from NumArray(4) itself, multiply it by 5 and store the result back into the same location.

This was working with a single element of an Array.  What about passing the full Array’s location address and work with hundreds of elements of this array in the called Function. 

We will use the same array we have used in the above example and sort the values in Descending order by passing the full array to the Sorting function. 

The modified version of the First Function Code is given below. 

Public Function ArrayArg_Test3()
Dim NumArray(1 To 5) As Integer, j As Integer

For j = 1 To 5
NumArray(j) = j
Next

'Pass the array to the called function
Call ArrayArg_Test4(NumArray())

'Print the Sorted Array
For j = 1 To 5
Debug.Print j, NumArray(j)
Next

End Function

Check the function call statement. NumArray() is passed without the element number, as we did in the earlier example. The opening and closing brackets are required along with the array variable name to indicate that the parameter passed is an array not a single variable.

When control is returned from the ArrayArg_Test4() function the sorted list of numbers are printed in the debug window. The value printed at the left side is the array element number and the right side value is array value itself,  sorted in Decending order.

The Data Sorting Program is given below:

Public Function ArrayArg_Test4(apple() As Integer)
Dim j As Integer, k As Integer, Temp As Integer

'Bubble Sort the Array in Descending order
' 1st loop runs maximum array elements minus 1 times

For j = 1 To 5 - 1 ' in place of 5-1 you may use Ubound(apple)-1

' inner loop starts with outer loop's current value + 1
' and runs to the maximum number of array elements times

For k = j + 1 To 5 ' replace 5 with Ubound(apple)

If apple(k) > apple(j) Then 'if second value is greater

Temp = apple(j) 'copy 1st value to Temp Variable

apple(j) = apple(k) 'move greater value up

apple(k) = Temp ' move the smaller value down

End If
Next k ' compare next two elements
Next j
End Function

If you want to sort the values in Ascending Order then the only change in this program required is to change Greater Than (>) symbol to Less Than (

If you remove the 5-1 constant from the first For...Next loop and replace with Ubound(apple)-1 and replace 5 with Ubound(apple) in the second loop you can use the program to sort the array with any number of elements without change in the Program.

Notice that we have omitted the ByRef specification in the called ArrayArg_Test4()Function Parameter definition. VBA, by default, takes it as ByRef Parameter Variable.

  • Autonumber with Date and Sequence Number
  • Overlaying Sub-Forms in Real-Time
  • Opening Multiple Instances of Form
  • Conditional Formatting in Continuous Form
  • Control Setfocus on Tab-Page Click


This post first appeared on LEARN MS-ACCESS TIPS AND TRICKS, please read the originial post: here

Share the post

Function Parameter Array Passing

×

Subscribe to Learn Ms-access Tips And Tricks

Get updates delivered right to your inbox!

Thank you for your subscription

×