调用VBA过程 - 错误丢失=

时间:2013-03-12 07:54:32

标签: vba excel-2007

我尝试调用名为Ins_to_temp的VBA过程,但它不起作用。奇怪,因为Ins_to_temp是从另一个程序的模型构建的:Truncate_table可以工作!

你能帮我理解为什么这会在“Ins_to_temp(x,y,z)”行上发出错误 - 它说“=”缺失了! (好像它是一个功能,但它显然是一个程序)

Sub Ins_to_temp(tbl1 As String, tbl2 As String, idx As String)
    Connect
    Dim strQ As String
    Set rstR = New ADODB.Recordset
    rstR.CursorType = adOpenStatic
    strQ = "insert into " & tbl1 & " (select * from " & tbl2 & " where id = '" & idx & "')"
    rstRec.Open strQ, objCon, adOpenDynamic, adLockOptimistic
End Sub

Sub Test()
   Ins_to_temp("temp_clients","clients","202")

End Sub

作为参考,这是一个有效的程序:

Sub Truncate_table(tbl1 As String)
    Connect
    Dim strQ As String
    Set rstR = New ADODB.Recordset
    rstR.CursorType = adOpenStatic
    strQ = "DELETE FROM " & tbl1
    rstR.Open strQ, objCon, adOpenDynamic, adLockOptimistic
End Sub

我可以称之为:Truncate_table(“coll”) 谢谢

2 个答案:

答案 0 :(得分:1)

你的问题是错字:

Sub Ins_to_temp(tbl1 As String, tbl2 As String, idx As String)
    Connect
    Dim strQ As String
    Set rstR = New ADODB.Recordset
    rstR.CursorType = adOpenStatic

    strQry = "insert into " & tbl1 & " (select * from " & tbl2 & " where id = '" & idx & "')"

    ' the line above should be

    strQ = "insert into " & tbl1 & " (select * from " & tbl2 & " where id = '" & idx & "')"

    rstRec.Open strQ, objCon, adOpenDynamic, adLockOptimistic
End Sub

答案 1 :(得分:1)

那么,它有用吗?

Sub Test()
   Ins_to_temp tbl1:="temp_clients", tbl2:="clients", idx:="202"
End Sub