VBA将记录从一个表移动到另一个表

时间:2017-05-25 08:24:20

标签: mysql excel-vba vba excel

我想将记录从表Sheet2移动到Table Sheet3。我收到错误

  对象打开时无法执行

操作。

With Acon
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=" + myFileNameDir
    .Open
End With

With Rs
    .CursorLocation = adUseClient
    .CursorType = adOpenDynamic
    .LockType = adLockOptimistic
    .ActiveConnection = Acon
    .Source = "Select * from [Sheet2] where [Manager Com]= 'OK'"
    .Open
End With

With Rs1
    .CursorLocation = adUseClient
    .CursorType = adOpenDynamic
    .LockType = adLockOptimistic
    .ActiveConnection = Acon
    .Source = "Select * from [Sheet3]"
    .Open
End With

Dim strSQL  As String
strSQL = "INSERT INTO Sheet3 SELECT * From Sheet2 where [Manager Com]= 'OK'"
Rs.Open strSQL

Rs1.Close
Rs.Close
Acon.Close

Set Rs = Nothing
Set Acon = Nothing

1 个答案:

答案 0 :(得分:0)

我怀疑是因为你试图从查询中执行一个语句。 请尝试以下方法:

Dim CMD As Object
Dim strSQL  As String

With Acon
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=" + myFileNameDir
    .Open
End With

Set CMD = CreateObject("ADODB.Command")
CMD.ActiveConnection = ACon


strSQL = "INSERT INTO Sheet3 SELECT * From Sheet2 where [Manager Com]= 'OK'"

CMD.CommandText = strSQL
CMD.Execute

Acon.Close