I have an Access 2016 database that started off as a classic access MDB. Along the way, it was converted to a .accdb, and linked to a backend 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.
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 = "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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.