Function GetTableSize(tableRec As String) As Variant
Reguards
SteWi
Main Topics
Browse All TopicsAndy,
I have a function defined:
Function GetTableSize(tableRec As String) As Integer
It has some code and then:
'Define array
Dim sizeArray(1) As Integer
'Enter size into an array
sizeArray(0) = tableWidth
sizeArray(1) = tableHeight
'Return the array
'GetTableSize = sizeArray
When running it I get told:
Missing or incorrect subscripts for array sizeArray
My point is how do I return an array? Cheers.
-Sam
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Just realised that wasn't what was causing the problem. The problem was I want to call that function and store what it returns (an array sizeArray(1)) in an array.
The error I get is
Function GetTableData(tableRec as String) As Integer
Dim tableSize(1) as Integer
tableSize = getTableSize(tableRec)
End Function
The error I get for this is:
Missing or incorrect subscripts for array tableSize
What am I doing wrong? Thanks.
-Sam
Use Variant!!
Private Function GetTableSize(tableRec As String) As Variant
'Define array
Dim sizeArray(1) As Integer
'Enter size into an array
sizeArray(0) = 123
sizeArray(1) = 456
'Return the array
GetTableSize = sizeArray()
End Function
Private Sub Form_Load()
Dim arr As Variant
arr = GetTableSize("string")
MsgBox arr(0) & ", " & arr(1)
End Sub
Hope you get what I mean.
SteWi
Sam,
In Visual Basic you can't return an array as a function returntype. Visual Basic won't allow it, and throws errors at you. It won't compile if you make an exe.
To resolve this, you must make one of the function arguments an array. Using that (by reference) array variable, you can return the data. The following function does this:
Public Function GetTableData(tableRec As String, tableSizeArray() As Integer) As Long
'
' Setup result array, destroy any data in it.
' Redim preseve is faster, but results in possible datacorruption.
'
ReDim tableSizeArray(1) As Integer
tableSizeArray(0) = tableWidth
tableSizeArray(1) = tableHeight
'
' As a result return the upper bounds of the array.
'
GetTableData2 = UBound(tableSizeArray)
End Function
Calling the function is pretty straight-forward, as the following code demonstrates:
Private Sub Command1_Click()
Dim myTableData() As Integer
Dim myTableDataUpperBound As Long
'
' Retrieve tabledata from the string
'
myTableDataUpperBound = GetTableData(tableRec, myTableData)
End Sub
Use of the Variant data-type should be avoided. Just my personal taste of style, imagine the night-mare when someone throws in an object....
Grtz.©
D.
"tableSize" is an array of 2 elements. Index of an array is called subscript. The value returned by "getTableSize(tableRec)" function call can be stored in one of the element of an array. When you write "tableSize = getTableSize(tableRec)", System will not come to know in which element (subscript) you want to store the value returned by your function and hence will give you the error "Missing or incorrect subscripts for array tableSize". In your case, you've not provided the subscript value of the array element.
Use:
tableSize(0) = getTableSize(tableRec)
tableSize(1) = getTableSize(tableRec)
BTW From Micro$oft help on advanced features of arrays:
Returning an Array from a Function
It's possible for a function to return an array of values. For example, you might want to return an array of bytes from a function without having to perform conversions to and from a string.
Here’s a simple example of a function that returns an array of bytes:
Private Sub Form_Load()
Dim b As Byte
Dim i As Integer
Dim ReturnArray() As Byte
b = Cbyte(54)
ReturnArray() = ArrayFunction(b)
For i = 0 To Ubound(ReturnArray)
Debug.Print ReturnArray(i)
Next
End Sub
Public Function ArrayFunction(b As Byte) As Byte()
Dim x(2) As Byte
x(0) = b
x(1) = b + CByte(200)
x(2) = b + b
ArrayFunction = x
End Function
Think it was mentioned to use ByRef so here's a more direct example:
Private Function GetTableSize(ByVal tableRec As String, ByRef sizeArray() As Integer) As Integer
sizeArray(0) = 100
sizeArray(1) = 100
End Function
Private Sub Form_Load()
Dim MyArray(1) As Integer
GetTableSize "Test", MyArray()
MsgBox MyArray(0)
MsgBox MyArray(1)
End Sub
Hi,
I want to clarify that although my solution works, it is not correct in terms of the VB language. As Puneesh said, you can return an array as the function return type. I stand corrected on that matter.
If the example I gave works for you, that is fine. It should be noted though, that there is another way to do exactly the same. The following code will return an array as the result:
Public Function GetTableData(ByRef tableRec As String, ByRef arraySize As Long) As Integer()
Dim tableSizeArray(1) As Integer
'
' Put data in the array
'
tableSizeArray(0) = tableWidth
tableSizeArray(1) = tableHeight
'
' Indicate success by returning the upperbound of the array.
' In this case, it will always return true, since the array
' is declared and initialized in this function.
'
arraySize = UBound(tableSizeArray)
'
' Set the return data
'
GetTableData = tableSizeArray()
End Function
Just to set things straight.
Grtz.©
D.
Business Accounts
Answer for Membership
by: samblakePosted on 2003-09-12 at 04:35:23ID: 9345029
lol, i sent this to andy a second ago. that's why he has his name at the top. anyone can answer really ;)