Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Function Parameter Array Passing

Introduction

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:

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.

Two Test Programs.

Function ArrayArg_Test1().

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

'Result:
1
2
3
20
5

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.


Function ArrayArg_Test2().

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. 


Get the Array Sorted and Print.

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.

Sort the Array in Descending (Z-A) 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

To Sort the values in Ascending Order then the only change in this program required is to change Greater Than (>) symbol to Less Than (<) symbol. The number we have loaded into the array was already in Ascending Order.

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.

Share:

Function Parameter ByVal and ByRef Usage

Introduction.

Before taking up the above subject let us look at some fundamentals on variables for the benefit of novices.

When we define a variable in VBA or in any other programming language the computer reserves some memory location and allocates some memory cells (the number of cells allocated depends on the declared variable type, like Integer, Double, String etc.) to store values passed to it. 

In layman's analogy we can imagine a variable as a box with the name 'Apple' or whatever name we give to the box and uses that name to pick the value stored in it.  Assume that you have put 5 in the Apple box.  We can give these apples to someone in two ways.

  1. Make copies of the apples (the number) from the box ourselves and put it into another box and pass it.  The target box's name will be different.  The recipient of the new box can work with his copy of the apples, like add more apples into his box or remove some of them etc.  There will not be any change in the first box contents. 
  2. We can tell the other person (or Function), which area of the room (location) you have kept the original box of  apples, get the box contents from there and work with it.  In this room (or within the function body) there may be other boxes (Variables) with different names and contents.

Making Copies of Original Values.

In the first method explained above, unlike the physical box, you can make copies of the original value and store them into different Variables.  The original value will not change in the first Variable.  The Function that gets the new Variable with the copy have no access to the first Variable.  He can do whatever he wants to do with the copy he has.

In the second case you can tell the location of the Apple_Box1 to the other Function so that it can go there and find it's contents and do whatever the Function wants to do with them (add, subtract, multiply etc. or use it as part of other calculations) or whatever operations you would like to do with them. 

To prove the first point above let us take a closer look at the next two example functions Test_1A() (the calling function) and Test_1B() (the called function with the copy of the value).


Method-1 Examples:

Public Function Test_1A()

'Define two variables(boxes)to hold values of
'Long Integer type
Dim Apple_Box1 As Long, ApplesReceived As Long

'Put an initial value of 10

'into the variable

Apple_Box1 = 10

'sending a copy of Apple_Box1 Value to Apple_Box2

'Whatever value changes happened in Apple_Box2 in Test_1B()
'Received back into the third box:ApplesReceived

ApplesReceived = Test_1B(Apple_Box1)

'call funcction Test_1B() with the value
'Display the result in MsgBox.
MsgBox "Apple_Box1: " & Apple_Box1 & vbLf & "Apple_Box2 contents: " & ApplesReceived

End Function

In the above program we are defining two Variables Apple_Box1 and ApplesReceived both to store Long Integer type values. Here, we are not going to bother about what is Long Integer or Short Integer and their range of values that we can store in them etc.

Next line Apple_Box1 = 10, value 10 is stored in Apple_Box1. 

The next three lines are remarks explaining what we are doing in the next line. 

We are calling Test_1B() Function and passing the value of Apple_Box1 to the function to do something with the value received in a Variable (ByVal Apple_Box2).  The ByVal before Apple_Box2 given in Test_1B() function states that take a copy of the value from Apple_Box1.  The ‘As Long’ appearing after the closing parenthesis indicates that the second function does some calculations with the received value and returns the result to the first function.  The new value received is stored in ApplesReceived Variable. 

Next line MsgBox() function displays the Apple_Box1 contents and the result value (after modification done to the original copy of value) received from Test_1B() function.


The Test_1B Function.

Public Function Test_1B(ByVal Apple_Box2 As Long) As Long

'Take Note of the Key-Word 'ByVal' (says take a Copy of the Passed Value)
'Return the value back into the first function Test_1A
'After adding 15 to the original value of 10
'copied into Apple_Box2

Test_1B = Apple_Box2 + 15

End Function

There is only one executable statement in the above function.  Immediately after the function definition four lines of remarks indicating what happens in the function.

We will take a closer look at the next line of statement. This statement have two parts – first part appears left of the = sign and the second part is at the right side of the equal sign. 

In this expression, the left side of the equal sign will be a Variable or a Function name.  By now you will be asking yourself as why a function name there. That will be explained in a moment.

The computer always evaluates the expression given at the right side of the equal sign first and arrives at a single value and moves that result into the Variable given at the left side of the equal sign.  Any existing value in the variable will be lost. You can write that expression in two lines to arrive at the same result as below:


Apple_Box2 = Apple_Box2 + 15

Test_1B = Apple_Box2

In the first expression you can see that we have used the Apple_Box2 variable to the left-side and right side of the = sign. As I said earlier the expression at the right side of the equal sign is evaluated first. So it takes the existing value of 10 from Apple_Box2 for calculations and Adds 15 to it, arriving at the single result value of 25 and moves that value into Variable Apple_Box2, replacing earlier value 10.


If the Function name (the function name given at the first line of Code) appears at the left side of equal sign then the meaning of the statement is that the final result of the expression must be returned to the Calling Function.  Here, the Function Name acts as a Variable with the Data Type (As Long) specified immediately after the closing brackets on the first line.

This function name appears on the calling statement in the calling function to the right side of the = sign and a Variable Name on the left side of the = sign that saves the received result value (ApplesReceived = Test_1B(Apple_Box1).


Method-2 Examples:

In this method we have defined only one variable Apple_Box1 as Long Integer Type.  In the next line the Variable is loaded with an initial value of 10.  Next two lines are remarks explaining what is happening in the next line that calls the second function Test_1D().

Compare this statement with the statement that calls Test_1B.  Unlike the statement that calls Test_1B() the Function Name Test_1D and the function parameter Variable Apple_Box1 is only appears here.  The opening and closing brackets are omitted from the function name. The parameter variable is the second item.  Test_1D() function is not returning any value back into the calling function Test_1C.  Therefore we don't need to write this line of code in the form of an expression as we did in Test_1A Function.  But, you cannot write the statement as:

Test_1D(Apple_Box1).,

Once we use the parenthesis (normally used with the function name) around the parameter variable then VBA assumes that some value is returned from the called function and you are forced to write it like we did it in Function Test_1A:

x = Test_1D(Apple_Box1) 'Expression
'OR use Call statement 
Call Test_1D(Apple_Box1)

There will not be any value in the variable x because no value is returned from the called function.

If you feel comfortable with this method then you may do so. You will be defining one more variable for this purpose and your program takes up more memory.

Usage of Call statement requires the parenthesis around the parameter variable. If no parameters to pass to the called function even then you should use the opening and closing parenthesis at the end of Function Name like Call Test_1D().

When control returns from Test_1D() the next line displays the changed value in Apple_Box1 Variable.

Public Function Test_1C()
Dim Apple_Box1 As Long

'put 10 into Variable
Apple_Box1 = 10

'here Test_1D function takes the
'location address of Apple_Box1

Test_1D Apple_Box1 'compare this statement with Test_1A function

MsgBox "Apple_Box1: " & Apple_Box1 & vbLf & "Apple_Box2 contents: " & ApplesReceived

End Function

Test_1D() function takes the location address of the parameter variable passed to it. It works directly with the value stored in the variable Apple_Box1's own location.


Public Function Test_1D(ByRef Apple_Box2 As Long)

Apple_Box2 = Apple_Box2 + 15

End Function

 Test_1D() takes the location address (this is the memory location number of the variable) of variable Apple_Box1 into variable Apple_Box2, defined in the parameter area of Test_1D() function.

Now, look at the expression:

Apple_Box2 = Apple_Box2 + 15

Apple_Box2 contains the location address, not the contents of Apple_Box1 variable. But, no need to make any change in the expression to do any kind of calculations.  Computer uses the location address to pick the value from there and use the value in calculations.

Even though we have used ByRef to the Parameter Variable, to accept the location address of the variable passed to it (always a number irrespective of different variable types), rest of the parameter definition is like any other variable with variable Type (as Long) specification.  Compare Test_1D() function definition with the Test_1B().  Test_1D doesn't have the As Long at the end of the line because it is not returning any value from the function but it changes the original value at it's location directly. 

You may omit the usage ByRef from the Function declaration. By default VBA assumes that the Function Parameter declaration is ByRef (By Reference), if you have not explicitly define the parameter as ByVal like:

Public Function Test_1D(Apple_Box2 As Long)

Each Variable Type, like Byte, Integer, Long (integer), Double, String  etc. gets allocated with enough memory cells, besides it's own address, to hold their maximum range of values. This is different among Programming Languages: VBA, C, C++, C# etc.


In Conclusion.

We don't have to bother about going too deep into all those things but it does no harm to have a basic understanding of them.

If you get in touch with C language and it's variants, like the examples given above, you need to deal with these things, sooner or later.

Our discussion here was on a single variable and it's value. How we can work with an Array of Variables and Values. We will explore it's methods in the coming weeks.


Share:

Translate



PageRank
Subscribe in a reader
Your email address:

Delivered by FeedBurner

Search

Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Follow by Email

Labels

Forms Functions How Tos MS-Access Security Reports Class Module msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Array External Links Queries msaccess reports msaccess tips Accesstips Downloads Objects Property Collection Object Event Menus and Toolbars Controls MsaccessLinks Process Controls WithEvents Art Work msaccess How Tos Combo Boxes Graph Charts List Boxes VBA msaccessQuery Command Buttons Dictionary Object Form Report Calculation Command Button Data Emails and Alerts Query RaiseEvent Custom Functions Custom Wizards DOS Commands Data Type Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Item msaccessprocess security advanced Access Security Add Auto-Number Field Type Fields Form Instances Key Macros Menus SubForm Top Values Variables msaccess email msaccess menus progressmeter Access2007 Copy Excel Expression Join Methods Microsoft Numbering System Records Recordset Security Split Table Time Difference Utility Workgroup Wrapper Classes database function ms-access msaccess wizards reference text tutorial vba code Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Conditional Formatting Data Filtering Defining Pages Diagram Disk Dynamic Lookup Error Handler Export External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Tables Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif msaccess msaccess alerts pdf files restore switch toolbar updating upload

Featured Post

Opening Access Objects from Desktop

Frequently Used Methods. Set the Form Name in Display Form Option of Current Database in Access Options . BIG DEAL!, this is the first t...

Labels

Blog Archive

Recent Posts