Share via

Return values/codes from DoCmd.RunSQL

Anonymous
2018-03-06T22:38:04+00:00

Hello, all. I have a VBA script that runs when a user presses a button on a form. Part of that script executes a DoCmd.RunSQL to insert a new record into one of my tables. In the next part of the script, I will be executing a similar command to insert a record into a different table, but I don't want to proceed with the 2nd insert if the first one failed for some reason. Does DoCmd.RunSQL return anything that would let me know if it succeeded or failed? If not, how should I go about checking for success?

Thanks,

-Chris

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
  1. ScottGem 68,810 Reputation points Volunteer Moderator
    2018-03-06T22:44:07+00:00

    Yes DoCmd.RunSQL should throw an error if the SQL Statement doesn't go through. If you use a DoCmd.SetWarning False prior to running the RunSQL it will suppress error messages.

    Alternative you can use:

    CurrentDB.Execute strSQL, dbFailOnError

    It will also throw an error. You can use Error Trapping to trap the error and display it, rather than run the second SQL statement.

    2 people found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2018-03-06T23:53:27+00:00

    >>If not, how should I go about checking for success? <<

    The usual approach would be to execute both SQL statements within a Transaction.  The following is a simple little procedure to illustrate this:

    Public Sub TranactionDemo()

        ' this demo procedure uses two empty tables, Families and FamilyMembers

        ' related on the FamilyName columns.

        ' the second SQL statement attempts to insert a row for family name 'Black'

        ' into FamilyMembers for which there is no row in Families, violating

        ' referential integrity and causing the transaction to be rolled back, so

        ' no row is inserted into either table

        On Error GoTo Err_Handler

        Dim strSQL As String

        Dim strCriteria As String

        DBEngine.BeginTrans

        strSQL = "INSERT INTO Families(FamilyName) " & _

            "VALUES(""White"")"

        CurrentDb.Execute strSQL, dbFailOnError

        strSQL = "INSERT INTO FamilyMembers(FamilyName, Firstname)  " & _

            "VALUES(""Black"",""John"")"

        CurrentDb.Execute strSQL, dbFailOnError

        DBEngine.CommitTrans

        ' after the transaction has been rolled back change the second SQL statement to insert

        ' a row into FamilyMembers for John White, and run the procedure again.

        ' the transaction should now be committed and rows inserted into both tables

    Exit_Here:

        Exit Sub

    Err_Handler:

        MsgBox Err.Description

        DBEngine.Rollback

        Resume Exit_Here

    End Sub

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2018-03-13T22:40:33+00:00

    Thanks, Ken. What does strCriteria do? I don't see it initialized or used anywhere other than its declaration in the Dim statement.

    I think it's left over from an earlier version of the code, and got accidentally left in place.  It's so many years since I originally wrote it, that I can't remember what it was for.

    0 comments No comments
  3. Anonymous
    2018-03-13T21:29:02+00:00

    Thanks, Ken. What does strCriteria do? I don't see it initialized or used anywhere other than its declaration in the Dim statement.

    Thanks,

    -Chris

    0 comments No comments