2

我有一个表格的代码,并重新排列表格以形成一个新表格。它适用于少量数据,但现在我尝试使用超过 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

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

4

2 回答 2

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
于 2013-07-23T17:35:49.100 回答
1

实际上,如果第 4 个插槽已满,您的递归代码和第一个答案都会跳过收件人,您使用另一个 Find 进行迭代,您将失去当前收件人!这也消除了递归。反而:

 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
于 2013-07-23T19:45:43.307 回答