3

I have a VB6 program which populates a two-dimensional array, passes that array into a function of a COM DLL, and then the COM DLL executes a VBScript assigning the array to a variable within the VBScript.

It sounds convoluted and antiquated, I agree, but my job is to fix the bug not re-write a lot of code.

In the VB6 program and in the VBScript the array variable is named "packageDetails". In VBScript and VB6 it is declared as:

dim packageDetails

In both VB6 and the VBScript I observe the following:

msgbox isArray(packageDetails) ' True
msgbox ubound(packageDetails, 1) ' 37
msgbox ubound(packageDetails, 2) ' 1

...this is as expected.

I have a CMS-generated TXT file with 10,000 records. I parse the TXT file with VB6. For each record I parse the data from the TXT file, populate array "packageDetails", then pass it to my DLL. 9,999 records work without error, but in ONE of the records I have the following issue:

In VB6 packageDetails(3, 0) stores the string "EA", this is the expected value. But in VBScript on that same array when I do msgbox packageDetails(3, 0) an exception is thrown with the description, "Type mismatch: 'packageDetails'".

The Event Viewer under Windows Logs/Application does not have any message for this issue.

Given that the max indices are 37 for the first dimension and 1 for the second, why does (3, 0) cause a type mismatch in VBScript but not in VB6 for the same array?

The array is populated by reading from a text file generated by the CMS operating system. I've observed the text file in a hex editor, and there are no unprintable characters in the file (no ASCII NUL bytes, etc).

Any thoughts on what might cause the issue?

Developer Webs
  • 983
  • 9
  • 29
  • What are you doing with `packageDetails(3, 0)`, are you assigning it to some variable, printing it, etc? Would be useful if you show the whole statement, or more code. Best is to post an [MCVE](https://stackoverflow.com/help/mcve). – A.S.H Jul 26 '17 at 14:36
  • I'm doing, `msgbox packageDetails(3, 0)` in VBScript. In VB6 I'm just debugging and using a combination of the immediate window and the "Watches". – Developer Webs Jul 26 '17 at 14:56
  • Any other error info ? like Event Viewer > Application error. Also, whats the declaration of packageDetails in vb6 and vbscript. – Mukul Varshney Jul 26 '17 at 14:57
  • packageDetails is a variant in VB6 and VBScript. It's delcared as, "dim packageDetails". I don't see anything in the Event Viewer under Windows Logs > Application. – Developer Webs Jul 26 '17 at 14:59
  • show us how the content of the array is being parsed and stored into the array – derloopkat Jul 26 '17 at 15:47
  • @deblocker That would mean posting a LOT of code. We instantiate a DLL. Then call, `dllInstance.value("my attribute") = packageDetails`. The "Value" property looks like this, "Public Property Let Value(ByRef AttributeSpecifier As Variant, Optional ByVal SequenceNumber As Long = mc_Seq_Num_Def, ByRef NewVal As Variant)". I should note that this code works for 9,999 of 10,000 records. – Developer Webs Jul 26 '17 at 15:53
  • @derloopkat We assign to each column separately. packageDetails(0, 0) = 'EA', packageDetails(0, 0) = '123', etc. As mentioned the source data is a text file generated by CMS. That file is a fixed-width text file. – Developer Webs Jul 26 '17 at 15:55
  • Check the LBound value for each dimension of the array in vb6 and in vba. It appears your vb6 array is zero based, perhaps when it gets to the vba code, it's then one (or another value) based? (though I don't know how that would happen off the top of my head, I'm also not sure this situation would result in a type mismatch rather than some other error, but worth checking to be sure, IMO). `LBound(packageDetails, 1)` and `LBound(packageDetails, 2)`. – MarkL Jul 26 '17 at 15:59
  • I'm sorry, I should have mentioned this in the original ticket. I thought I had, I've updated it now. I'm populating the array by reading from a fixed-width text file. The text file has 10,000 records. 9,999 of them update without error, only one record in the file causes issues. I've closely observed that 1 record and it has the correct values I'd expect. – Developer Webs Jul 26 '17 at 16:01
  • @MarkL The LBound of both dimensions is 0. – Developer Webs Jul 26 '17 at 16:03
  • @DeveloperWebs, when you try MsgBox Cstr(packageDetails(3, 0)) or MsgBox " " & packageDetails(3, 0) in vbscript what do you get? – derloopkat Jul 26 '17 at 16:19
  • @derloopkat I'd thought about the NULL base already. Prepending vbNullString it still blows up. Ditto for the Cstr call. – Developer Webs Jul 26 '17 at 17:02
  • If I didn't know better I'd swear there are ASCII NUL bytes in the data corrupting the array, but the array works fine in VB6 and having gone over the data with a Hex editor I don't see any hex values for non-printable characters. – Developer Webs Jul 26 '17 at 17:03
  • Can you add the code where you declare the array in vb6. Posting more of your code would help other members nail your issue. You could always strip the unnecessary code out and the post only the statements relevant to the arrays . – Zeddy Jul 27 '17 at 00:15
  • @Zeddy The declaration is: `dim packageDetails`. If you're asking for the code that populates it, that's hundreds of lines of code. – Developer Webs Jul 27 '17 at 19:41
  • It looks like it may be a VBScript interpretor error. My VBScript calls a property to retrieve the array. I use a debugger and observe that this property is returning an array with the correct values . The debugger's Immediate window and Watch can both resolve index (3, 0) of the variable being returned to the correct "EA" string value. But after the property returns the array to the caller, we see the error as discussed in the original question. – Developer Webs Jul 27 '17 at 19:48

2 Answers2

0

Solved it. The original array created in VB6 was of type variant. In VB6 after the array is populated we use redim to add/remove rows. When we redim, "As String()" was also accidentally used. In VB6, after the redim, index notation still properly returns values from the array. However, in VBScript it seems that trying to use index notation against the array which had been redimmed with "as string" causes the "type mismatch" error. When I remove the "AS String()" from the redim in VB6 the type mismatch error is no longer raised in VBScript.

Developer Webs
  • 983
  • 9
  • 29
  • Well done, you found the issue and posted the answer so that others can benefit if they ever fall into the same kind of problem. I would have hazarded a guess it had something to do with the declaration of the arrays, but without the code its pure guesswork. Well done anyway. +1 – Zeddy Jul 28 '17 at 19:23
  • can't still understand your statement: " this code works for 9,999 of 10,000 records" – deblocker Jul 30 '17 at 16:29
  • @deblocker I process 10,000 records using the same logic that is causing the problem and 9,999 don't have the error. I think only one of those is using reDim. When I correct the redim statement, removing the "as string" portion, there is no error. – Developer Webs Jul 31 '17 at 12:54
-1

Array should be declared using parenthesis, in VBScript also (as in VB6)

VBScript Arrays

https://www.tutorialspoint.com/vbscript/vbscript_arrays.htm

Giorgio Brausi
  • 414
  • 3
  • 7
  • Method #3 on the page you linked doesn't use parenthesis :) Declaring the variable as I had is not incorrect. It's valid to `dim x` and later do `redim x(0, 0)` to create an array. In fact, in my case it's preferable. If I don't have one or more records to store I want the variable to be `Empty` (the API I'm communicating with expects this behavior). – Developer Webs Jul 28 '17 at 18:09