Access 2010中的VBA - 运行时错误430

时间:2013-12-06 17:23:52

标签: sql sql-server vba ms-access listbox

我收到运行时错误'430':类不支持自动化或不支持此代码行Set Me.lstResults.Recordset = rs或此Set Me![frmM_SearchForDocumentsSubForm].Form.Recordset = rs上的预期接口。我正在尝试获取基于SQL存储过程的ADO Recordset,以显示在Access表单的未绑定列表框或子表单中。我在Win 7计算机上使用Access 2010连接到SQL Server 2008:

On_Click事件:

Private Sub cmdRun_Click()
'On Error Resume Next

    Dim strSQL As String

    'Stored procedure + parameters called from form
    strSQL = "Exec sqlsp_searchalltables " & Me.txtTables & _
       ", " & "'%" & Me.txtSearchTerm & "%'"

    OpenMyRecordset rs, strSQL

    'debug - view procedure
    Me.lblQuery.Caption = strSQL
    Me.Repaint

    Set Me.lstResults.Recordset = rs
    'or this
    'Set Me![frmM_SearchForDocumentsSubForm].Form.Recordset = rs
End Sub

我在网上发现了一些针对此错误的解决方案并尝试了所有这些解决方案无济于事。大多数建议检查我所做的参考并进行验证。

当我使用DAO Querydef和直通查询或者我使用这个.listbox方法时,我能够成功连接到SQL服务器并在Listbox和Subform中显示结果:

With Me.lstResults
    Do
       strItem = rs.Fields("CLIENT_ID").Value
       .AddItem strItem
       rs.MoveNext
    Loop Until rs.EOF
End With

我不想使用DAO方法,因为我发现我需要ADO的编码灵活性,特别是在SQL中连接多个Recordset时。想法?

仅供参考:我在模块中的OpenMyRecordset公共功能:

Option Compare Database
Option Explicit

Global con As New ADODB.Connection
Global rs As ADODB.Recordset
Global NoRecords As Boolean

Public Enum rrCursorType
    rrOpenDynamic = adOpenDynamic
    rrOpenForwardOnly = adOpenForwardOnly
    rrOpenKeyset = adOpenKeyset
    rrOpenStatic = adOpenStatic
End Enum

Public Enum rrLockType
    rrLockOptimistic = adLockOptimistic
    rrLockReadOnly = adLockReadOnly
End Enum

Public Function OpenMyRecordset(rs As ADODB.Recordset, strSQL As String, Optional rrCursor As rrCursorType, _
    Optional rrLock As rrLockType, Optional bolClientSide As Boolean) As ADODB.Recordset

    If con.STATE = adStateClosed Then
        con.ConnectionString = "ODBC;Driver={SQL Server};Server=mysqlsvr;DSN=RecordsMgmt_SQLDB;UID=XXX;Trusted_Connection=Yes;DATABASE=RecordsManagementDB;"
        con.Open
    End If

    Set rs = New ADODB.Recordset
    With rs
        .ActiveConnection = con
            .CursorLocation = adUseClient
        .CursorType = IIf((rrCursor = 0), adOpenDynamic, rrCursor)
        .LockType = IIf((rrLock = 0), adLockOptimistic, rrLock)
        .Open strSQL
        If .EOF And .BOF Then
            NoRecords = True
            Exit Function
        End If
    End With

End Function

1 个答案:

答案 0 :(得分:1)

您绝对不必执行循环方法来填充列表框。我不熟悉您使用的OpenMyRecordset命令,但我怀疑其功能中的某些内容是导致此错误的原因(即,它没有以与此方式兼容的方式打开记录集。列表框)。这是我连接到SQL Server Express的本地实例并能够填充列表框的方式。

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cn = New ADODB.Connection

With cn
    .ConnectionString = _
        "Provider=SQLOLEDB;Data Source=localhost\SQLEXPRESS;" & _
        "Initial Catalog=Northwind;Trusted_Connection=yes"
    .Open
End With

Set rs = New ADODB.Recordset

With rs
    Set .ActiveConnection = cn
    .Source = "SELECT FirstName, LastName FROM Employees"
    .LockType = adLockOptimistic
    .CursorType = adOpenKeyset
    .Open
End With

Set Me.lstTest.Recordset = rs

Set rs = Nothing
Set cn = Nothing

您必须确保在项目中启用了Microsoft ActiveX数据对象库参考。