title: ms access vba "Run-time error '3061'. Too few parameters. Expected 7" when querying access query
body: <p>I google a lot and read all post here related to this issue but found nothing that could give an explanation or help me to resolve this issue.
  Following here, a function which work great when the &quot;TableName&quot; parameter is a base table but raise error when it is an ms access view (query). I found nothing yet that could explain this issue as many access query already refer to such views (queries) without issues.</p>
  <pre><code>Function DBDistinctCount(FieldName As String, tableName As String) As Long
  Dim rs As Recordset, curDb As Database, strSql As String
  
      On Error GoTo ERR_Handling
      Set curDb = CurrentDb
      'strSql = &quot;SELECT COUNT(PR.[&quot; &amp; FieldName &amp; &quot;]) AS CNT FROM (SELECT [&quot; &amp; FieldName &amp; &quot;] FROM &quot; &amp; TableName &amp; &quot; GROUP BY [&quot; &amp; FieldName &amp; &quot;]) AS PR;&quot;
      strSql = &quot;SELECT COUNT(PR.&quot; &amp; FieldName &amp; &quot;) AS CNT FROM (SELECT &quot; &amp; FieldName &amp; &quot; FROM &quot; &amp; tableName &amp; &quot; GROUP BY &quot; &amp; FieldName &amp; &quot;) AS PR;&quot;
      'strSql = &quot;SELECT COUNT([&quot; &amp; FieldName &amp; &quot;]) AS CNT FROM (SELECT [&quot; &amp; FieldName &amp; &quot;] FROM [&quot; &amp; TableName &amp; &quot;] GROUP BY [&quot; &amp; FieldName &amp; &quot;]);&quot;
      
      'Debug.Print result: SELECT COUNT(PR.ID_Projet) AS CNT FROM (SELECT ID_Projet FROM R_CompilationProjet GROUP BY ID_Projet) AS PR
      
  '    Dim qdf As DAO.QueryDef
  '    Set qdf = curDb.CreateQueryDef(vbNullString, strSql)
      
      Set rs = curDb.OpenRecordset(strSql)
  '    Set rs = qdf.OpenRecordset(dbOpenSnapshot)
      
      DBDistinctCount = Nz(rs.Fields(&quot;CNT&quot;), 0)
      
  ERR_Handling:
      If Err.Number &lt;&gt; 0 Then
          Dim mess As String
          mess = &quot;Erreur vba &quot; &amp; Err.Number &amp; &quot; : &quot; &amp; Err.Description
          On Error Resume Next
          Call DBHelper.AddLog(mess, &quot;DBDistinctCount&quot;)
      End If
      
      If Not rs Is Nothing Then rs.Close
      Set rs = Nothing
      
      If Not curDb Is Nothing Then curDb.Close
      Set curDb= Nothing
      
  End Function
  </code></pre>
  <p>As you can see, I messed up the function a bit in order to find out what could be wrong. I even tried to use a querydef with the same result. I should mention that I've tried to put the resulting sql string itself inside an access query to see exactly the expected result when I ran the query. Any advice would be greatly appreciated.</p>