尝试在窗体上填充组合框时获取运行时错误3265

时间:2017-04-17 14:30:04

标签: vba access-vba

我正在尝试使用来自传递查询的数据填充表单中的组合框,但仍然得到"运行时错误3265"。

以下是与表单相关联的VBA:



Private Sub Form_Open(Cancel As Integer)
    Call populatePlantsComboBox
End Sub

Private Function populatePlantsComboBox()

    Dim SQLstring As String, QryName As String
    
    'build query string
    SQLstring = "SELECT w.PlantID, w.PlantName FROM dbo.Warehouses w " & _
    "LEFT OUTER JOIN (SELECT DISTINCT CompanyID, PlantID FROM dbo.PlantCapacityFactors) c " & _
    "ON w.CompanyID = c.CompanyID and w.PlantID = c.PlantID " & _
    "WHERE w.CapacityPerWeek IS NOT NULL and c.PlantID IS NULL;"
    
    'assign a name to the query
    QryName = "Plants"
    
    If PTQ(SQLstring, QryName) = True Then Me.cboPlantID.RowSource = QryName
    
End Function




这是位于模块中的传递查询功能:



Public Function PTQ(ByVal SQL As String, Optional ByVal QueryName As String)
    'purpose of this function is to run a pass through query (PTQ)
    'got this code from http://www.dbforums.com/showthread.php?1006185-Writing-Pass-Through-Queries-in-VBA
    Dim connect As String
    connect = "ODBC;DSN=AP Readers;Description=AP Database App Connection;UID=sa;DATABASE=AP;Trusted_Connection=Yes"

    Dim dbs As DAO.Database
    Dim qdf As DAO.QueryDef
    
    Set dbs = CurrentDb
    Set qdf = dbs.CreateQueryDef
    With qdf
        .Name = QueryName
        .connect = connect
        .SQL = SQL
        .ReturnsRecords = (Len(QueryName) > 0)
        If .ReturnsRecords = False Then
            .Execute
        Else
            If Not IsNull(dbs.QueryDefs(QueryName).Name) Then dbs.QueryDefs.Delete QueryName
            dbs.QueryDefs.Append qdf
        End If
        .Close
    End With
    Set qdf = Nothing
    Set dbs = Nothing
    
End Function




打开表单时发生错误。在调试时,它会突出显示此行If Not IsNull(dbs.QueryDefs(QueryName).Name) Then dbs.QueryDefs.Delete QueryName。为什么这不起作用?

0 个答案:

没有答案