Redim Array error - can't see the problem

This is a routine in a script library. I’ve highlighted where the error occurs. I’ve recompiled all the script in the system - nothing changes. I tried making i and BL() long and double but I see the same error. I’ve also done copy/paste to make sure the BL(i) isn’t a typo.

Any pointers are appreciated.

Doug

Sub BuildActualNestedBOM(_

PN() As Variant, _

Desc() As String, _

PrtQty() As Integer, _

BOMView As NotesView, _

i As Integer, _

BL() As Integer)

'==== i and BL() come in as 0

’ This is a recursive agent that can drill down n levels to build a full blown BOM.

’ This code could probably be modified to display the BOM in an indented fashion by

' logging the number of times each recursive call has been made.

' This is not required for the current implementation.



Dim ThisLevelQty As Integer

Dim TotQty As Integer

Dim dc As NotesDocumentCollection

Dim doc As NotesDocument     

Dim J As Integer

Dim IL As Integer   'indent level

Dim ParentQty As Integer



TotQty = 0   'initialize for this pass

ParentQty = PrtQty(i)

IL = BL(i) + 1



Set dc = BOMView.GetallDocumentsByKey(PN(i))    

If dc.count = 0 Then 

         ' do nothing as there is no new key

Else               

            ' extend key set

	For J = 1 To dc.count                       

      'Get Jth document NextLowerPart

		Set doc = dc.getNthdocument(J)         

		If doc.ComponentNumber(0) = "" Then

                'skip it and do not increment the parts list array

		Else

			i = i + 1                   

			Redim Preserve PN(i)

			Redim Preserve PrtQty(i)

			Redim Preserve Desc(i)

			Redim Preserve BL(i) '====>>> Redim error happens here on the first pass through the code.  i has the value of 1

			

			PN(i) = doc.ComponentNumber(0)                    

			Desc(i) = doc.ComponentDescription(0)

			ThisLevelQty = doc.ComponentQuantity(0)

			BL(i) = IL

			

			TotQty = ParentQty * ThisLevelQty

			PrtQty(i) = TotQty

			Call  BuildActualNestedBOM(PN, Desc, PrtQty,BOMView, i, BL)

		End If

	Next

End If

End Sub

Subject: Redim Array error - can’t see the problem

Can you redim a paramater?

Shawn

Subject: RE: Redim Array error - can’t see the problem

If by ‘parameter’ you mean all the stuff I pass into the sub, then the 3 params sitting above BL() redim just fine. Only BL(i) errors out.

If you mean something else, let me know.

Thanks.

Doug

Subject: RE: Redim Array error - can’t see the problem

Yes you can.

The following sample code will illustrate -

on Button onClick -

Sub Click(Source As Button)

Dim arr() As Integer

Redim Preserve arr(0)

arr(0) = 1

Call testfunc(arr)

End Sub

Function testfunc(arr)

i = 1

Redim Preserve arr(i)

arr(i) = 2

Msgbox Ubound(arr) 'results 1

End Function

The only thing to be noted is that the array “arr” if declared as fixed array (example - arr(0) or arr(1)) then you will see an error in “testfunc” function.

The array declared and then passed to the function should not have any fixed dimensions.

–Phani

Subject: RE: Redim Array error - can’t see the problem

So why is mine failing?

More info. The code that calls the sub looks like this (I picked out two integer arrays passed into the sub PrtQty() works, BOMLev() doesn’t):

.snip.

Dim PrtQty() As Integer          'component quantity

Dim BOMLev() As Integer     ' indent level

.snip.

n = 0

Redim Preserve PrtQty(n)

Redim Preserve BOMLev(n)

.snip.

PrtQty(n) = doc.ComponentQuantity(0)	

BOMLev(n) = 0

Call BuildActualNestedBOM(Keys, Desc, PrtQty,  luView,  n, BOMLev)

Sub BuildActualNestedBOM(_

PN() As Variant, _

Desc() As String, _

PrtQty() As Integer, _

BOMView As NotesView, _

i As Integer, _

BL() As Integer)

I’m not even sure where to look right now…

TIA

Doug

Subject: RE: Redim Array error - can’t see the problem

Are you seeing the Illegal ReDim on the initial call to the sub, or on one of the recursive calls?

Also, you do realize that ReDim Preserve can only increase the size of the arrray AND that unless you explicitly declare it, all parameters operate in a By Reference manner…that is you pass a pointer to the existing structure in memory.

Say at some point your i value gets to be 10; then you recursively enter the sub again with i being a value less than 10 you will get an error.

Subject: Holy…found it…updated

In the original code stored in the button I do this:

Dim BOMLev() as Integer

Dim n as integer

n = 0

BOMLev(n) = 0

BOMLev(0) turns out to be 0 and gets passed into the sub, no problem until you try and redim and then it fails


Changed it to:

BOMLev(n) = n

Poof, magic, it works.

Chage back to BOMLev(n) = 0, fails again with Illegal Redim.

WTF???

n is an integer

0 is an integer, right?

So why if I explicitly set an array value to an integer does it fail but if I set it to the value of an integer variable does it work?

1/2 hour…1/2 hour…1/2 hour…time to go home…

My thanks to everyone who stuck with me to here. If anybody actually understands what just happened, let me know.

This is a bug, right? Tell me it’s a bug…

Doug

ps - if I build a tiny little test routine that does the same thing, I can set var(i) = 0 and the sub handles the redim with no problem.

I know there’s another reason it’s failing…

If I take the code from the view button and the script library into a test db, strip out all of the ‘doc’ and ‘doc collection’ stuff that won’t work in the test db, I get the array error.

OK, I am going home now. Thanks again for sticking with this thread. Monday I’m going to come to work and the code will work fine with either form…wanna bet?

Subject: RE: Holy…found it…updated

Doug,

By itself this code:

Dim BOMLev() as Integer

Dim n as integer

n = 0

BOMLev(n) = 0

should fail - you are accessing an uninitialized array.

Are you not initializing the array before passing it into the function?

Subject: The whole enchilada - all da code…

Sorry, just left that bit off but yes, I do a Redim Preserve on the array before I set it.

Here is the entire code set in all of its glorious ugliness…

View level action:

Option Declare

Use “sLibGetAllChildDocs”

Sub Click(Source As Button)

Close  'closes all open files

Dim session As New NotesSession

Dim db As NotesDatabase  

Dim selDocs As NotesDocumentCollection

Dim luView As NotesView

Dim dc As NotesDocumentCollection     

Dim doc As NotesDocument     

Dim Keys() As Variant           'component part number

Dim Desc() As String             'component dest

Dim PrtQty() As Integer          'component quantity

Dim BOMLev() As Integer     ' indent level

’ Dim BIL() As Integer 'bom indent level, new var name to see if code will run

Dim n As Integer

Dim p As Integer

Dim CRLF As String

Dim fileNum As Integer

Dim FN As String

Dim DfltFN As String

Dim Hdr As String

Dim OutString As String

Dim J As Double



CRLF = Chr(13) & Chr(10)



Set db = session.CurrentDatabase

Set selDocs  = db.UnprocessedDocuments   'selected docs to be processed - test for n=1

If selDocs.Count <>1 Then

	Messagebox "You may only process one doc at a time.  Please make sure only one doc is highlighted and/or checkmarked and try again"

	Exit Sub

End If

Set doc = selDocs.GetFirstDocument

DfltFN = "C:\BOMFileFor_" & doc.ItemNumber(0) & ".txt"

fileNum% = Freefile()

FN = Inputbox( "Enter the name for the BOM file" , "File Name" , DfltFN )

Open FN For Output As fileNum%



n = 0

Set luView = db.GetView( "luNLAByNHA" )

Print "Building BOM"

Redim Preserve Keys(n)

Redim Preserve Desc(n)

Redim Preserve PrtQty(n)

Redim Preserve BOMLev(n)

’ Redim Preserve BIL(n)

Keys(n) = doc.ItemNumber(0)

Desc(n) = doc.ItemDescription(0)

PrtQty(n) = doc.ComponentQuantity(0)	

BOMLev(n) = n   'was 0, changed to n to see what happens.  It works, no more illegal redim in sub

’ BIL(n) = n

Call BuildActualNestedBOM(Keys, Desc, PrtQty,  luView,  n, BOMLev) 



On Error Resume Next



Print "Creating BOM file, please wait"

’ tilde delimited version

’ Hdr = “Item Number~Desc~Quantity~Indent”

’ Print #fileNum%, Hdr

’ For p = 0 To n - 1

’ OutString = Keys(p) & “~” & Desc(p) & “~” & PrtQty(p)& “~” & BOMLev(p)

’ Print #fileNum%, OutString

’ Next

’ tabbed to show indented bom

Print #fileNum%,  "Indent", "Item Number", "[Desc]", "Quantity"

For p = 0 To n '   - 1

	Print "Exporting item: " & Cstr(p) & " of: " & Cstr(n)

	Outstring = BOMLev(p) & "   " & Space(BOMLev(p) * 5) & Keys(p) & " [" & Desc(p) & "]    " &  PrtQty(p)

	Print #fileNum%, OutString

Next	

Messagebox "Done"

Close

Print

End Sub

Script Library:

Option Public

Option Declare

Sub BuildActualNestedBOM(_

PN() As Variant, _

Desc() As String, _

PrtQty() As Integer, _

BOMView As NotesView, _

i As Integer, _

BIL() As Integer)

’ This is a recursive agent that can drill down n levels to build a full blown BOM.

’ This code could probably be modified to display the BOM in an indented fashion by

' logging the number of times each recursive call has been made.

' This is not required for the current implementation.



Dim ThisLevelQty As Integer

Dim TotQty As Integer

Dim dc As NotesDocumentCollection

Dim doc As NotesDocument     

Dim J As Integer

Dim IL As Integer   'indent level

Dim ParentQty As Integer

TotQty = 0   'initialize for this pass

ParentQty = PrtQty(i)

IL = BIL(i) + 1  'increment bom level by 1 so it can be passed into next recursive call



Set dc = BOMView.GetallDocumentsByKey(PN(i))    

If dc.count = 0 Then 

         ' do nothing as there is no new key

Else               

            ' extend key set

	For J = 1 To dc.count                       

     'Get Jth document NextLowerPart

		Set doc = dc.getNthdocument(J)         

		If doc.ComponentNumber(0) = "" Then

                'skip it and do not increment the parts list array

		Else

			i = i + 1                   

			Redim Preserve PN(i)

			Redim Preserve PrtQty(i)

			Redim Preserve Desc(i)

			Redim Preserve BIL(i)

			

			PN(i) = doc.ComponentNumber(0)                    

			Desc(i) = doc.ComponentDescription(0)

			ThisLevelQty = doc.ComponentQuantity(0)

			BIL(i) = IL

			

			TotQty = ParentQty * ThisLevelQty

			PrtQty(i) = TotQty

			Call  BuildActualNestedBOM(PN, Desc, PrtQty,BOMView, i, BIL)

		End If

	Next

End If

End Sub

Subject: Interesting additional bit of info

I have discovered that this same code apparently ran without problem on 9.Feb.2007 at about 7 am.

Around 10 am, I made a change to the way data is pulled into the db (I use an lclsx routine to suck data in from Oracle. I changed the SQL select statement) and the agent turned on but not off. About 1/2 hour later, a second db running the same query (similarly modified) also turned on. My agent manager runs 2 threads so all other scheduled agents were locked out. There’s a seperate thread on this little problem elsewhere.

About noon, I downed the Dom server. It failed to go down or come back up because some rogue Unicenter update process had a lock on the system and would not let either Domino or the OS shut down. I killed Unicenter, rebooted the server (OS) and Domino restarted.

All subsequent attempt so run this file export function fail with the ‘illegal redim’ error.

If I strip the code down to just the bits that deal with the BOMLev/BIL, it works.

I’m in the process of gradually adding back in parts of the code to see if I can figure out which bit is interacting with the arrays and causing the problem.

I’m left wondering if the agent manager lock up/server down/up or the Unicenter updater caused the problem and I am actually chasing ghosts.

I don’t expect anybody to have an answer on this, I’m just continuing the tread for future reference.

Doug

Subject: RE: Redim Array error - can’t see the problem

The error occurs in the sub the first time it sees the variable.

Outside of the library/sub, the redim works.

Option declare is on in both the button that houses the original LS that calls the sub from the library and in the library itself.

You can’t dim variables in subs - I think that’s part of the sub declaration statement.

I’m about ready to retype the whole thing and see if that fixes anything.

Thanks.

Subject: RE: Redim Array error - can’t see the problem

Immediately before calling

Call BuildActualNestedBOM(Keys, Desc, PrtQty, luView, n, BOMLev)

The first time, put a

MsgBox Cstr(uBound(BOMLev))

I bet it says that it is a number greater than 0

Subject: RE: Redim Array error - can’t see the problem

And I’d win that bet…it’s 0

What is bizarre, is if I strip this down to a tiny little routine with 1 array and a mini sub that redims it but based on the same overall logic, it works fine.

See my ‘found it’ post for more confusing information.

Thanks.

Doug

Subject: RE: Redim Array error - can’t see the problem

Doug,

What is/does BL() have in it when it gets passed to BuildActualNestedBOM?

Also, what happens if you change the data type to variant on the BL() parameter like: BL() as Variant?

Shawn

Subject: RE: Redim Array error - can’t see the problem

What exactly is the error you are getting, subscript out of range? or Illegal redim or type mismatch or object variable not set or…?

Subject: RE: Redim Array error - can’t see the problem

Illegal Redim

The value passed in from BOMLev() is 0

The initial value of BL(0) is 0

I indexes to 1

All the other arrays redim to (1) no problem at all.

Hits BL(1) and I get the Illegal Redim error.

Makes no sense at all to me. Variable integer array, legal values, redims in the top level button code and properly sets BOMLev(0) = . Pass it in, BL(0) = 0, then wham…

Signed Frustrated in NH…

Subject: RE: Redim Array error - can’t see the problem

Doug, do you have Option Declare turned on?

When I run your code in my debugger. The redim preserve on BL(i) works like a champ.

Shawn

Subject: RE: Redim Array error - can’t see the problem

Option Declare is on in both the button that starts the process and in the library. Additionally, the libray is declared as public.

I have renamed the variable and that doesn’t help.

Subject: Redim Array error - can’t see the problem - update

As it happens, I had to walk away from this for a while and am just now back into it.

I’m no closer to actually understanding the issue, but the error happens when running in debug mode. Turn off the debugger and it works fine.

I’ve tried changing the name of the variable, looked to see if it’s in use elsewhere (it’s not), converted it to a variant - all no change; debugger on → illegal redim. debugger off → works fine.

If I figure anything else out, I’ll post.

Doug

Subject: never mind…

thought I had it but I didn’t.