简体   繁体   English

VBA - 使用 Typeof ... 是 ADODB.Recordset 导致编译错误

[英]VBA - Using Typeof ... Is ADODB.Recordset Results in Compile Error

I am building a function with a set of supporting sub-functions to create ADOX.Catalog objects to help me build automation for Access database generation.我正在构建一个带有一组支持子功能的函数来创建 ADOX.Catalog 对象,以帮助我构建 Access 数据库生成的自动化。

I like to use late-binding for my applications because my user base doesn't always have the same version of office applications, so I can't always rely on them having the same versions of the libraries I'm calling.我喜欢对我的应用程序使用后期绑定,因为我的用户群并不总是拥有相同版本的办公应用程序,所以我不能总是依赖他们拥有我正在调用的相同版本的库。

My public function accepts several objects as parameters, but I need to make sure they're actually ADODB.Recordset objects before I start processing them.我的公共函数接受多个对象作为参数,但在开始处理它们之前,我需要确保它们实际上是 ADODB.Recordset 对象。 I referred to the msdn article at https://msdn.microsoft.com/en-us/library/s4zz68xc.aspx to get started, and I'm trying to use If TypeOf ... Is ADODB.Recordset per the article's recommendation, but it generates the following error:我参考了https://msdn.microsoft.com/en-us/library/s4zz68xc.aspx上的 msdn 文章开始,我正在尝试使用If TypeOf ... Is ADODB.Recordset根据文章的建议,但它会产生以下错误:

Compile error:
User-defined type not defined

Here is a snippet of my code.这是我的代码片段。 The first offending line is TypeOf adoRsColumns Is ADODB.Recordset :第一行是TypeOf adoRsColumns Is ADODB.Recordset

Public Function ADOX_Table_Factory( _
ByVal strTblName As String, _
Optional ByVal adoRsColumns As Object, _
Optional ByVal adoRsIndexes As Object, _
Optional ByVal adoRsKeys As Object _
) As Object

'Init objects/variables.
Set ADOX_Table_Factory = CreateObject("ADOX.Table")

'Begin interactions with the new table object.
With ADOX_Table_Factory
    .Name = strTblName

    'Check if we've received an ADO recordset for the column(s).
    If TypeOf adoRsColumns Is ADODB.Recordset Then
        'Check that the recordset contains rows.
        If Not (adoRsColumns.BOF And adoRsColumns.EOF) Then
            'Loop through the column definitions.
            Do
                .Columns.Append ADOX_Column_Factory(adoRsColumns.Fields(0), adoRsColumns.Fields(1), adoRsColumns.Fields(2), adoRsColumns.Fields(3))
            Loop Until adoRsColumns.EOF
        End If
    End If

My Googling has not yielded any results that have helped me get around this error.我的谷歌搜索没有产生任何帮助我解决这个错误的结果。 I have confirmed this code works if I set a reference to the ADO library.如果我设置了对 ADO 库的引用,我已经确认此代码有效。 I have also confirmed, via the TypeName function, that the objects are identified by name as Recordset .我还通过TypeName函数确认对象通过名称标识为Recordset If I replace TypeOf adoRsColumns Is ADODB.Recordset with TypeOf adoRsColumns Is Recordset , however, then the test evaluates false and the desired code doesn't execute.但是,如果我将TypeOf adoRsColumns Is ADODB.Recordset替换为TypeOf adoRsColumns Is Recordset ,则测试评估为 false 并且不会执行所需的代码。 I haven't resorted to a string comparison to TypeName 's output because, as stated in the MSDN article, TypeOf ... Is is faster.我没有对TypeName的输出进行字符串比较,因为正如 MSDN 文章中所述, TypeOf ... Is更快。

Thanks in advance for any assistance!在此先感谢您的帮助!

Just to recap, without an ADO reference included in your project, you get a compile error at this line:回顾一下,如果您的项目中没有包含 ADO 引用,您会在这一行收到编译错误:

If TypeOf adoRsColumns Is ADODB.Recordset Then

Without the reference, VBA doesn't recognize ADODB.Recordset The situation is basically the same as if you tried to declare Dim rs As ADODB.Recordset without the reference.如果没有引用,VBA 无法识别ADODB.Recordset情况与您尝试在没有引用的情况下将Dim rs As ADODB.Recordset声明为ADODB.Recordset的情况基本相同。 That declaration would trigger the same compile error.该声明将触发相同的编译错误。

There is no way to use ADODB.Recordset as a recognized type without the reference.如果没有引用,就无法将ADODB.Recordset用作可识别的类型。

As an alternative approach, you could create a custom function to check whether the object supports a method or property which is available in an ADODB.Recordset but not in a DAO.Recordset作为替代方法,您可以创建一个自定义函数来检查对象是否支持在ADODB.Recordset可用但在DAO.Recordset不可用的方法或属性

This one checks whether the recordset includes a Supports method.这个检查记录集是否包含Supports方法。 That method is available in an ADODB but not DAO Recordset .该方法在ADODB可用,但在DAO Recordset不可用。

Public Function IsAdoRecordset(ByRef pObject As Object) As Boolean
    Const adAddNew As Long = 16778240
    Dim lngTmp As Long
    Dim blnReturn As Boolean
    Dim strMsg As String

On Error GoTo ErrorHandler

    blnReturn = False
    If TypeName(pObject) = "Recordset" Then
        lngTmp = pObject.Supports(adAddNew)
        blnReturn = True
    End If

ExitHere:
    On Error GoTo 0
    IsAdoRecordset = blnReturn
    Exit Function

ErrorHandler:
    Select Case Err.Number
    Case 438  ' Object doesn't support this property or method
        ' leave blnReturn = False
    Case Else
        ' notify user about any other error
        strMsg = "Error " & Err.Number & " (" & Err.Description _
            & ") in procedure IsAdoRecordset"
        MsgBox strMsg
    End Select
    Resume ExitHere

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM