Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用ADODB记录集不会';当Access中的同一查询执行时,不返回值_Sql_Excel_Vba_Ms Access_Adodb - Fatal编程技术网

Sql 使用ADODB记录集不会';当Access中的同一查询执行时,不返回值

Sql 使用ADODB记录集不会';当Access中的同一查询执行时,不返回值,sql,excel,vba,ms-access,adodb,Sql,Excel,Vba,Ms Access,Adodb,我在使用ADODB记录集使用EXCEL VBA时遇到了一个令人沮丧的问题。我有一个直接在Access中正常工作的查询,但是当我把它放在VBA中时,记录集总是返回null。我还有其他查询可以正常工作,但是这种类型的查询总是无法在VBA中返回结果,即使它在Access中工作 Function GetDBProjCount(sCharIn As String, sFldIn As String, Optional sYrIn As Integer = -1) As Integer Dim

我在使用ADODB记录集使用EXCEL VBA时遇到了一个令人沮丧的问题。我有一个直接在Access中正常工作的查询,但是当我把它放在VBA中时,记录集总是返回null。我还有其他查询可以正常工作,但是这种类型的查询总是无法在VBA中返回结果,即使它在Access中工作

   Function GetDBProjCount(sCharIn As String, sFldIn As String, Optional sYrIn As Integer = -1) As Integer
   Dim iResult As Integer
   Dim sQryString As String
   Dim sSearchChar As String
   Dim pQryParam As Object
   Dim sParamValA As String
   Dim sParamValB As String
   Dim iParamLenA As Integer
   Dim iParamLenB As Integer

   iResult = 0
   Call MakeDBConnection
   Set pQryParam = CreateObject("ADODB.Parameter")
   If CInt(sYrIn) > 0 Then
      If ((CInt(sYrIn) > 10) And (CInt(sYrIn) < 50)) Then
         sYrIn = Right(sYrIn, 2)
      ElseIf ((CInt(sYrIn) > 2010) And (CInt(sYrIn) < 2050)) Then
         sYrIn = Right(sYrIn, 2)
      Else
         sYrIn = -1
      End If
   End If

   If sYrIn < 0 Then
      sQryString = "SELECT Count(*) FROM " & tblProjList & " WHERE Left(" & sFldIn & ", 1)=?;"
      sParamValA = sCharIn
      sParamValB = ""
      iParamLenA = 1
      iParamLenB = 1
   Else
      sQryString = "SELECT Count(*) FROM (Select * from [" & tblProjList & "] WHERE [" & garProjListFlds(4, 1) & "] Like ?) WHERE Left([" & sFldIn & "],1)=?;"
      'sQryString = "SELECT Count(*) FROM [" & tblProjList & "] WHERE ((Left([" & sFldIn & "], 1)= ? ) AND ([" & garProjListFlds(4, 1) & "] LIKE ?));"
      sParamValA = "*19-*"   'comment "'*" & CStr(sYrIn) & "-*'"
      sParamValB = sCharIn
      iParamLenA = 8
      iParamLenB = 1
   End If

   If QDebugMode Then Debug.Print "GetDBProjCountA: " & sQryString

   With goDBCmd
      .ActiveConnection = goDBConn
      .CommandText = sQryString
      .CommandType = adCmdText
      Set pQryParam = .CreateParameter("ParamA", adChar, , iParamLenA, sParamValA)
      .Parameters.append pQryParam
      If sYrIn > 0 Then
         Set pQryParam = .CreateParameter("ParamB", adChar, , iParamLenB, sParamValB)
         .Parameters.append pQryParam
      End If
      Set goDBRecSet = .Execute
   End With

   If QDebugMode Then Debug.Print ("GetDBProjCountB:  Parameters:   A: " & sParamValA & " B: " & sParamValB)
   Dim msg, fld
   For Each fld In goDBRecSet.Fields
      msg = msg & fld.Value & "|"
   Next
   If QDebugMode Then Debug.Print ("GetDBProjCountC:  Result: " & msg)

   GetDBProjCount = goDBRecSet.Fields(0)
   Call CloseDBConnection
End Function
下面是负责执行查询的函数。它允许我在ProjList表上进行计数,并在类似的函数中进行求和。在不使用年份限制标志(sYrIn=-1)的情况下使用第一个样式查询,并按预期工作

第二种类型的查询是当year in标志包含一个值以将计数或总和限制到特定年份时。我们的项目ID的格式为X99-999,其中第一个位置表示项目类型,接下来的两位数字表示项目开始的年份,最后三位数字是唯一的递增数字,每年重置为001

这里有许多行仅用于调试反馈。在下面的代码中,我包含了一些调试输出

我知道我们要问一个特定的问题,但我有四个与这种情况和代码示例紧密相关的帖子,我不认为这四篇如此紧密相关的帖子是一件好事。我的问题是关于第二组查询。他们从不在VBA中工作。
1)在这种情况下,我是否读取了错误的记录集?
2)以这种方式执行的查询的“复杂性”是否有一些限制?
3)为什么第一个查询返回运行,第二个查询返回不运行?
4)是否有更好的方法查看实际查询执行结果?

同样,所有查询都在Access中工作。
注意:由于实际代码使用的是当前年份,并且没有足够的2020年数据使结果有意义(0是0!),因此显示的代码对年份搜索进行了硬编码。
对于第二种类型,还有两个查询。其中一个被注释掉了——它在VBA中遇到了同样的失败,在Access中也遇到了同样的成功

   Function GetDBProjCount(sCharIn As String, sFldIn As String, Optional sYrIn As Integer = -1) As Integer
   Dim iResult As Integer
   Dim sQryString As String
   Dim sSearchChar As String
   Dim pQryParam As Object
   Dim sParamValA As String
   Dim sParamValB As String
   Dim iParamLenA As Integer
   Dim iParamLenB As Integer

   iResult = 0
   Call MakeDBConnection
   Set pQryParam = CreateObject("ADODB.Parameter")
   If CInt(sYrIn) > 0 Then
      If ((CInt(sYrIn) > 10) And (CInt(sYrIn) < 50)) Then
         sYrIn = Right(sYrIn, 2)
      ElseIf ((CInt(sYrIn) > 2010) And (CInt(sYrIn) < 2050)) Then
         sYrIn = Right(sYrIn, 2)
      Else
         sYrIn = -1
      End If
   End If

   If sYrIn < 0 Then
      sQryString = "SELECT Count(*) FROM " & tblProjList & " WHERE Left(" & sFldIn & ", 1)=?;"
      sParamValA = sCharIn
      sParamValB = ""
      iParamLenA = 1
      iParamLenB = 1
   Else
      sQryString = "SELECT Count(*) FROM (Select * from [" & tblProjList & "] WHERE [" & garProjListFlds(4, 1) & "] Like ?) WHERE Left([" & sFldIn & "],1)=?;"
      'sQryString = "SELECT Count(*) FROM [" & tblProjList & "] WHERE ((Left([" & sFldIn & "], 1)= ? ) AND ([" & garProjListFlds(4, 1) & "] LIKE ?));"
      sParamValA = "*19-*"   'comment "'*" & CStr(sYrIn) & "-*'"
      sParamValB = sCharIn
      iParamLenA = 8
      iParamLenB = 1
   End If

   If QDebugMode Then Debug.Print "GetDBProjCountA: " & sQryString

   With goDBCmd
      .ActiveConnection = goDBConn
      .CommandText = sQryString
      .CommandType = adCmdText
      Set pQryParam = .CreateParameter("ParamA", adChar, , iParamLenA, sParamValA)
      .Parameters.append pQryParam
      If sYrIn > 0 Then
         Set pQryParam = .CreateParameter("ParamB", adChar, , iParamLenB, sParamValB)
         .Parameters.append pQryParam
      End If
      Set goDBRecSet = .Execute
   End With

   If QDebugMode Then Debug.Print ("GetDBProjCountB:  Parameters:   A: " & sParamValA & " B: " & sParamValB)
   Dim msg, fld
   For Each fld In goDBRecSet.Fields
      msg = msg & fld.Value & "|"
   Next
   If QDebugMode Then Debug.Print ("GetDBProjCountC:  Result: " & msg)

   GetDBProjCount = goDBRecSet.Fields(0)
   Call CloseDBConnection
End Function
记录集在makedbconction子节点中实例化为

Set goDBRecSet = New ADODB.Recordset
   goDBRecSet.ActiveConnection = goDBConn
   goDBRecSet.CursorLocation = adUseClient
   goDBRecSet.CursorType = adOpenStatic
   goDBRecSet.LockType = adLockPessimistic

   Set goDBCmd = New ADODB.Command

连接字符串是:“Provider=Microsoft.ACE.OLEDB.12.0;”和“Data Source=“&DBPath

尝试使用通配符%not asterik*

使用ADO时,应使用%作为通配符,而不是*Erik a:谢谢!!!请把它作为一个解决方案,我会投票赞成!这几乎不费吹灰之力就解决了问题!!!!搜索耗时数小时,而且我从未找到一个引用说不要使用SQL标准“*”而是“%”。非常感谢。对不起,如果Erik A愿意的话,答案将归他所有。我同意你的看法,Erik先回答了。