如何修复“堆栈空间不足”错误?

时间:2013-07-23 17:21:47

标签: vba ms-access recursion access-vba

我有一个代码,它接受一个表,并重新排列表以形成一个新表。它使用了少量数据,但是现在我尝试使用超过1,000条记录运行相同的代码,它得到的错误28是“堆栈空间不足”。我不会在这里复制我的所有代码,因为它太多了,我觉得没必要,除非你不这么认为。我认为这是我的sub的递归问题。我需要这个,因为DONOR_CONTACT_ID只能有4个收件人,如果它有更多,那么它必须创建一个具有相同DONOR_CONTACT_ID的新记录并填充收件人。

这是得到错误的子例程:

Sub NextDonor()

With rstOutput
.FindNext "[DONOR_CONTACT_ID] = " & strDonor2
'Find the next record in T_OUTPUT with that DONOR_CONTACT_ID

            If .NoMatch Then
                'If there are no more records with that DONOR_CONTACT_ID, add a new one
                .AddNew
                !DONOR_CONTACT_ID = strDonor1
                !RECIPIENT_1 = strRecip1
                !ORDER_NUMBER = strOrderNum1
                .Update
            Else
            'A second DONOR_CONTACT_ID in T_OUTPUT exists. Check to see if all fields are filled.
                If !DONOR_CONTACT_ID = strDonor2 Then
                    If IsNull(!RECIPIENT_2) And Not (IsNull(!RECIPIENT_1)) Then
                    'RECIPIENT_2 is empty, so populate it
                        .Edit
                        !RECIPIENT_2 = strRecip1
                        .Update

                    ElseIf IsNull(!RECIPIENT_3) And Not (IsNull(!RECIPIENT_2)) Then
                    'RECIPIENT_3 is empty, so populate it
                        .Edit
                        !RECIPIENT_3 = strRecip1
                        .Update
                    ElseIf IsNull(!RECIPIENT_4) And Not (IsNull(!RECIPIENT_3)) Then
                    'RECIPIENT_4 is empty, so populate it
                        .Edit
                        !RECIPIENT_4 = strRecip1
                        .Update

                    ElseIf Not IsNull(!RECIPIENT_4) Then
                    'RECIPIENT_4 is filled, so run this function again
                        Call NextDonor
                    End If

                End If
            End If
End With
End Sub

错误出现在“调用NextDonor”的行中,可能是因为递归。如果您需要我澄清我的代码尝试做什么,或者您是否希望我复制代码的其他部分,请告诉我。

2 个答案:

答案 0 :(得分:3)

试试这个以避免递归...

Sub NextDonor(byref Again as Boolean)
With rstOutput
DoItAgain :
.FindNext "[DONOR_CONTACT_ID] = " & strDonor2

  If ....
    ....
  ElseIf Not IsNull(!RECIPIENT_4) Then
    'RECIPIENT_4 is filled, so run this function again
    Goto DoItAgain
  End If
End Sub

答案 1 :(得分:0)

实际上,如果第4个插槽已满,您的递归代码和第1个答案都跳过收件人,您使用另一个查找迭代并丢失当前收件人!这也消除了递归。 代替:

 If .NoMatch or (not isnull(!recipient_4)Then
            'If there are no more records with that DONOR_CONTACT_ID, add a new one
            '    or current record is full
            .AddNew
            !DONOR_CONTACT_ID = strDonor1
            !RECIPIENT_1 = strRecip1
            !ORDER_NUMBER = strOrderNum1
            .Update
        Else
相关问题