Hi Tony, thanks for the pointers, having examined it fairly closely I have discovered what was going on. I managed to create a quick code sample which illustrates it as well. From my post count you can see that I'm new here so I hope I manage to format this correctly:
Option Explicit
Private Type NEW_TYPE
alngNums() As Long
End Type
Private m_audtNewTypes() As NEW_TYPE
Public Sub BreakMe()
Dim i As Long, j As Long
On Error GoTo Uninitialized_Error:
ReDim Preserve m_audtNewTypes(UBound(m_audtNewTypes) + 1)
Uninitialized_Resume:
On Error GoTo 0
ReDim m_audtNewTypes(UBound(m_audtNewTypes)).alngNums(0 To 9)
For i = 0 To UBound(m_audtNewTypes)
For j = 0 To 9
m_audtNewTypes(i).alngNums(j) = j
Next j
Next i
'with this line in it fails every second run.
ReDim m_audtNewTypes(0)
Exit Sub
Uninitialized_Error:
ReDim m_audtNewTypes(0)
Resume Uninitialized_Resume
End Sub
Basically my laziness in using
ReDim Preserve array(Ubound(array) + 1)
style array building was causing me problems cos at the very end of the routine I was calling
which I hoped was an easy way of clearing the array. Problem is, VBA seems to remember this assignment so next time it runs, m_audtNewTypes already has one element at index 0 (this can be checked by putting a breakpoint on the error handler, it is never reached on the second run), so
ReDim Preserve m_audtNewTypes(UBound(m_audtNewTypes) + 1)
actually adds another element at index 1. Later, when each of the arrays of Long for each element is iterated, the element at 0 has never had its array of Long initialised, as this is only done on whatever is at the last array position, which I thought would always be 0, ie the only element, but as VBA is remembering the assignment just before execution stops on the first run, it is causing me to receive my old friend subscript out of range. Once you have clicked 'End' on this error, the next tim eit runs it works fine.
What is really puzzling me is why, when the offending line is omitted, there is no problem. The array has still been initialized with one element. It is never explicitly removed. hmmm
I was always under the impression that VBA released objects and memory when execution stopped. I am finding out increasingly that I was wrong!!
Sorry for the remarkably long post. Maybe someone will be able to explain to me what is going on!!