MS Excel的VBA – 问题与设置类ADODB.Connection

我正在构build使用ADODB.Connection从其他工作簿导入数据的MS Excel工具。 当我在常规模块中构build解决scheme时,它工作正常,但由于我经常使用这种方法,所以我想构build一个类模块,我可以在其他自动化中使用它。 但是由于我对VBA课程不太灵活,所以我很难克服一个问题。

以下是常规模块的代码:

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 

这是我的class级模块:

 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 

目的是将连接stringvariables传递给相应的属性,然后build立连接。 我在这一行中得到一个错误,我想这不是写在一个正确的方式。

 Set con = Me.ConnSet(cn) 

你能给我一些提示,这个class怎么样?

非常感谢!

你的代码有几个问题。

  1. 类中的属性有getter和setter。 在VBA中,语法是Get for getter,让给setter。 Public property set行将不会执行任何操作。 编辑:在VBA中, set关键字用于将对象指定给variables当然,但是再次,这不是我们在这里讨论的。

  2. 类可以有方法:在类中做些事情的小子程序。 为此,您可以使用Private sub作为只能用于类本身的例程, Public sub用于需要从其他模块访问的例程。 这是你所需要的,而不是所述的属性集。

  3. clsSQL.ConnString = "Data Source=" & strConnString & "; Extended Properties='Excel 12.0; HDR=YES'"被parsing为“数据源=”等,所以这将导致一个不正确的连接string,以“数据源=数据源= …”

除此之外:你应该对class级应该做什么以及你想要做什么有一个非常清楚的认识。 例如,什么应该是可重用的部分。 如果我正确地读取了你的代码,你试图想出一个具有3个属性的类(数据提供者的string,指向文件或数据库连接的string以及string属性)。 接下来,这个类应该把这些东西组合成一个适当的连接string,并为你提供一个连接对象。

整个class级将成为:

 Option Explicit Private p_strProvider As String Private p_strConn As String Private p_strProperties As String Private p_con As ADODB.Connection Private Sub Class_Initialize() 'Use initialization to make sure there's Always the private connection object Set p_con = new ADODB.Connection End Sub Private Sub Class_Terminate() 'Clean up after yourself Set p_con = Nothing End Sub 'Properties needed: Property Let ConnProvider(strCP As String) p_strProvider = strCP End Property Property Let ConnString(strCS As String) p_strConn = strCS End Property Property Let ConnProperties(strCPP As String) p_strProperties = strCPP End Property Private Sub OpenConnection() 'Takes the variables, builds a connectionstring, creates the connection Dim conStr As String If p_strProvider = "" Or p_strConn = "" Or p_strProperties = "" Then MsgBox "Connection parameters were not provided." Exit Sub Else conStr = "Data Source=" & strConn & "; Extended Properties='" & strProperties & "'" With p_con .Provider = strProvider .ConnectionString = conStr .CursorLocation = adUseClient .Open End With End If End Sub Public Function GetConnectionObject() As ADODB.Connection 'Builds and then exposes the connection object from within the class to the outside world OpenConnection Set GetConnectionObject = p_con End Function 

这个类可以在一个模块中使用,如下所示:

Option Explicit Dim clsSQL As clsWbkSQLImport Dim strConnString As String

 Sub Test() Dim clsSQL as clsWbkSQLImport Dim connectionObject as ADODB.Connection strConnString = Sheets("somesheet").Range("RawDataPath") 'I take it this named range holds a filepath? Set clsSQL = New clsWbkSQLImport clsSQL.ConnProvider = "Microsoft.ACE.OLEDB.12.0" clsSQL.ConnString = strConnString clsSQL.ConnProperties = "Excel 12.0; HDR=YES" Set connectionObject = clsSQL.GetConnectionObject 'connectionObject will now hold an open ADODB connection. End Sub 

更多的error handling是可取的。 根据你想要做什么保持暂停等。 除此之外:确实看看Chip Pearson的网站上的课程:)

一个Set属性不会返回任何东西。 它就像是一个使用这样的语句时运行的子Set Me.ConnSet = cnSet Me.ConnSet = cn

这将传递'cn'作为您的Property Set ConnSet(cn As ADODB.Connection)

这里有更多关于类模块,包括SetLetGet属性: VBA中的类:Chip Pearson