Marc B. Hankin
Marc B. Hankin

Reputation: 751

cannot pass array as an argument to a sub in vbscript; error message = "data is invalid"

In vbscript, I am trying (without success thus far) to pass an array as an argument to a sub:

subUnPackAArray(arrLineFieldValues)  

I get the following error message from Vbscript:

Error:  Type mismatch: 'subUnPackAArray'
Code:   800A000D
Source: Microsoft VBScript runtime error
System: The data is invalid.

I would appreciate any solutions or suggestions about where to look for a solution. I'm a vbscript hobbyist, not a pro, so please be somewhat gentle in your criticism.

The array contains the following text:

Line  0
Line  1
Line  2
Line  3
Line  4
Line  5
Line  6
Line  7
Line  8
Line  9
Line  
Line  
Line  
.... etc....

The name of the file from which the text is harvested is:

strFile2HarvestFromFullName = "C:\Apps\E_drive\Parameters for vbscript or python_script to harvest .txt"

I wanted to confirm that there are no errors in the sub "subUnPackAArray" (to which I am trying to pass "arrLineFieldValues" as an argument).
So, to confirm that, I inserted -- above the line: subUnPackAArray(arrLineFieldValues)
... virtually all the code in the sub "subUnPackAArray".
The code executes just fine, and I get the error message only when the vbscript hits the line:

subUnPackAArray(arrLineFieldValues)  

I can't figure out what's wrong.

Here's the entire text of my vbsript program: ' Read Arguments from a Text File

strFile2HarvestFromFullName = "C:\Apps\E_drive\Parameters for vbscript or python_script to harvest .txt"

fn_Read_text_file_lines_into_1_dimL_array(strFile2HarvestFromFullName)


'*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'* BEGIN_Function'*' BEGIN_Function '*' BEGIN_Function '*' BEGIN_Function '*' BEGIN_Function '*' BEGIN_Function '*' BEGIN_Function '*' BEGIN_Function 
'*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function fn_Read_text_file_lines_into_1_dimL_array(fname)

    Dim strFile2HarvestFromFullName
    Dim objFSO
    Dim objTextFile
    Dim arrLineFieldValues()
    Dim strText
    Dim subUnPackAArray

    strFile2HarvestFromFullName = fname

    Const ForReading = 1

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(strFile2HarvestFromFullName, ForReading)
    i = 0

    Do Until objTextFile.AtEndOfStream 

        ReDim Preserve arrLineFieldValues(i)
        strText = objTextFile.ReadLine
        'MsgBox strText
        arrLineFieldValues(i) = strText
        i = i + 1

    Loop


    intUbound = UBound(arrLineFieldValues)
    strMsgBoxMsg = ""   

    For i = 0 To intUbound

        strMsgBoxMsg = strMsgBoxMsg & _
            "arrLineFieldValues(" & i & ") = " & arrLineFieldValues(i) & vbCrLf 

    Next ' For i = 0 To intUbound

    MsgBox "Line 45:  " & vbCrLf & strMsgBoxMsg


    subUnPackAArray(arrLineFieldValues)

End Function ' fn_Read_text_file_lines_into_1_dimL_array(fname)

'*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'* END_Function '*'END_Function '*'END_Function '*'END_Function '*'END_Function '*'END_Function '*'END_Function '*'END_Function '*'END_Function '*'END_Function 
'*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'* BEGIN_Sub'*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' 
'*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

sub subUnPackAArray(strArray_OneDimensionOnly)
    '
    Dim strArray

    'strArray = strArray_OneDimensionOnlye
    intUbound = UBound(strArray_OneDimensionOnly)
    strMsgBoxMsg = ""   

    For i = 0 To intUbound

        strMsgBoxMsg = strMsgBoxMsg & _
            "strArray(" & i & ") = " & strArray_OneDimensionOnly(i) & vbCrLf 

    Next ' For i = 0 To intUbound

    MsgBox "Line 25:  " & vbCrLf & strMsgBoxMsg


End Sub ' subUnPackAArray()

'*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'* END_Sub '*'END_Sub '*'END_Sub '*'END_Sub '*'END_Sub '*'END_Sub '*'END_Sub '*'END_Sub '*'END_Sub '*'END_Sub '*'END_Sub '*'END_Sub '*'END_Sub 
'*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Upvotes: 0

Views: 3210

Answers (2)

Kul-Tigin
Kul-Tigin

Reputation: 16950

Remove the line Dim subUnPackAArray from fn_Read_text_file_lines_into_1_dimL_array. Local variable overrides global one. You are trying to invoke a global object from the local scope but you already defined locally. It fails.

Upvotes: 2

Nilpo
Nilpo

Reputation: 4816

Subs do not get called with parenthesis.

Change this:

MsgBox "Line 45:  " & vbCrLf & strMsgBoxMsg

subUnPackAArray(arrLineFieldValues)

To this:

MsgBox "Line 45:  " & vbCrLf & strMsgBoxMsg

subUnPackAArray arrLineFieldValues

The error indicates that the parser is expecting to encounter a function, not a subroutine.

Upvotes: 2

Related Questions