Object variable or with a block variable not set when returning from a function
Why am I getting an Object variable or with a block variable does not set an error with the following code:
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 declared the object to be "cn", initialized it correctly, and then set some properties and opened it before returning it.
I am getting error on the GetConnection = cn line.
+3
Nightwolf
source
to share
1 answer
If memory suits me ... you need to use the 'set' keyword when working with reference types (objects) in classic vb
t
Set GetConnection = cn
This applies to all assignments, not just return statements.
+9
Mike gasparelli
source
to share