Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 11th, 2004, 12:47 PM
Registered User
 
Join Date: Jun 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I determine if the command failed?

How can/do I determine if an Update command was successful?
  DoCmd.RunSQL "Update table set ...."

If the command doesn't run then I know the record doesn't exist, so I want to write and If statement that says ... If update fails run Insert instead.

Thanks in advance for any advice.
 
Old June 14th, 2004, 11:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

The only means I know of to determine if the UPDATE succeeded is to evaluate the numer of records affected by executing your query. If 0 records are affected, the record doesn't exist. Your code can then branch to an INSERT statement.

Problem is, the RunSql method is pretty limited, and doesn't return the number of records affected. An ADO Command object, however, does. I put together a generic wrapper function, ExecuteDMLQuery, that accepts any Jet SQL data manipulation language statment. A boolean value is returned indicating success or failure of the action. You'll also need to set the return value to False in the error handler, should an error occur.

Hope it helps,

Bob

Sub Main()

    Dim strSQLCommand As String

    strSQLCommand = "UPDATE Orders Set Amount = 5 WHERE OrderID = 2"

    ' if Order# 2 doesn't exist, the INSERT statement gets processed.
    If ExecuteDMLQuery(strSQLCommand) Then
        MsgBox "UPDATE succeeded"
    Else
        ' execute INSERT instead
        strSQLCommand = "INSERT INTO Orders VALUES (2, 5);"
        If ExecuteDMLQuery(strSQLCommand) Then
            MsgBox "INSERT succeeded"
        End If
    End If

End Sub

Public Function ExecuteDMLQuery(strCommandText) As Boolean
    On Error GoTo ErrorHandler

    Dim cnn As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim lngRecordsAffected As Long

    On Error GoTo ErrorHandler

    Set cnn = CurrentProject.Connection

    ' Create command object
    Set cmd = New ADODB.Command
    Set cmd.ActiveConnection = cnn
    cmd.CommandType = adCmdText
    cmd.CommandText = strCommandText

    ' If 0 records returned, the record does not exist.
    cmd.Execute lngRecordsAffected

    ' set boolean flag to indicate update success.
    If lngRecordsAffected <> 0 Then
        ExecuteDMLQuery = True
    Else
        ExecuteDMLQuery = False
    End If

    ' clean up
    cnn.Close
    Set cnn = Nothing
    Exit Function

ErrorHandler:

    ' set boolean flag to false if error encountered.
    ExecuteDMLQuery = False

    If Not cnn Is Nothing Then
        If cnn.State = adStateOpen Then cnn.Close
    End If
    Set cnn = Nothing

    If Err <> 0 Then
        MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If

End Function

 
Old June 14th, 2004, 11:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Delete the second On Error GoTo ErrorHandler statement in the function. My bad.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Command text was not set for the command object Sheraz Khan Classic ASP Databases 2 May 29th, 2007 12:57 AM
Command text was not set for the command object. deepa12 BOOK: Beginning ASP 3.0 5 November 2nd, 2004 05:37 PM
Run-time Error 4198 - Command Failed while using I dineshmahadev Pro VB 6 0 April 28th, 2004 01:19 AM
Need Help: Can't determine cause of error xgbnow Visual C++ 3 September 22nd, 2003 05:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.