2012-10-01 94 views
4

在與MsgBox cn.RecordsAffected線下面的代碼錯誤:如何獲取VBA中的受影響行ADO是否執行?

參數的錯誤類型,超出可接受的範圍內,或在彼此的衝突。

如何成功獲取受影響的行數?這是針對Access 2003項目的。我寧願保留在2003年的格式,所以如果有另一種方式來做到這一點,那將會很棒。爲了實現這一功能,我不想升級整個項目。

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 

回答

7

ADODB.Connection沒有RecordsAffected財產。但是,Execute方法返回受影響的記錄爲ByRef參數[MSDN]:

Dim recordsAffected As Long 
cn.Execute "SELECT * INTO someschema.sometable FROM someschema.anothertable", _ 
      recordsAffected 
MsgBox recordsAffected  
+0

完美,非常感謝Heinzi! –

相關問題