从包含excel行的记录集中删除记录

时间:2012-05-08 05:18:12

标签: vbscript

好!然后直接进入代码!

  

代码:

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")


objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\Users\HV\Desktop\samp.xls;" & _
    "Extended Properties=""Excel 8.0;HDR=Yes;"";" 

objRecordset.Open "Select * FROM [Sheet1$]", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText

objRecordSet.delete 1
  

错误消息:

http://s14.postimage.org/fntmbde0x/err.jpg

  

XLS:

http://s7.postimage.org/4oklf867f/xls.jpg

我期望做的是只删除记录集中的第一条记录,记住只记录集未更新到XLS中。非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

您需要断开连接的记录集。

  1. Recordset.CursorLocation设为adUseClient

  2. 打开记录集&获取行

  3. Recordset.ActiveConnection设为Nothing

  4. 现在您可以删除记录集中的记录。

    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adCmdText = &H1
    Const adUseClient = 3
    Set objConnection = CreateObject("ADODB.Connection")
    Set objRecordSet = CreateObject("ADODB.Recordset")
    
    
    objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\samp.xls;" & _
        "Extended Properties=""Excel 8.0;HDR=Yes;"";"
    objRecordSet.CursorLocation = adUseClient
    objRecordSet.Open "Select * FROM [Sheet1$]", objConnection, adOpenStatic, adLockOptimistic
    Set objRecordSet.ActiveConnection = Nothing
    objConnection.Close
    objRecordSet.Delete 1