访问 2010 VBA 失忆

Access 2010 VBA Lost Memory

我试图在尝试连接几个不同的 table 时追踪我可能抛出指针 (Lost/misdirected) 的位置。请记住,下面的代码部分只是 16,000 行代码的一小部分,但完全独立。

下面是两段代码。

代码的工作原理:

基本上代码应该做的如下......当我按下一个按钮(代码的第一部分)时,几个 table 名称一次传递给一个子名称(代码的第二部分).

更清楚的是,传递了一个 table 名称(第一部分),然后子程序以该 table 名称运行(第二部分)并放入一个名为 table 的 "CymeImportTable",然后 table two 被传递(第一部分)然后子程序再次运行(第二部分)并在 "CymeImportTable" 中附加 table two。我知道我可以将它们放入一个数组中,但也许稍后再说。我只是想让所有的东西都先工作,然后再优化。好的,所以上面的工作正常......有时。

结果:

代码运行并将每个 table 记录放入一个 MEMO 样式的单元格中。对于下面的代码和我的 table 大小,我有大约 4000 条记录,在我的 table 中有几个字段,这些记录被放入一个只有一个字段的 4000 条记录的 CymeImportFile table 中。这有时效果很好,而且似乎在一开始就一直有效,也许是在我的 table 开始变大之前?

问题:

问题是... CymeImportFile table 有时会被填充,但其他时候会填充似乎什么都没有的东西。两种情况下出现的记录数量相同,但有时有文本(我想要的),而其他时候没有任何内容(不是我想要的)。只是带有空白字段的空白记录。

我知道的:

  1. 我知道不推荐使用 MEMO,这可能是我的问题,但我将不得不大量修改我的代码以避免使用备忘录格式,它适合我作为备忘录的需要。
  2. 我的 table 总是包含其中的内容,这个问题不是我的程序没有引入任何内容的结果。
  3. 此问题发生在我的代码的其他几个部分(未显示),但这是 simplest/shortest 部分。我不能 post 其他部分,因为总共有大约 16,000 行代码。
  4. 我知道仅凭我提供的代码可能无法定位错误,但我想看看你们是怎么想的,看看是否有明显的我不知道的地方。我才编程 VBA 大约一个月。

下面的第一部分代码: 我不认为问题出在这里,但我想提供尽可能多的信息。下面的这一部分只是在按下按钮时将 table 名称传递给名为 "Concatenate" 的子程序。请记住,此代码可能未优化。

Private Sub buttonConcatenate_Click()
DoCmd.SetWarnings False
DoCmd.CopyObject , "CymeImportFile", acTable, "Template_CymeImportFile"
'DoCmd.RunSQL "DELETE * FROM CymeImportFile"
DoCmd.SetWarnings True

Dim Table As String
'[GENERAL]
Table = "GENERAL"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckGeneral.Value = 0
'[IMPERIAL]
Table = "IMPERIAL"
Concatenate.Concatenate Table
' [HEADNODES]
Table = "HEADNODES"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckHeadnodes.Value = 0
' [SOURCE]
Table = "SOURCE"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckSource.Value = 0
' [NODE]
Table = "NODE"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckNode.Value = 0
' [LINE_CONFIGURATION]
Table = "LINE_CONFIGURATION"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckLine_Configuration.Value = 0
' [SECTION]
Table = "SECTION"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckSection.Value = 0
' [SWITCH SETTING]
Table = "SWITCH_SETTING"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckSwitchSetting.Value = 0
' [FUSE SETTING]
Table = "FUSE_SETTING"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckFuseSetting.Value = 0
' [RECLOSER SETTING]
Table = "RECLOSER_SETTING"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckRecloserSetting.Value = 0
' [TRANSFORMER SETTING]
Table = "TRANSFORMER_SETTING"
'Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckTransformerSetting.Value = 0
' [SECTIONALIZER SETTING]
Table = "SECTIONALIZER_SETTING"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckSectionalizerSetting.Value = 0
' [REGULATOR SETTING]
Table = "REGULATOR_BYPHASE_SETTING"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckRegulatorSetting.Value = 0
' [SHUNT CAPACITOR SETTING]
Table = "SHUNT_CAPACITOR_SETTING"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckShuntCapacitorSetting.Value = 0
' [INTERMEDIATE NODES]
Table = "INTERMEDIATE_NODES"
Concatenate.Concatenate Table
Form_Cyme_Model_Update.CheckIntermediateNodes.Value = 0
' [RELAY SETTING]
'Table = "RELAY_SETTING"
'Concatenate.Concatenate Table
'Form_Cyme_Model_Update.CheckRelaySetting.Value = 0
Form_Cyme_Model_Update.CheckConcatenate.Value = -1
''Message Box
    MsgBox "Cyme imput has been concatenated into CymeImportFile"

End Sub

下一部分是传递变量(Table 名称)的地方,用于将传递的 table 名称的内容放入 table "CymeInputFile"。

Option Compare Database

Sub Concatenate(Table As String)
  ' Set the database
    Set dbsCyme_Model_Update = CurrentDb
    Dim Field As String

'Count records and fields
    RecordCount = CurrentDb.TableDefs(Table).RecordCount
    fieldCount = CurrentDb.TableDefs(Table).Fields.Count
    RecordCounter = 0
    ReDim fieldArray(0) As String
    ReDim recordArray(0) As String
    ReDim fieldArray(fieldCount - 1) As String
    ReDim recordArray(RecordCount - 1) As String
    Set rst = dbsCyme_Model_Update.OpenRecordset(Table)
    rst.MoveFirst
    Do Until rst.EOF
      'Field data
        fieldCounter = 0
        While fieldCounter < fieldCount - 1
        'Set Recordset
            fieldCounter = fieldCounter + 1
            Field = "Field" & fieldCounter
        'rstGENERAL
            fieldString = rst.Fields(Field)
            fieldArray(fieldCounter - 1) = fieldString
        Wend
        printCounter = 0
        For Each element In fieldArray
            If Not fieldArray(printCounter) = "N/A" Then
               holder = recordArray(RecordCounter)
                If fieldArray(printCounter + 1) = "N/A" Then
                    recordArray(RecordCounter) = holder +         fieldArray(printCounter)
                Else
                recordArray(RecordCounter) = holder + fieldArray(printCounter) + ","
            End If
                printCounter = printCounter + 1
            End If
        Next element
        RecordCounter = RecordCounter + 1
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
    Dim temp As String

'Print to the CymeImportFile
    Set rst = dbsCyme_Model_Update.OpenRecordset("CymeImportFile")
    'rst.MoveFirst
    RecordCounter = 0
        If rst.BOF Then
            rst.AddNew
            rst.Update
            rst.MovePrevious
        Else
            rst.MoveLast
            rst.MoveNext
            rst.AddNew
            rst.Update
            rst.MovePrevious
        End If

    For Each element In recordArray
        rst.Edit
        temp = recordArray(RecordCounter)
        rst!Field1 = temp
        rst.Update
        rst.MoveNext
        If rst.EOF Then
            rst.AddNew
            rst.Update
            rst.MovePrevious
        End If
        RecordCounter = RecordCounter + 1
    Next element
rst.Close
Set rst = Nothing


End Sub

我明白了。所有这些不同的添加和更新混淆了一切。我将其简化为这样...不完全是马丁帕金所说的,但你的评论让我在这个部分和创建我的表格的所有其他部分中尝试我的打印过程。

新代码...

'Print to the CymeImportFile
Dim bob As String
bob = 0
Set rst = dbsCyme_Model_Update.OpenRecordset("CymeImportFile")
RecordCounter = 0
    rst.MoveLast
    rst.AddNew
For Each element In recordArray

    'rst.Edit

    rst.AddNew
    temp = recordArray(RecordCounter)
    rst!Field1 = temp
    rst.Update
    RecordCounter = RecordCounter + 1
Next element
 rst.Close
 Set rst = Nothing
 ReDim fieldArray(0) As String
 ReDim  recordArray(0) As String

 Debug.Print Table
End Sub