我能够运行代码并建立连接,但是当我尝试使用 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/