Search code examples
vbscriptcatia

Transfer data from sub-Sub to Function and back to main Sub


I'm trying to write code that fill array in Function and than return result to main Sub. But this Function called from another Sub (sub-Sub). To understand how this will work I tried go step by step. And wrote this code:

Sub CATMain()
    Dim val1
    Call WalkDownTree() 'Here I call Sub to walk down Product Tree in CATIA
    val1 = ParamTable(PartNumber, Name, Material, Texture, Color, Quantity)
    For i=0 To UBound(val1)
        MsgBox val1(i)
    Next

End Sub

Sub WalkDownTree() 'Simplified code of the walk down tree to understand data transfer
    PartNumber = "PartNumber"
    Name = "Name"
    Material = "Material"
    Texture = "Texture"
    Color = "Color"
    Quantity = 1
    Call ParamTable(PartNumber, Name, Material, Texture, Color, Quantity)
End Sub

Function ParamTable(PartNumber, Name, Material, Texture, Color, Quantity) 'Simplified array filing code. At original code I get all data from Part
    Dim BOMTable(6,1000)
    BOMTable(1,k) = PartNumber 
    BOMTable(2,k) = Name
    BOMTable(3,k) = Material
    BOMTable(4,k) = Texture
    BOMTable(5,k) = Color
    BOMTable(6,k) = 1
    ParamTable = BOMTable
End Function

But I have error at line "MsgBox val1(i)": "Subscript out of range". What did I miss? And maybe exists more simple way to transfer array from Function to main Sub when Function fill from sub-Sub?


Solution

  • After some time I have a solution This code works:

    Dim PartNumber, Name, Material, Texture, Color, Quantity, k
    Dim BOMTable(6,1000)
    
    Sub CATMain()
        Dim val1
        Call WalkDownTree()
        val1 = ParamTable(PartNumber, Name, Material, Texture, Color, Quantity, k)
        For i = 1 To UBound(val1, 1)
            For j = 1 To k-1
            MsgBox val1(i,j)
            Next
        Next
    
    End Sub
    
    Sub WalkDownTree()
        For k = 1 To 3
            PartNumber = "PartNumber" & k
            Name = "Name" & k
            Material = "Material" & k
            Texture = "Texture" & k
            Color = "Color" & k
            Quantity = 1
            Call ParamTable(PartNumber, Name, Material, Texture, Color, Quantity, k)
        Next
    End Sub
    
    Function ParamTable(PartNumber, Name, Material, Texture, Color, Quantity, k)
        BOMTable(1,k) = PartNumber 
        BOMTable(2,k) = Name
        BOMTable(3,k) = Material
        BOMTable(4,k) = Texture
        BOMTable(5,k) = Color
        BOMTable(6,k) = Quantity
        ParamTable = BOMTable
    End Function