1

使用 MS Access 完成此项目。

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

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

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

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

更新

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

.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
4

1 回答 1

1

谢谢你的意见。我相信我已经设计出我正在寻找的东西。

两点

  • 如果要使用单个 .Execute 使用参数插入/更新和检索记录计数,则需要 ADODB.Command。这方面的例子可以在整个互联网上找到,包括我在更新部分下的原始帖子。

  • 如果您有插入/更新查询和选择查询,则不需要 ADODB.Command。我找不到这种方法的例子。下面是我想出的一个例子。

正在发生的事情的高级概述

  • 执行插入/更新查询。插入/更新不会使用单行方法返回记录集。
  • 执行选择查询。这将返回一个记录集,但是,我无法让 .Count 方法像我认为的那样工作。
  • tlemaster 的建议链接在答案部分提供了解决方法。解决方法是修改选择查询以对结果进行分组并使用 COUNT(*) 返回计数。然后使用返回值而不是 .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
    
于 2016-12-27T16:12:13.147 回答