I am building MS Excel tool which imports the data from other workbooks using ADODB.Connection. It works fine when I build the solution in regular modules but as I am using this method quite frequently I wanted to build a class module which I can use in other automations. But as I am not flexible with VBA classes I struggle with overcoming one problem.
Here is the code from the regular module:
Option Explicit
Dim clsSQL As clsWbkSQLImport
Dim strConnString As String
Private Sub btnImportData_Click()
strConnString = Me.Range("RawDataPath")
Set clsSQL = New clsWbkSQLImport
clsSQL.ConnProvider = "Microsoft.ACE.OLEDB.12.0"
clsSQL.ConnString = "Data Source=" & strConnString & "; Extended Properties='Excel 12.0; HDR=YES'"
clsSQL.ConnProperties = "Excel 12.0; HDR=YES"
clsSQL.SetConnection
End Sub
And here is my class module:
Option Explicit
Private strProvider As String
Private strConn As String
Private strProperties As String
Private con As ADODB.Connection
Property Let ConnProvider(strCP As String)
strProvider = strCP
End Property
Property Let ConnString(strCS As String)
strConn = strCS
End Property
Property Let ConnProperties(strCPP As String)
strProperties = strCPP
End Property
Property Set ConnSet(cn As ADODB.Connection)
With cn
.Provider = strProvider
.ConnectionString = "Data Source=" & strConn & "; Extended Properties='" & strProperties & "'"
.CursorLocation = adUseClient
.Open
End With
Set con = cn
End Property
Sub SetConnection()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
If strProvider = "" Or strConn = "" Or strProperties = "" Then
MsgBox "Connection parameters were not provided."
Exit Sub
Else
Set con = Me.ConnSet(cn)
End If
End Sub
The purpose is to transfer connection string variables to respective properties and then establish a connection. I am getting an error in this line and I guess it is not written in a correct way.
Set con = Me.ConnSet(cn)
Can you give me some hints how this class should look like?
Many thanks!