错误对象不支持命名参数VBA

时间:2018-01-23 08:22:28

标签: excel vba excel-vba

我今天刚尝试编写VBA代码,这就是我自己的目标:

SQLConnection模块

Function connectDB(ByVal sqldbname As String) As ADODB.Connection
Dim objMyConn As ADODB.Connection
Set objMyConn = New ADODB.Connection
Select Case sqldbname
    Case Is = "MYDB"
        objMyConn.ConectionString = "Provider=SQLOLEDB;Data Source=servername;Initial Catalog=db_name;User ID=dbadmin;Password=dbpwd;"
End Select
objMyConn.Open 
End Function

Sheet1对象

Sub main()
'MsgBox "WELCOME DUCK!"
Dim objMyConn As ADODB.Connection
Set objMyConn = SQLConnection.connectDB("MYDB") 
End Sub

我总是得到"对象不支持命名参数"错误消息 请帮忙解决这个错误。

thanksyou

- 放入Option Explicit(Sub main未定义)后

Option Explicit

Sub main()
'MsgBox "WELCOME DUCK!"
Dim objMyConn As ADODB.Connection
Set objMyConn = SQLConnection.connectDB("MYDB")
Set objMyRecordset = SQLConnection.getdata(objMyConn, "SELECT * FROM MYTBL")
End Sub

- getdata函数

Function getdata(ByVal objMyConn As ADODB.Connection, ByVal strSQL As String) As ADODB.Recordset
Dim objMyRecordset As ADODB.Recordset
Set objMyRecordset = New ADODB.Recordset
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
Set getdata = objMyRecordset
End Function

0 个答案:

没有答案