LearningJrDev
LearningJrDev

Reputation: 941

Returning a 2d array

I have the following function

Function GetSearchField2dArray(tableName)
    sql = "SELECT * FROM "&tableName&" WHERE TEST IS NULL"

    set rstField = DataConn.execute(sql)

    Dim rv : rv = ConvertSqlResponseTo2dArray (rstField, "")

    set rstField = Nothing

    GetTCLSSearchFields2dArray = rv
End Function

This function takes a sql resultset and using ConvertSqlResponseTo2dArray converted it to a 2d array. I have verified that rv is indeed an array working perfect.

Here is ConvertSqlResponseTo2dArray for reference:

Function ConvertSqlResponseTo2dArray(rstField, strExclude)
    iField_tmp = 0

    ReDim arrField(2, 0)
    For i = 0 To rstField.Fields.Count - 1
        If InStr(strExclude, "," & rstField.Fields(i).Name & ",") = 0 Then
            strFieldType = rstField.Fields(i).Type
            ReDim Preserve arrField(2, iField_tmp)
            arrField(0, iField_tmp) = rstField.Fields(i).Name
            If strFieldType = adDate Or strFieldType = adDBDate Or strFieldType = adDBTime Or strFieldType = adDBTimeStamp Then
                ' Date
                arrField(1, iField_tmp) = "date"
            ElseIf strFieldType = adSmallInt Or strFieldType = adInteger Or strFieldType = adTinyInt Or strFieldType = adUnsignedTinyInt Or strFieldType = adUnsignedSmallInt Or strFieldType = adUnsignedInt Or strFieldType = adBigInt Or strFieldType = adUnsignedBigInt Then
                ' Integer
                arrField(1, iField_tmp) = "num"
            ElseIf strFieldType = adSingle Or strFieldType = adDouble Or strFieldType = adCurrency Or strFieldType = adDecimal Or strFieldType = adVarNumeric Then
                ' Decimal
                arrField(1, iField_tmp) = "num"
            ElseIf strFieldTYpe = adBoolean Then
                ' Boolean
                arrField(1, iField_tmp) = "boolean"
            Else
                arrField(1, iField_tmp) = "string"
            End If
            iField_tmp = iField_tmp + 1
        End If
    Next

    ConvertSqlResponseTo2dArray = arrField
End Function

Then I have code where I am using it

Session("SearchFields") = GetSearchField2dArray("Test1")

Session("SearchFields") is empty, and not an array. The case it that it's a 2d array before it returns and not a 2d array after it returns. Been struggling with this for almost an hour.

I have tried setting the result of GetSearchField2dArray to a variable and it still has the same issue.

Upvotes: 0

Views: 90

Answers (1)

Kul-Tigin
Kul-Tigin

Reputation: 16950

Typo. GetSearchField2dArray returns Empty because there is no value assignment for it.
The last line of the function must be GetSearchField2dArray = rv instead of GetTCLSSearchFields2dArray = rv.

Remember to use Option Explicit to prevent such confusions.

Upvotes: 2

Related Questions