What is the VBA equivalent for using Command.Prepare in ADO.NET

流过昼夜 提交于 2020-06-12 15:18:26

问题


Is there a way to use Command.Prepare, and Command.ExecuteNonQuery in VBA?

The section entitled "Consider Using Command.Prepare" in the following article on ADO.NET describes objects and methods that are similar to ADODB properties and methods and I am wondering if there is an equivalent for VBA.

ADODB has .Command, .Prepared, and .Parameters.Append, but I can't seem to put them together in a way that works similar to the sample code in the article.

http://msdn.microsoft.com/en-us/library/ms998569.aspx#scalenetchapt12_topic11

Thanks!


回答1:


The answer turned out to be fairly straightforward once I got all the syntax down. Unfortunately, it turns out there isn't much advantage to using Command.Prepared according to this article:

http://msdn.microsoft.com/en-us/library/aa260835%28VS.60%29.aspx

Read the section entitled the "so-called prepared property."

Here's the code. It works the same with and without .Prepared = True

With objCommand

    .ActiveConnection = ADO.Connection
    .Prepared = True
    .CommandText = "INSERT INTO [table1] ( col1 ) VALUES ( ?, ? )"
    .Parameters.Append .CreateParameter("intVariable", adInteger, adParamInput)
    .Parameters.Append .CreateParameter("strVariable", adVarChar, adParamInput)

    For intCounter = 0 To 10
        .Parameters("intVariable").Value = intCounter
        .Parameters("strVariable").Value = "test_value"
        .Execute
    Next intCounter

End With

I was hoping for a significant reduction in the time it took to upload my data (there is a lot of it), but all I really got was better use of parameters.



来源:https://stackoverflow.com/questions/1745997/what-is-the-vba-equivalent-for-using-command-prepare-in-ado-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!