Passing an array to function

  • Thread starter jer99 via AccessMonster.com
  • Start date
J

jer99 via AccessMonster.com

I've read through the messages here and have tried a number of things.
Still can't get this to work.
Here's the two sub/functions:

Function LastValue(sArray() As String) As Integer
Dim iTempCount As Integer
For iTempCount = 0 To UBound(sArray)
If iTempCount = "" Then
Exit For
End If
Next
If iTempCount <> UBound(sArray) Then
LastValue = iTempCount - 1
Else
LastValue = iTempCount
End If
End Function

sub testit()
Dim aTemp(10) As String
aTemp(0) = "0"
aTemp(1) = "one"
Debug.Print LastValue(aTemp) <<<<<<<<--------------------Problem

End Function

I can't understand why I keep getting an error message saying it's looking
for an Array to be passed.

Anyone?
 
S

strive4peace

here is something to pattern after..

'~~~~~~~~~~~~~~~~~~~~~~~~

Sub test()
Dim aStr(1 To 10) As String _
, i As Integer

For i = 1 To 10
aStr(i) = Chr(64 + i)
Next i

ListArray aStr

End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~

Sub ListArray( _
pStr)

Dim i As Integer _
, s As String

s = ""
For i = LBound(pStr) To UBound(pStr)
s = s & pStr(i) & " "
Next i

MsgBox s
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~


Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*
 
J

jer99 via AccessMonster.com

I guess I missed it.
The Sub ListArray( pStr) , pStr is not defined.

In the example, I did define it as a string (I also tried variant) and I
still receive the error.
 
S

strive4peace

try not data-typing the array argument for LastValue (as I did in the
example)...

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*
 
R

RoyVidar

jer99 via AccessMonster.com said:
I've read through the messages here and have tried a number of
things. Still can't get this to work.
Here's the two sub/functions:

Function LastValue(sArray() As String) As Integer
Dim iTempCount As Integer
For iTempCount = 0 To UBound(sArray)
If iTempCount = "" Then
Exit For
End If
Next
If iTempCount <> UBound(sArray) Then
LastValue = iTempCount - 1
Else
LastValue = iTempCount
End If
End Function

sub testit()
Dim aTemp(10) As String
aTemp(0) = "0"
aTemp(1) = "one"
Debug.Print LastValue(aTemp) <<<<<<<<--------------------Problem

End Function

I can't understand why I keep getting an error message saying it's
looking for an Array to be passed.

Anyone?

When I copy/paste your function into VBE, it first complains about
the last End Function, which is supposed to be End Sub. Then it
complains about Type Mismatch on the line

If iTempCount = "" Then

where you compare an integer with ZLS, which isn't supposed to be
possible. I don't know what you intend with this test, but I
suppose you could remove it. Or are you intending to test the
content of the array?

If sArray(iTempCount) = "" Then

When this is fixed, there's no complaint about missing array on my
setup. I suggest using Option Explicit at the top of each module,
which helps pick up such as this. To ensure this is turned on in
every new module, in Tools | Options, check "Require Variable
Declaration".
 
J

jer99 via AccessMonster.com

Thanks for the good eye Roy.
Actually I had caught the end function, however, I didn't catch the
sArray(iTempCount) = ""

I probably would have if the error I received wasn't as strange.

It had said that it couldn't recognize the array passed - or some such
message. That had thown me off since I do not deal with arrays for the most
part.

Again, thanks Roy and I apologize for posting code with such a blatant error.


jerry
jer99 via AccessMonster.com said:
I've read through the messages here and have tried a number of
things. Still can't get this to work.
[quoted text clipped - 26 lines]

When I copy/paste your function into VBE, it first complains about
the last End Function, which is supposed to be End Sub. Then it
complains about Type Mismatch on the line

If iTempCount = "" Then

where you compare an integer with ZLS, which isn't supposed to be
possible. I don't know what you intend with this test, but I
suppose you could remove it. Or are you intending to test the
content of the array?

If sArray(iTempCount) = "" Then

When this is fixed, there's no complaint about missing array on my
setup. I suggest using Option Explicit at the top of each module,
which helps pick up such as this. To ensure this is turned on in
every new module, in Tools | Options, check "Require Variable
Declaration".
 
R

RoyVidar

jer99 via AccessMonster.com said:
Thanks for the good eye Roy.
Actually I had caught the end function, however, I didn't catch the
sArray(iTempCount) = ""

I probably would have if the error I received wasn't as strange.

It had said that it couldn't recognize the array passed - or some
such message. That had thown me off since I do not deal with arrays
for the most part.

Again, thanks Roy and I apologize for posting code with such a
blatant error.


jerry
jer99 via AccessMonster.com said:
I've read through the messages here and have tried a number of
things. Still can't get this to work. [quoted text clipped - 26
lines]

Anyone?

When I copy/paste your function into VBE, it first complains about
the last End Function, which is supposed to be End Sub. Then it
complains about Type Mismatch on the line

If iTempCount = "" Then

where you compare an integer with ZLS, which isn't supposed to be
possible. I don't know what you intend with this test, but I
suppose you could remove it. Or are you intending to test the
content of the array?

If sArray(iTempCount) = "" Then

When this is fixed, there's no complaint about missing array on my
setup. I suggest using Option Explicit at the top of each module,
which helps pick up such as this. To ensure this is turned on in
every new module, in Tools | Options, check "Require Variable
Declaration".

If you feel the need to apologize, then it's probably I who should
apologize. I didn't intend to ridicule, but to demonstrate that when
using Option Explicit at the top of the each module, things like this
is spotted immideately by the compiler, which frees up more time to
deal with the buisiness issues.
 
S

strive4peace

Hi Jerry,

it wasn't that blatant... I missed it too ;)

Roy, good catch!

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*


Thanks for the good eye Roy.
Actually I had caught the end function, however, I didn't catch the
sArray(iTempCount) = ""

I probably would have if the error I received wasn't as strange.

It had said that it couldn't recognize the array passed - or some such
message. That had thown me off since I do not deal with arrays for the most
part.

Again, thanks Roy and I apologize for posting code with such a blatant error.


jerry
jer99 via AccessMonster.com said:
I've read through the messages here and have tried a number of
things. Still can't get this to work.
[quoted text clipped - 26 lines]
When I copy/paste your function into VBE, it first complains about
the last End Function, which is supposed to be End Sub. Then it
complains about Type Mismatch on the line

If iTempCount = "" Then

where you compare an integer with ZLS, which isn't supposed to be
possible. I don't know what you intend with this test, but I
suppose you could remove it. Or are you intending to test the
content of the array?

If sArray(iTempCount) = "" Then

When this is fixed, there's no complaint about missing array on my
setup. I suggest using Option Explicit at the top of each module,
which helps pick up such as this. To ensure this is turned on in
every new module, in Tools | Options, check "Require Variable
Declaration".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top