SQL查询适用于访问,但不适用于excel

时间:2018-04-15 17:35:09

标签: vba excel-vba excel

我需要从记录集中获取数据。 SQL查询在MS Access中正常工作并返回预期的值,但是当在VBA Excel中查找相同的查询时,我收到以下错误:

  

没有给出一个或多个必需参数的值

您对此问题发生的原因有什么看法吗?

谢谢。

Philippe-Olivier Roussel

Private Sub CBtype_AfterUpdate()

    Dim strConnexion As String
    Dim connexion As New ADODB.Connection
    strConnexion = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Database & ""
    connexion.Open strConnexion

    Dim rsMarque As New ADODB.Recordset
    Dim seltype As String



    seltype = CBtype.Value


    rsMarque.Open "SELECT DISTINCT tblMarque.marque_nom FROM tblMarque, tblModele WHERE " & _
      " tblMarque.marque_id = tblModele.marque_id AND tblModele.marque_id IN " & _
      " (SELECT DISTINCT tblModele.marque_id FROM tblModele, tblType " & _
      " WHERE tblModele.type_id = tblType.type_id AND tblModele.type_id = " & _
       " (SELECT tblType.type_id FROM tblType WHERE " & _
       " (tblType.type_nom = " & seltype & ")))", connexion, adOpenStatic



    rsMarque.MoveFirst
    With UserForm2.CBmarque
        .Clear
        Do
            .AddItem rsMarque!marque_nom
            rsMarque.MoveNext
        Loop Until rsMarque.EOF
    End With

End Sub

1 个答案:

答案 0 :(得分:0)

此错误消息看起来像是DBMS的输出而不是Excel

我认为你可能在字符串变量之前和之后都缺少撇号。看看

" (tblType.type_nom = '" & seltype & "')))"有效(我假设您要查询的列是varchar类型,因为您将seltype声明为字符串)