简体   繁体   English

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

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

Using MS Access to complete this project. 使用MS Access完成此项目。

I am attempting to simplify my ADODB code by removing the need for the ADODB.Command object. 我试图通过消除对ADODB.Command对象的需求来简化我的ADODB代码。 There are two requirements, the need to use parameters and the need to retrieve records affected (for verification that the SQL executed properly). 有两个要求,即需要使用参数和检索受影响的记录(以验证SQL是否正确执行)。

The syntax I am attempting to use was mentioned in an article documented in the code block. 我尝试使用的语法在代码块中记录的一篇文章中提到。

{connection object}.[{name of query}] {parameter 1, ..., parameter n [, record set object]} {连接对象}。[{查询名称}] {参数1,...,参数n [,记录集对象]}

cn.[TEST_ADODB_Connection] 204, Date & " " & Time(), rs cn。[TEST_ADODB_Connection] 204,日期&“”和Time(),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

UPDATE 更新

Including the original code attempting to be simplified. 包括试图简化的原始代码。

The .Command object does provide the functionality I desire, but I am looking for an alternative method if it is feasible. .Command对象确实提供了我想要的功能,但是我正在寻找一种可行的替代方法。

The article ( https://technet.microsoft.com/en-us/library/aa496035(v=sql.80).aspx ) provides an example where the .Connection object could be executed using parameters. 文章( https://technet.microsoft.com/zh-cn/library/aa496035(v=sql.80).aspx )提供了一个示例,其中可以使用参数执行.Connection对象。 I am trying to extend that example and obtain records affected. 我正在尝试扩展该示例并获取受影响的记录。

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

Thank you for the comments. 谢谢你的意见。 I believe I have devised what I was searching for. 我相信我已经找到了想要的东西。

Two points 两点

  • ADODB.Command is needed if you want to insert/update and retrieve a record count using parameters using a single .Execute. 如果要使用单个.Execute使用参数插入/更新和检索记录计数,则需要ADODB.Command。 Examples of this can be found all over the internet including my original post under the update section. 可以在互联网上找到此类示例,包括我在更新部分下的原始帖子。

  • ADODB.Command is NOT needed if you have an insert/update query and a select query. 如果您具有插入/更新查询和选择查询,则不需要ADODB.Command。 I could not find examples of this method. 我找不到这种方法的示例。 Below is an example I have come up with. 下面是我想出的一个例子。

High level overview of what is going on 高层概述

  • Execute the insert/update query. 执行插入/更新查询。 Inserts/Updates will not return a recordSet using the one line method. 插入/更新不会使用单行方法返回recordSet。
  • Execute a select query. 执行选择查询。 This will return a recordSet, however, I couldn't get the .Count method to work as I would think it should. 这将返回一个recordSet,但是,我无法使.Count方法正常工作。
  • tlemaster's suggested link provided a work around in the answer section. tlemaster的建议链接在答案部分提供了解决方法。 The work around is to revise the select query to group the results and use the COUNT(*) to return the count. 解决方法是修改选择查询以将结果分组并使用COUNT(*)返回计数。 The returning value is then utilized instead of the .Count method. 然后使用返回值代替.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 

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

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