VBA - ADODB.Connection - 使用参数和检索影响计数的记录

VBA - ADODB.Connection - Using parameters and retrieving records affected count

使用 MS Access 完成此项目。

我试图通过消除对 ADODB.Command 对象的需要来简化我的 ADODB 代码。有两个要求,需要使用参数和需要检索受影响的记录(用于验证 SQL 是否正确执行)。

代码块中记录的一篇文章中提到了我尝试使用的语法。

{连接对象}.[{查询名称}] {参数 1, ..., 参数 n [ 记录集对象]}

cn.[TEST_ADODB_Connection] 204, 日期 & " " & 时间(), rs

Sub TEST_ADODB_Connection()

'https://technet.microsoft.com/en-us/library/aa496035(v=sql.80).aspx
'Using ADODB without the use of .Command

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Dim lngRecordsAffected As Long

Set cn = CurrentProject.Connection

'TEST_ADODB_Connection Query
'INSERT INTO tbl_Log ( LogID_Orig, LogMessage )
'SELECT [NewLogID] AS _LogID, [NewLogMessage] AS _LogMessage;

Set rs = New ADODB.Recordset
cn.[TEST_ADODB_Connection] 204, Date & " " & Time(), rs
lngRecordsAffected = rs.RecordCount 'Error 3704 - no records returned
                                    'so this is expected, but how do we 
                                    'get records affected by the update query?

Debug.Print lngRecordsAffected 

End Sub

更新

包括试图简化的原始代码。

.Command 对象确实提供了我想要的功能,但我正在寻找可行的替代方法。

文章 (https://technet.microsoft.com/en-us/library/aa496035(v=sql.80).aspx) 提供了一个示例,其中可以使用参数执行 .Connection 对象。我正在尝试扩展该示例并获取受影响的记录。

Sub TEST_ADODB_Command()

Dim cm As ADODB.Command
Dim rs As ADODB.Recordset

Dim iLogID_Auto As Integer
Dim strLogMessage As String

Dim lngRecordsAffected As Long

Set cm = New ADODB.Command

iLogID_Auto = 204
strLogMessage = Date & " " & Time

With cm
    Set .ActiveConnection = CurrentProject.Connection
    .CommandText = "TEST_ADODB_Connection"
    .CommandType = adCmdStoredProc
    .NamedParameters = True ' does not work in access

    .Parameters.Append .CreateParameter("[NewLogID]", adInteger, adParamInput, , iLogID_Auto)
    .Parameters.Append .CreateParameter("[NewLogMessage]", adVarChar, adParamInput, 2147483647, strLogMessage)

    Set rs = .Execute(lngRecordsAffected)

    Debug.Print lngRecordsAffected
End With

Set rs = Nothing
Set cm = Nothing

End Sub

感谢您的评论。我相信我已经想出了我要找的东西。

两分

  • ADODB.Command 如果您想 insert/update 并使用单个 .Execute 使用参数检索记录计数,则需要

  • ADODB.Command。这方面的例子可以在整个互联网上找到,包括我在更新部分下的原始 post。

  • 如果您有一个 insert/update 查询和一个 select 查询,则不需要
  • ADODB.Command。我找不到这种方法的例子。下面是我想出的一个例子。

对正在发生的事情的高度概述

  • 执行insert/update查询。 Inserts/Updates 不会 return 使用单行方法的记录集。
  • 执行 select 查询。这将 return 一个记录集,但是,我无法让 .Count 方法按我认为的那样工作。
  • tlemaster 的建议 link 在答案部分提供了解决方法。解决方法是修改 select 查询以对结果进行分组并使用 COUNT(*) 来 return 计数。然后使用 returning 值代替 .Count 方法。

    Sub TEST_ADODB_Connection()
    'https://technet.microsoft.com/en-us/library/aa496035(v=sql.80).aspx
    'Using ADODB without the use of .Command and .Parameters
    
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim lngRecordsAffected As Long
    
    Dim strDateTime As String
    Dim lngID As Long
    
    Set cn = CurrentProject.Connection
    strDateTime = Date & " " & Time()
    lngID = 204 'random number for example purpose
    
    'TEST_ADODB_Connection INSERT Query
    'INSERT INTO tbl_Log ( LogID_Orig, LogMessage )
    'SELECT [NewLogID] AS _NewLogID, [NewLogMessage] AS _LogMessage;
    
    'This line will execute the query with the given parameters
    'NOTE: Be sure to have the parameters in the correct order
    cn.[TEST_ADODB_Connection] lngID, strDateTime
    
    'TEST_ADODB_Select
    'SELECT Count(tbl_Log.LogID_Orig) AS recordCount
    'FROM tbl_Log
    'WHERE tbl_Log.LogID_Orig=[_LogID] AND tbl_Log.LogMessage=[_LogMessage];
    
    'Must initilize recordset object
    Set rs = New ADODB.Recordset
    
    'This line will execute the query with given parameters and store
    'the returning records into the recordset object (rs)
    'NOTE: Again, be sure the parameters are in the correct order
    'NOTE: the recordset object is always the last argument
    cn.[TEST_ADODB_Select] lngID, strDateTime, rs
    
    'unable to directly utilize the .Count method of recordset
    'workaround and more optimal solution is to write the SQL
    'to return a count using grouping and Count(*) - see SQL above
    lngRecordsAffected = rs("recordCount").Value
    
    'Close recordset object
    rs.Close
    
    Debug.Print lngRecordsAffected 
    End Sub