Why am I receiving an Object variable or With block variable not set 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've declared the object as 'cn', initialized it properly, and am then setting some properties and opening it, before returning it.
I get the error at the GetConnection = cn line.