"堆栈空间不足"在vba中使用递归函数进行访问

时间:2016-04-04 14:08:31

标签: vba ms-access recursion access-vba

我和你联系是因为我在为Access程序编写模块时遇到了麻烦。

简介

我有4张桌子:产品,食谱,Ordonnancement&命令。 Ordonnancement和Commands具有相同的结构,第二个是通过receipes处理命令的结果。

VBA模块的目标:

我创建了一个模块,通过在配方中处理命令,为Ordonnancement表创建记录。详细地说,我使用递归函数来处理recipes的变量深度,这允许我遍历Receipes表并在所有产品中生成一个日期的需求。

说明:

- 我通常在C#中使用EF来处理数据库。在几次尝试直接使用Access的RecordSet可能性之后,我决定为ReceipeLign和OrdoLign生成POCO类,在这些对象的集合中存储表的数据,使用它们然后提交到Access表,向每个对象添加记录记录集。

- 我用法语工作,所以我翻译了一些东西,以便每个人都能理解。它可能不完美,如果不清楚,请告诉我。

代码:

Option Compare Database
Option Explicit

Dim cnc As New ADODB.Connection
Dim CRecordSet As New ADODB.Recordset
Dim FTRecordSet As New ADODB.Recordset
Dim ORecordSet As New ADODB.Recordset

Public Sub GenerateOrdonnancement()

'Retrieving info from tables Commandes & FT in RecordSets.
Set cnc = CurrentProject.Connection
Set CRecordSet = cnc.Execute("SELECT * FROM Commandes")
Set FTRecordSet = cnc.Execute("SELECT * FROM FichesTechniques")
Set ORecordSet = cnc.Execute("SELECT * FROM Ordonnancement")

'Creation of the list to receive data from the tables
Dim Commandes As New Collection
Dim FicheTechniques As New Collection

'Retrieving commands and receipes
Dim Commande As ligneOrdo
Dim ordo As ligneOrdo
Dim FT As ligneFT

Do Until CRecordSet.EOF

    Set Commande = New ligneOrdo
    Commande.DateCommande = CRecordSet("dateCommande").Value
    Commande.Produit = CRecordSet("Produit").Value
    Commande.Quantite = CRecordSet("quantite").Value
    Commandes.Add Commande
    CRecordSet.MoveNext

Loop
CRecordSet.Close


Do Until FTRecordSet.EOF

    Set FT = New ligneFT
    FT.Nom = FTRecordSet("Nom").Value
    FT.Ingredient = FTRecordSet("Ingredient").Value
    FT.Quantite = FTRecordSet("quantité").Value
    FT.IsComposed = FTRecordSet("Composé").Value
    FicheTechniques.Add FT
    FTRecordSet.MoveNext

Loop
FTRecordSet.Close


'creation of the collection of ordo
'Later: versionning of the Ordonnancements
Dim AProduire As New Collection

Dim mr As ligneOrdo
For Each mr In Commandes
    Dim coll As Collection
    Set coll = CreateOrdoLigne(mr, FicheTechniques)
    Dim item As New ligneOrdo
    For Each item In coll
        AProduire.Add item
    Next item
Next mr

'Adding and saving the coll AProduire in the RecordSetO
cnc.BeginTrans
Dim item2 As ligneOrdo
For Each item2 In AProduire
    ORecordSet.AddNew
    ORecordSet("DateCommande").Value = item2.DateCommande
    ORecordSet("Produit").Value = item2.Produit
    ORecordSet("Quantite").Value = item2.Quantite
    ORecordSet.Update
Next item2

ORecordSet.Close

cnc.CommitTrans

End Sub

Function CreateOrdoLigne(ligne As ligneOrdo, FT As Collection) As Collection

Dim ordo As New Collection

Dim ligneFT As Variant
'Loop through the receipes
For Each ligneFT In FT
    If ligneFT.Nom = ligne.Produit Then
        Dim AProduire As New ligneOrdo
        AProduire.Produit = ligneFT.Ingredient
        AProduire.DateCommande = ligne.DateCommande
        AProduire.Quantite = ligne.Quantite * ligneFT.Quantite
        ordo.Add AProduire
        If ligneFT.IsComposed = True Then
            Dim ordoList2 As New Collection
            Set ordoList2 = CreateOrdoLigne(AProduire, FT)
            Dim recordOrdo As ligneOrdo
            For Each recordOrdo In ordoList2
                ordo.Add recordOrdo
            Next recordOrdo
            Set ordoList2 = Nothing
        End If
        Set AProduire = Nothing
    End If
Next ligneFT


Set CreateOrdoLigne = ordo

End Function

问题陈述:

运行模块,我得到一个运行时错误28:"堆栈空间",这似乎是在一些研究在如此紧密的环境中使用递归函数的常见事情。问题是,我无法真正优化流程。我正在寻找绕过这个错误或想法的直接方法,以另一种方式解决这个问题。

谢谢大家,

0 个答案:

没有答案