[AccessD] [EXTERNAL] Re: Parameter (argument) order

jack drawbridge jackandpat.d at gmail.com
Fri Feb 17 07:45:43 CST 2023


Arthur,
I submitted your question to chatgpt and received a response as follows.
         ------------------------------------------------------------------
Ms access how to pass 2 arrays to a function that compares the arrays
element by element and returns false if they are not equivalent?

To pass two arrays to a function in MS Access VBA and compare them element
by element, you can follow these steps:

    Declare a function that accepts two arrays as arguments. For example:

vbnet

Function CompareArrays(arr1() As Variant, arr2() As Variant) As Boolean
    ' Your code here
End Function

In this example, we're declaring a function called CompareArrays that
accepts two arrays of Variant type as arguments.

    In the function body, loop through the elements of the arrays and
compare them element by element. If any of the elements are not equal,
return False. If all of the elements are equal, return True. For example:

vbnet

Function CompareArrays(arr1() As Variant, arr2() As Variant) As Boolean
    Dim i As Integer

    If UBound(arr1) <> UBound(arr2) Then
        CompareArrays = False
        Exit Function
    End If

    For i = 0 To UBound(arr1)
        If arr1(i) <> arr2(i) Then
            CompareArrays = False
            Exit Function
        End If
    Next i

    CompareArrays = True
End Function

In this example, we're first checking if the length of the two arrays is
equal. If they are not, we return False. Then, we loop through the elements
of the arrays using the For loop and compare them element by element. If
any of the elements are not equal, we return False. If all of the elements
are equal, we return True.

    To call the function, create two arrays and pass them to the function
as arguments. For example:

scss

Dim arr1(2) As Variant
Dim arr2(2) As Variant

arr1(0) = 1
arr1(1) = 2
arr1(2) = 3

arr2(0) = 1
arr2(1) = 2
arr2(2) = 4

Debug.Print CompareArrays(arr1, arr2) ' Returns False

In this example, we're creating two arrays arr1 and arr2, filling them with
elements and passing them to the CompareArrays function. The function
returns False because the last element of arr1 and arr2 are different.

Note that when passing arrays to a function, you need to make sure that the
arrays are of the same data type and have the same number of elements.

On Fri, Feb 17, 2023 at 1:30 AM Shane Groff via AccessD <
accessd at databaseadvisors.com> wrote:

> Hi Arthur, Stuart gave you an example of how to declare a function that
> takes arrays as arguments:
>
> Function ArrayCompare(ar1() As String, ar2() As String) As Boolean
>
> The parentheses () after ar1 and ar2 indicate that these are arrays of
> Strings, not just strings.
>
> As Stuart also pointed out, since a Variant can hold an Array, you could
> use:
>
> Function ArrayCompare(ar1 As Variant, ar2 As Variant) As Boolean
>
> And pass in Arrays as arguments (but this would be less clear, unless you
> had some case where you wanted to pass in something other than an array).
>
> My point was simply that the documentation for ParamArray is correct, it
> just isn't designed for the problem you are trying to solve.
>
> Shane
>
> -----Original Message-----
> From: AccessD <accessd-bounces+shaneg=microsoft.com at databaseadvisors.com>
> On Behalf Of Arthur Fuller
> Sent: Thursday, February 16, 2023 10:24 PM
> To: Access Developers discussion and problem solving <
> accessd at databaseadvisors.com>
> Subject: Re: [AccessD] [EXTERNAL] Re: Parameter (argument) order
>
> Thanks for trying, Stuart. You know that I love your mind and intellect.
> But apparently I am still failing to pose my question properly. Let me try
> once more. I want to pass two arrays to a function, and then do whatever I
> want with them. One simple possibility is to compare them. Another is to
> concatenate them and return the result. A third is to "subtract" them and
> return the difference as a tiny array. Another is to perform a
> matrix-multiplication on them, and return that result.
>
> the template is simple:
> Function arrManipulate(a1,a2) as <whatever>    'not sure how to declare the
> args; as Variant?
> Do Somestuff with a1 and a2       ' they are both arrays
> Return SomeResult
> End Function
>
> Maybe I'm missing something in VBA, but so far I can't seem to get from
> here to there. It's easy in C and C++, but I have not yet arrived at the
> equivalent method in VBA.
>
> On Fri, Feb 17, 2023 at 1:08 AM Stuart McLachlan <stuart at lexacorp.com.pg>
> wrote:
>
> > On 17 Feb 2023 at 5:07, Shane Groff via AccessD wrote:
> >
> > > https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fle
> > > arn.microsoft.com%2Fen-us%2Foffice%2Fvba%2Flanguage%2Fconcepts%2Fget
> > > ting&data=05%7C01%7Cshaneg%40microsoft.com%7Cfe4f20e8635c4d5f041508d
> > > b10afa42f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C6381221188605
> > > 78677%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLC
> > > JBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=19kB%2FD0FQCP9Y21
> > > YHyrpeF1ZulVrm%2BecPu%2Bkpln%2BHBo%3D&reserved=0
> > > -started/understanding-parameter-arrays
> > >
> > > A ParamArray is not for passing an array as an argument, it is for
> > > allowing you to pass a dynamic number of arguments as an array:
> > >
> > > If you declare:
> > > Function CalcSum(ParamArray args() as Variant) As Double
> > >
> > > You can call it like so:
> > >
> > > x = CalcSum(34, 47, 99, 12)
> > > or
> > > x = CalcSum(1, 1)
> > >
> > > and it can add all the arguments together (it receives all the
> > > arguments as a single array).
> > >
> > > It must be the last argument in the declaration, since all the
> > > arguments at the end of the caller will be 'consumed' to create the
> > > array, so there can't be any additional arguments.
> > >
> >
> > Another way to pass a series of arguments as an array :) Function
> > PassArray() As Long Dim v As Variant v = Array("Some text", 2, 3)
> > UseArray v End Function
> >
> > Function UseArray(v As Variant) As Long Debug.Print Mid$(v(0), v(1),
> > v(2)) End Function
> > --
> > AccessD mailing list
> > AccessD at databaseadvisors.com
> > https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdata
> > baseadvisors.com%2Fmailman%2Flistinfo%2Faccessd&data=05%7C01%7Cshaneg%
> > 40microsoft.com%7Cfe4f20e8635c4d5f041508db10afa42f%7C72f988bf86f141af9
> > 1ab2d7cd011db47%7C1%7C0%7C638122118860578677%7CUnknown%7CTWFpbGZsb3d8e
> > yJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C30
> > 00%7C%7C%7C&sdata=pvd9iE12M85Lc7zq0Tif0Jwu8%2FLjg2i2a6Qd%2Fq158Ws%3D&r
> > eserved=0
> > Website:
> > https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.d
> > atabaseadvisors.com%2F&data=05%7C01%7Cshaneg%40microsoft.com%7Cfe4f20e
> > 8635c4d5f041508db10afa42f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7
> > C638122118860578677%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIj
> > oiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=4W8m9rL
> > ettQh6Zvfz%2FjFcrU1QBeetXyxAynYgoUs%2B1A%3D&reserved=0
> >
>
>
> --
> Arthur
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
>
> https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdatabaseadvisors.com%2Fmailman%2Flistinfo%2Faccessd&data=05%7C01%7Cshaneg%40microsoft.com%7Cfe4f20e8635c4d5f041508db10afa42f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638122118860578677%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=pvd9iE12M85Lc7zq0Tif0Jwu8%2FLjg2i2a6Qd%2Fq158Ws%3D&reserved=0
> Website:
> https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.databaseadvisors.com%2F&data=05%7C01%7Cshaneg%40microsoft.com%7Cfe4f20e8635c4d5f041508db10afa42f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638122118860578677%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=4W8m9rLettQh6Zvfz%2FjFcrU1QBeetXyxAynYgoUs%2B1A%3D&reserved=0
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>


More information about the AccessD mailing list