Compile Error: Expected function or variable in VBA for access

ぐ巨炮叔叔 提交于 2019-12-24 10:44:10

问题


I know near nothing about VBA, but I'm trying to modify an application to connect to a MySQL Database.

The following code produces a Compile error at rstProjets.Open and I can't seem to find why.

Public mysqlConn As ADODB.Connection

Private Sub cmdUpdate_Click()
Dim rstProjets As ADODB.Recordset
ConnectMySQL
Set rstProjets = rstProjets.Open("SELECT * FROM subventions LIMIT 5", mysqlConn)
With rstProjets
    If Not .EOF And Not .BOF Then
        .MoveFirst
        Do While Not .EOF
        MsgBox "Subventions:" & rstProjets![pin], , "Subvention ajoutée"
        .MoveNext
        Loop
    Else
        MsgBox "Aucune données à mettre à jour !", , "LVMB"
    End If 
    .Close
End With
mysqlConn.Close
End Sub

Private Sub ConnectMySQL()
Set mysqlConn = New ADODB.Connection
mysqlConn.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver};" & _
    "SERVER=127.0.0.1;" & _
    "DATABASE=database;" & _
    "USER=root;" & _
    "PASSWORD=;" & _
    "Option=0"
End Sub

回答1:


Set your rstProjets object variable to a New ADODB.Recordset, and then call its .Open method.

Dim rstProjets As ADODB.Recordset
ConnectMySQL
Set rstProjets = New ADODB.Recordset
rstProjets.Open "SELECT * FROM subventions LIMIT 5", mysqlConn


来源:https://stackoverflow.com/questions/28816518/compile-error-expected-function-or-variable-in-vba-for-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!