我正在构建MS Excel工具,它使用ADODB.Connection从其他工作簿导入数据。当我在常规模块中构建解决方案时它工作正常,但由于我经常使用这种方法,我想构建一个类模块,我可以在其他自动化中使用它。但由于我对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
这是我的课程模块:
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
目的是将连接字符串变量传输到各个属性,然后建立连接。我在这一行收到错误,我猜它不是以正确的方式写的。
Set con = Me.ConnSet(cn)
你能给我一些关于这个课程应该是什么样子的提示吗?
非常感谢!
答案 0 :(得分:2)
您的代码存在一些问题。
类中的属性具有getter和setter。在VBA中,语法是Get for getter和Let for setter。 Public property set
行不会做任何事情。编辑:在VBA中,set
关键字用于将对象分配给变量,当然,这不是我们在这里讨论的内容。
类可以有方法:在类中执行某些操作的小子例程。为此,您可以将Private sub
用于仅适用于类本身的例程,并将Public sub
用于需要可从其他模块访问的例程。这就是你需要的,而不是所提到的属性集。
行clsSQL.ConnString = "Data Source=" & strConnString & "; Extended Properties='Excel 12.0; HDR=YES'"
被解析为"数据源="等等,这将导致一个错误的连接字符串,以"数据源=数据源= ..."
除此之外:你应该非常清楚地知道班级应该做什么以及你想做什么。例如。什么应该是可重复使用的部分。如果我正确地读取了您的代码,您将尝试创建一个具有3个属性的类(数据提供程序的字符串,指向要连接的文件或数据库的字符串以及字符串属性)。 接下来,类应该将这些内容组合成一个正确的连接字符串,并为您提供一个连接对象。
整个班级将成为:
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
此类可以在模块中使用,如下所示:
选项明确 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
建议进行更多错误处理。根据您的尝试,记住超时等。除此之外:确实查看Chip Pearson的网站上的课程:)
答案 1 :(得分:1)
Set
属性不会返回任何内容。它类似于使用如下语句时运行的子:Set Me.ConnSet = cn
这将通过' cn'作为Property Set ConnSet(cn As ADODB.Connection)
以下是有关类模块的更多信息,包括Set
,Let
和Get
属性:
Classes in VBA: Chip Pearson