excel - 运行时错误 '28' : Out of stack space in Excel VBA and Excel crashes

标签 excel vba

我能够运行代码并建立连接,但是当我尝试使用 VBA 从 Excel 将数据插入 MySQL 数据库时。它显示'28:堆栈空间不足'

我有一个代码,这是这里的数据

Name     Analyst     Method    Numsample   Condition
AAA       AAA        AAA         2           ABC

下面是我写的代码,


Dim oConn As ADODB.Connection
Private Sub ConnectDB()

   On Error GoTo ErrHandle

   Set oConn = New ADODB.Connection
    oConn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _
        "SERVER=******************t-1.rds.amazonaws.com;" & _
        "DATABASE=worksheet;" & _
        "USER=***;" & _
        "PASSWORD=****;" & _
        "Option=3"

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
    ConnectDB
    With wsBooks
        For rowCursor = 2 To 3
            strSQL = "INSERT INTO TestExperiment (Experiment_Name, Experiment_Method, Experiment_Analyst, Experiment_NumSample, Experiment_condition) " & _
                "VALUES ('" & esc(.Cells(rowCursor, 1)) & "', " & _
                "'" & esc(.Cells(rowCursor, 2)) & "', " & _
                "'" & esc(.Cells(rowCursor, 3)) & "', " & _
                "'" & esc(.Cells(rowCursor, 4)) & "', " & _
                esc(.Cells(rowCursor, 5)) & ")"
            rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic
        Next
    End With
    MsgBox "Connection successful"

ExitHandle:
   Set con = Nothing  ' RELEASE ALL set OBJECTS
   Exit Sub

ErrHandle:
   MsgBox Err.Number & ": " & Err.Description
   Resume ExitHandle
    End Sub

Function esc(txt As String)
    esc = Trim(Replace(txt, "'", "\'"))
End Function



对 VBA 完全陌生,只是学习代码可能会有一些错误。如果可以的话,请帮助我。

最佳答案

“堆栈空间不足”是 VBA 的 StackOverflowException .这意味着您无意中编写了递归逻辑,该逻辑永远不会逃脱递归,不断挖掘,并深入挖掘,......直到调用堆栈完全填满并且 VBA 无法再跟踪返回的位置。

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
    ConnectDB '<~ recursive call!
    With wsBooks

去掉递归调用,问题解决!

关于excel - 运行时错误 '28' : Out of stack space in Excel VBA and Excel crashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58772047/

相关文章:

VBA SumIfs 不适用于日期范围

vba - 如何获取表单的最后一条记录ID?

excel - 在有或没有合并单元格的情况下可靠地获取 Excel 中的最后一列

VBA:Str(myString) 与 Str(CDbl(myString)) 的作用相同吗?

matlab - 如何使用Excel VBA通​​过matlab在Excel中制作页眉和页脚

arrays - 我正在尝试通过 for each 循环将值添加到数组中

php - Webkit 和 Excel 文件(PHPexcel)

ms-access - ADODB 记录集列标题

excel - 在 Excel Online 中,OfficeJS API 不再将 host_Info_ 参数传递给 Excel 加载项

VBA 编辑器提示缺少对象