简体   繁体   English

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

[英]Using ADODB recordset doesn't return a value when same query in Access does

I am having a frustrating problem with EXCEL VBA using an ADODB recordset.我在使用 ADODB 记录集的 EXCEL VBA 遇到了令人沮丧的问题。 I have a query which directly in Access works correctly but when I put it in the VBA the recordset always comes back null.我有一个直接在 Access 中正常工作的查询,但是当我将它放入 VBA 时,记录集总是返回空值。 I have other queries that work as expected but something about this type of query always fails to return a result in VBA even though it works in Access.我有其他按预期工作的查询,但有关此类查询的某些内容始终无法在 VBA 中返回结果,即使它在 Access 中工作。

Below is the function responsible for executing the query.下面是负责执行查询的函数。 It is built to allow me to do counts, and in a similar function, sums on the ProjList table.它的构建允许我进行计数,并在类似的函数中对 ProjList 表进行求和。 The first style query is used where no year limiting flag is to be used (sYrIn = -1) and it works as expected.第一个样式查询用于不使用年份限制标志 (sYrIn = -1) 的情况,它按预期工作。

The second style query is when the year in flag contains a value to limit the count or sum to a specific year.第二种样式查询是当标志中的年份包含一个值以将计数或总和限制为特定年份时。 Our Project IDs are of the form X99-999 where the first position denotes a type of project, the next two digits signify the year the project opened and the last three digits are a unique incremented number which resets to 001 each year.我们的项目 ID 的格式为 X99-999,其中第一位表示项目类型,接下来的两位数字表示项目开始的年份,最后三位数字是唯一递增的数字,每年重置为 001。

There are a number of lines here that are only for debugging feedback.这里有许多行仅用于调试反馈。 Below the code I have included some of the debugging output.在代码下方,我包含了一些调试输出。

I understand we are to ask a specific question, but I have four that are tightly related to this situation and code sample and do not think that four posts so tightly related are a good thing.我知道我们要问一个具体的问题,但我有四个与这种情况和代码示例密切相关的帖子,并且认为四个帖子如此紧密相关并不是一件好事。 My questions are about the second set of queries.我的问题是关于第二组查询。 They never work in VBA.他们从不使用 VBA。
1) Am I reading the recordset incorrectly in that case? 1) 在这种情况下,我是否错误地读取了记录集?
2) Is there some limitation to the "complexity" of query that can be executed in this way? 2)以这种方式执行的查询的“复杂性”是否有一些限制?
3) Why does the first query return runs and the second doesn't? 3)为什么第一个查询返回运行而第二个没有?
4) Is there a better way to see the real query execution result? 4)有没有更好的方法可以看到真实的查询执行结果?

Again all of the queries work in Access.同样,所有查询都在 Access 中工作。
Note: the code as it appears has the year search hard coded since the actual code uses the current year and not enough data for 2020 exists for the result to be meaningful (0 is 0!)注意:显示的代码具有硬编码的年份搜索,因为实际代码使用当前年份并且 2020 年没有足够的数据使结果有意义(0 是 0!)
Also there are two queries for the second type.第二种类型也有两个查询。 One is commented out--it suffers the same failure in VBA and success in Access.一个被注释掉了——它在 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

Here is what I see in the immediate window:这是我在即时窗口中看到的内容:

GetDBProjCountA: SELECT Count(*) FROM ProjList WHERE Left(ProjArchiveFlag, 1)=?;
GetDBProjCountB:  Parameters:   A: O B: 
GetDBProjCountC:  Result: 45|
GetDBProjCountA: SELECT Count(*) FROM (Select * from [ProjList] WHERE [ProjCCID] Like ?) WHERE Left([ProjArchiveFlag],1)=?;
GetDBProjCountB:  Parameters:   A: *19-* B: O
GetDBProjCountC:  Result: 0|    (In Access this one returns 44)

The recordset is instantiated in the MakeDBConection sub as记录集在 MakeDBCononection 子中实例化为

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

   Set goDBCmd = New ADODB.Command

The connection string is: "Provider=Microsoft.ACE.OLEDB.12.0;"连接字符串是:“Provider=Microsoft.ACE.OLEDB.12.0;” & " Data Source=" & DBPath &“数据源=”&DBPath

尝试使用通配符 % 而不是星号 *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM