错误3001 Excel VBA MySQL选择

时间:2015-07-02 17:15:14

标签: mysql excel vba excel-vba

我尝试使用Excel宏在MySQL数据库中选择行。连接似乎正常,但我收到VBA 3001错误

  

(Microsoft visual basic 3001参数类型错误,或者超出可接受的范围,或者彼此冲突)

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim oConn As ADODB.Connection
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.2 Unicode Driver};" & _
    "SERVER=localhost;" & _
    "DATABASE=talar;" & _
    "USER=root;" & _
    "PASSWORD=root;" & _
    "Option=3"         
strSQL = "SELECT * FROM ots where Estado in (2,3)"   
rs.Open SQLStr, Cn, adOpenForwardOnly, adLockReadOnly
Dim myArray()
myArray = rs.GetRows()
oConn.Close
安装MySQL ODBC 5.2 Unicode驱动程序,MySQL服务运行正常,我使用的是Excel 2010,Windows 7.任何人都有任何想法?谢谢!

1 个答案:

答案 0 :(得分:0)

你实际上遇到了两个问题,这两个问题都更容易被发现你使用Option Explicit

两者都在这一行:

rs.Open SQLStr, Cn, adOpenForwardOnly, adLockReadOnly

您的SQL字符串变量为strSQL,您的连接对象为oConn。因此,将代码行更改为:

rs.Open strSQL, oConn, adOpenForwardOnly, adLockReadOnly