使用 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