"for loop not initialized" on for each loop

G

Gavin

Hi!
I have a For Each loop in my code that cycles through values in an array,
but I'm getting a "for loop not initialized" error on it. The line that
generates the error is "For Each varArrayItem In varReqdFlds". The function
is called from inside another for each loop, which passes the variables
intApptStatus and varItem to it. The variables in the required fields list
are public and are declared at the top of the module. Any ideas?

Here's the code:

Function MissingFields(intApptStatus As Integer, varItem As Variant) As
Boolean
Dim varReqdFlds() As Variant
Dim varArrayItem As Variant
Dim intCounter As Integer

'Select the right list of required fields for this record
Select Case intApptStatus
Case Is < 4
ReDim strReqdFlds(9)
strReqdFlds = Array(strApptTypeDesc, dteRankEffDate, strDeptKey,
_ strDivAffil, strRankType, intApptStatus, strSalStatus, strWorkStatus,
strTenureCode)
Case 4
ReDim strReqdFlds(4)
strReqdFlds = Array(strApptTypeDesc, dteRankEffDate,
intApptStatus, _ strTenureCode)
Case 5
ReDim strReqdFlds(7)
strReqdFlds = Array(strApptTypeDesc, dteRankEffDate, strDeptKey,
_ strDivAffil, strRankType, intApptStatus, strEndReason)
End Select

'Test to make sure all needed fields are populated
For Each varArrayItem In varReqdFlds
If varArrayItem = "" Or IsNull(varArrayItem) Then
For intCounter = 1 To Form_CreateRankRecords.lstApproved.ListCount
Form_CreateRankRecords.lstApproved.Selected(intCounter) =
False
Next intCounter

'Select the first problem record
Form_CreateRankRecords.lstApproved.Selected(varItem) = True

MissingFields = True
GoTo exitProc
End If
Next varArrayItem

'If it gets through the previous loop without exiting, there are no
missing fields.
MissingFields = False

End Function
 
D

Dirk Goldgar

Gavin said:
Hi!
I have a For Each loop in my code that cycles through values in an
array,
but I'm getting a "for loop not initialized" error on it. The line
that generates the error is "For Each varArrayItem In varReqdFlds".
The function is called from inside another for each loop, which
passes the variables intApptStatus and varItem to it. The variables
in the required fields list are public and are declared at the top of
the module. Any ideas?

Here's the code:

Function MissingFields(intApptStatus As Integer, varItem As Variant)
As Boolean
Dim varReqdFlds() As Variant
Dim varArrayItem As Variant
Dim intCounter As Integer

'Select the right list of required fields for this record
Select Case intApptStatus
Case Is < 4
ReDim strReqdFlds(9)
strReqdFlds = Array(strApptTypeDesc, dteRankEffDate,
strDeptKey, _ strDivAffil, strRankType, intApptStatus, strSalStatus,
strWorkStatus, strTenureCode)
Case 4
ReDim strReqdFlds(4)
strReqdFlds = Array(strApptTypeDesc, dteRankEffDate,
intApptStatus, _ strTenureCode)
Case 5
ReDim strReqdFlds(7)
strReqdFlds = Array(strApptTypeDesc, dteRankEffDate,
strDeptKey, _ strDivAffil, strRankType, intApptStatus, strEndReason)
End Select

'Test to make sure all needed fields are populated
For Each varArrayItem In varReqdFlds
If varArrayItem = "" Or IsNull(varArrayItem) Then
For intCounter = 1 To
Form_CreateRankRecords.lstApproved.ListCount
Form_CreateRankRecords.lstApproved.Selected(intCounter) =
False
Next intCounter

'Select the first problem record
Form_CreateRankRecords.lstApproved.Selected(varItem) =
True

MissingFields = True
GoTo exitProc
End If
Next varArrayItem

'If it gets through the previous loop without exiting, there are
no missing fields.
MissingFields = False

End Function

I suspect your main problem is that you are initializing an array named
"strReqdFlds", but then looping over an uninitialized dynamic array
named "varReqdFlds".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top