简体   繁体   English

运行时错误“28”:Excel VBA 和 Excel 崩溃中的堆栈空间不足

[英]Runtime error '28': Out of stack space in Excel VBA and Excel crashes

I am able to run the code and build a connection but when I am trying to insert data into a MySQL Database from Excel using VBA.我能够运行代码并建立连接,但是当我尝试使用 VBA 从 Excel 将数据插入 MySQL 数据库时。 It shows '28: Out of Stack space'它显示'28:堆栈空间不足'

I have a code and this is the data here我有一个代码,这是这里的数据

Name     Analyst     Method    Numsample   Condition
AAA       AAA        AAA         2           ABC

Below is the code I have written,下面是我写的代码,


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


Completely new to VBA, just learning code might have some errors. VBA全新,刚学代码可能会有一些错误。 Please help me if you can thanks.如果可以的话,请帮助我。

"Out of stack space" is VBA's StackOverflowException . “堆栈空间不足”是 VBA 的StackOverflowException It means you have inadvertently written recursive logic that never escapes the recursion, keeps digging, and digging deeper, ....until the call stack is completely filled and VBA can't track where to return anymore.这意味着您无意中编写了递归逻辑,该逻辑永远不会逃脱递归,不断挖掘并深入挖掘,......直到调用堆栈完全填满并且 VBA 无法再跟踪返回的位置。

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

Remove the recursive call, problem solved!去掉递归调用,问题解决!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM