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
source to share