Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 11th, 2004, 12:47 PM
Registered User
 
Join Date: Jun 2004
Location: Greensboro, NC, USA.
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.
Reply With Quote
  #2 (permalink)  
Old June 14th, 2004, 11:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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

Reply With Quote
  #3 (permalink)  
Old June 14th, 2004, 11:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:16 AM.


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