8

以下代码错误就MsgBox cn.RecordsAffected行了:

参数类型错误、超出可接受范围或相互冲突。

如何成功获取受影响的行数?这是针对 Access 2003 项目的。我更愿意将其保留为 2003 格式,所以如果有其他方法可以做到这一点,那就太好了。为了这个 1 功能,我不想升级整个项目。

Private Sub Command21_Click()
On Error GoTo Err1:
    Dim cn As ADODB.Connection
    Set cn = New ADODB.Connection
    With cn
        .Provider = "SQL Native Client"
        .ConnectionString = "Server=myserver\myinstance;Database=mydb;Uid=myuser;Pwd=mypass;]"
        .Open
    End With

On Error GoTo Err2:
    cn.Execute "SELECT * INTO someschema.sometable FROM someschema.anothertable"
    MsgBox cn.RecordsAffected
    Exit Sub

Err1:
    MsgBox "Failed to connect to database!"
    Exit Sub

Err2:
    MsgBox Err.DESCRIPTION
    cn.Close

End Sub
4

1 回答 1

10

ADODB.Connection没有RecordsAffected财产。但是,该Execute方法将受影响的记录作为ByRef参数 [ MSDN ] 返回:

Dim recordsAffected As Long
cn.Execute "SELECT * INTO someschema.sometable FROM someschema.anothertable", _
           recordsAffected
MsgBox recordsAffected    
于 2012-10-01T16:00:54.207 回答