adodb.command.ectute如果使用新连接,则会失败

发布于 2025-02-10 12:10:58 字数 1429 浏览 0 评论 0原文

如果我创建一个新连接,则第一个调用SQL,无论它是什么,都会失败。

        ConnectionString = CurrentProject.Connection.ConnectionString
        Set connection = New ADODB.connection

        connection.Open ConnectionString  ' Fails here now
    End If
    If connection.STATE = adStateOpen Then
        MsgBox "Success"
    Else
        MsgBox "Failure to communicate"
    End If
    connection.Execute "Select 1 from [System Log] where 1 = 0", , adCmdText Or adExecuteNoRecords

它可以正常工作,则使用“对象变量或未设置块变量”的失败

如果我只使用

 Set connection = CurrentProject.connection

连接字符串现在看起来像
提供者= microsoft.ace.oledb.12.0;用户id = admin; data source = c:\ apps \ accessdb.accdb; mode = share none; extended none; extended properties =“”; jet oledb:jet oledb:jet ole database = c:\ users \ users \ users \ users \用户\ appdata \ roaming \ Microsoft \ Access \ System.mdw; Jet OLEDB:注册表路径= Software \ Microsoft \ Office \ Office \ 16.0 \ Access \ Access \ Access \ Access Connectivition Engine; Jet oledb:jet Base oledb:database password =“ ; JET OLEDB:数据库锁定模式= 1; Jet OLEDB:全局部分散装OPS = 2; Jet OLEDB:全局散装交易= 1; Jet OLEDB:JET OLEDB:新数据库密码=“”; Jet OLEDB:Create System Database = false; jet OLEDB; JET OLEDB :加密数据库= false; jet OLEDB:不要在compact上复制语言环境= false; jet OLEDB:无复制修复= false; jet oledb; jet oledb:sfp = false; jet OLEDB:支持复杂数据验证= false;喷气OLEDB:有限的db caching = false; jet OLEDB:旁路选择菲尔德验证= false

If I create a new connection, the first call to SQL, whatever it is, fails.

        ConnectionString = CurrentProject.Connection.ConnectionString
        Set connection = New ADODB.connection

        connection.Open ConnectionString  ' Fails here now
    End If
    If connection.STATE = adStateOpen Then
        MsgBox "Success"
    Else
        MsgBox "Failure to communicate"
    End If
    connection.Execute "Select 1 from [System Log] where 1 = 0", , adCmdText Or adExecuteNoRecords

Fails with "Object variable or With block variable not set"

If I just use

 Set connection = CurrentProject.connection

it works fine.

Connection string now looks like
Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\Apps\AccessDB.accdb;Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database=C:\Users\user\AppData\Roaming\Microsoft\Access\System.mdw;Jet OLEDB:Registry Path=Software\Microsoft\Office\16.0\Access\Access Connectivity Engine;Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=True;Jet OLEDB:Bypass UserInfo Validation=False;Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass ChoiceField Validation=False

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春风十里 2025-02-17 12:10:58

通常,当您创建一个新连接时,您将尝试连接到外部数据,而ADO则非常适合。
在这种情况下,我想通过现有的本地和链接表进行工作,只是通过单独的连接,以便我可以将所有内容都放在交易中。
我不想使用现有连接,因为有些事情我不想在失败时重新卷起,例如记录。

最后,我无法做到这一点。

我考虑将所有内容移至使用DAO,以使访问本地数据变得容易,但这将是一个巨大的风险变化。

最终版本以另一种方式进行,并将日志记录转换为使用DAO和CurrentDB,而其他所有内容都继续使用ADO和CurrentProject.connection。这样,我只是在连接上设置交易,而无需新的ADODB。

Normally, when you create a new connection, you are trying to connect to external data and ADO works great for that.
In this case, I wanted to work through the existing local and linked tables, just with a separate connection so I could have everything in a transaction.
I didn't want to use the existing connection because there are some things that I didn't want to get rolled back on failure, like logging.

In the end, I couldn't get this to work.

I considered moving everything to using DAO to make it easy to access the local data, but that would have been a massive and risky change.

The final version went the other way and switched the logging to using DAO and CurrentDB, while everything else continued to use ADO and CurrentProject.Connection. This way I just set the transaction on the connection and no new ADODB needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文