简体   繁体   English

MS Access链接表插入事务提交

[英]MS Access linked table insert transaction commit

I have an Access 2016 database that started off as a classic access MDB. 我有一个Access 2016数据库,它最初是一个经典的Access MDB。 Along the way, it was converted to a .accdb, and linked to a backend SQL Server. 在此过程中,它被转换为.accdb,并链接到后端SQL Server。 All of the tables used were converted to linked tables. 使用的所有表都转换为链接表。

There is code that performs code something like this: 有一些代码可以执行如下代码:

sql = "insert into [TableA]..."
CurrentDB.Execute sql

DoCmd.OpenReport(...)

What I'm finding is that intermittently the Insert into the backend SQL doesn't seem to complete before the OpenReport command selects the data from the same table. 我发现的是,在OpenReport命令从同一表中选择数据之前,间歇性地插入后端SQL似乎没有完成。

Would it be reasonable to solve this by using. 通过使用来解决这个问题是否合理。 Assuming my issue is that the data is not committed to the SQL table in time for the Report Execution to see it, would the following code insure this? 假设我的问题是数据没有及时提交到SQL表以供报表执行查看,下面的代码是否可以确保这一点?

sql = "insert into [TableA]..."
CurrentProject.Connection.BeginTrans
CurrentProject.Connection.Execute sql
CurrentProject.Connection.CommitTrans
DoCmd.OpenReport(...)

If you're using ADO, you can use the RecordsAffected output parameter to determine if a record got inserted. 如果使用的是ADO,则可以使用RecordsAffected输出参数来确定是否插入了记录。

You can use the following: 您可以使用以下内容:

sql = "insert into [TableA]..."
Dim recordsAffected As Long
CurrentProject.Connection.Execute sql, recordsAffected
If recordsAffected <> 0 Then
    DoCmd.OpenReport(...)
Else
    'Nothing got inserted
End If

Or, if you don't like the extra variable, you can consider using a helper function: 或者,如果您不喜欢多余的变量,可以考虑使用辅助函数:

Public Function ADOExecute(Query As String) As Long
    CurrentProject.Connection.Execute Query, ADOExecute
End Function

sql = "insert into [TableA]..."
If ADOExecute(sql) <> 0 Then
    DoCmd.OpenReport(...)
Else
    'Nothing got inserted
End If

Note that I highly recommend using a connection to your SQL Server instead of CurrentProject.Connection , which is an ADO connection to Access. 请注意,我强烈建议使用与SQL Server的连接,而不要使用CurrentProject.Connection ,它是Access的ADO连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM