Why am I receiving an Object variable or With block variable not set error with the following code:
为什么我使用以下代码接收Object变量或With block变量未设置错误:
Function GetConnection() As ADODB.Connection
'Create connection to worksheet
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.Provider = "Microsoft.Jet.OLEDB.4.0"
cn.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & "Extended Properties=Excel 8.0;"
cn.Open
GetConnection = cn
End Function
I've declared the object as 'cn', initialized it properly, and am then setting some properties and opening it, before returning it.
我已经将对象声明为'cn',正确初始化它,然后在返回之前设置一些属性并打开它。
I get the error at the GetConnection = cn line.
我在GetConnection = cn行收到错误。
1 个解决方案
#1
9
If memory serves me right... you need to use the 'set' keyword when working with reference types (objects) in classic vb
如果内存对我有用......在使用经典vb中的引用类型(对象)时,需要使用'set'关键字
ie:
即:
Set GetConnection = cn
This applies to all assignments, not just function return statements.
这适用于所有赋值,而不仅仅是函数返回语句。
#1
9
If memory serves me right... you need to use the 'set' keyword when working with reference types (objects) in classic vb
如果内存对我有用......在使用经典vb中的引用类型(对象)时,需要使用'set'关键字
ie:
即:
Set GetConnection = cn
This applies to all assignments, not just function return statements.
这适用于所有赋值,而不仅仅是函数返回语句。