Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246

    SQL Update queries run fine when stepping through, but not when run.

    Access 2007, SQL Server 2008 R2.

    The Total Value below is entered into a table called dbo_SPYD. The fields are bound to a table.

    The end user enters a set of values the tax assessors office publishes. They relate to one piece of property. After the numbers are entered the end user navigates to another screen where the totals are to be displayed (fields on this form are bound to table) and clicks a "Refresh Button".

    Sample of numbers entered. These numbers should generate a "Reduced" from the values from the code that follows.

    Click image for larger version. 

Name:	Decrease.JPG 
Views:	21 
Size:	16.6 KB 
ID:	21549

    This set of numbers entered should generate "No Change".

    Click image for larger version. 

Name:	NoChange.JPG 
Views:	21 
Size:	16.0 KB 
ID:	21550

    This set of numbers entered should generate "Increase Error".

    Click image for larger version. 

Name:	IncreaseError.JPG 
Views:	21 
Size:	15.9 KB 
ID:	21551

    The following code runs vba which pulls the totals from a Domain aggregate function for each number. The code then tests the numbers against each other to generate the property message such as "Increase Error". When I step through this code it works fine. No errors are generated. The variables RS1, RS2, etc all are populated with the correct numbers. When I run Debug.Print each query runs correctly and prints the proper data. When I check the table the proper data is sometimes written to the table, sometimes not. If I eliminate the STOP and run the code from the button it does not write correctly, or at all. The data is written to an underlying table that is bound to the Second Form PYD, "Property Year Detail".

    Code:
    Private Sub Refresh_Click()
       
        Dim Rs1 As Variant
        Dim RS2 As Variant
        Dim RS3 As Variant
        Dim RS4 As Variant
        Dim RS5 As Variant
        Dim stSQL As String
        
        Dim stNOChange As String '(Error Message)
        Dim stReduced As String  '(Error Message)
        Dim stIncError As String '(Error Message)
        
        Dim lngPYDID As Long 'Holds Primary Key of Destination PYD table.
        
       stIncError = "Increase Error"
       stReduced = "Reduced"
       stNOChange = "No Change"
        
       DoCmd.SetWarnings False
    
        'KEEP THIS CODE CONTAINS ORIGINAL 5 EQUATIONS AND TEST.
        'EQUATION 1 RS1. AO Proposed AV-1
        'GRABS START VALUE1 FOR AOPROPOSEDAV
    
       Rs1 = DSum("total", "TotalAOProposedAVResult")
       If IsNull((Rs1) = True Or (Rs1) = 0) Then
       Exit Sub
       End If
        
       lngPYDID = Me.PropYearDetID 'This is the Primary Key of the PYD table.
       
       stSQL = "Update dbo_PropertyYearDetail" & " set AOProposedAV = " & Rs1 & " WHERE PropYearDetID = " & lngPYDID & ";"
       Debug.Print stSQL
       DoCmd.RunSQL stSQL
        
    'END OF PROCESS FOR AOPROPOSED AV
    'EQUATION 2 RS2. AO Initial AV Result-2
    'Grabs Value for AO Proposed AV
    
        RS2 = DSum("total", "TotalAOInitialAVResult")
        If IsNull((RS2) = True Or (RS2) = 0) Then
        Exit Sub
        End If
        stSQL = "Update dbo_PropertyYearDetail" & " set AOinitialAVresult = " & RS2 & " WHERE PropYearDetID = " & lngPYDID & ";"
        Debug.Print stSQL
        DoCmd.RunSQL stSQL
        
    'EQUATION 2 Test 1
    
        If RS2 = Rs1 Then
        stSQL = "Update dbo_PropertyYearDetail" & " set AOresult = '" & stNOChange & "' WHERE PropYearDetID = " & lngPYDID & ";"
        Debug.Print stSQL
        DoCmd.RunSQL stSQL
        End If
        
    'EQUATION 2, Test 2
    
        If RS2 < Rs1 Then
        stSQL = "Update dbo_PropertyYearDetail" & " set AOresult = '" & stReduced & "' WHERE PropYearDetID = " & lngPYDID & ";"
        Debug.Print stSQL
        DoCmd.RunSQL stSQL
        End If
        
    'EQUATION 2, Test 3
    
       If RS2 > Rs1 Then
       stSQL = "Update dbo_PropertyYearDetail" & " set AOresult = '" & stIncError & "' WHERE PropYearDetID = " & lngPYDID & ";"
       Debug.Print stSQL
       DoCmd.RunSQL stSQL
       End If
        
    'EQUATION 3 RS3 AO Certified AV-3
    
       RS3 = DSum("total", "TotalAOCertifiedAVResult")
       If IsNull((RS3) = True Or (RS3) = 0) Then
       Exit Sub
       End If
       stSQL = "Update dbo_PropertyYearDetail" & " set AOcertifiedAV = " & RS3 & " WHERE PropYearDetID = " & lngPYDID & ";"
       Debug.Print stSQL
       DoCmd.RunSQL stSQL
                       
    'EQUATION 3, Test 1.
    
    'AOreReviewResult
        If RS3 = RS2 Then
        stSQL = "Update dbo_PropertyYearDetail" & " set AOreReviewResult = '" & stNOChange & "' WHERE PropYearDetID = " & lngPYDID & ";"
        Debug.Print stSQL
        DoCmd.RunSQL stSQL
        End If
    
    'EQUATION 3, Test 2.
    
        If RS3 < RS2 Then
        stSQL = "Update dbo_PropertyYearDetail" & " set AOreReviewResult = '" & stReduced & "' WHERE PropYearDetID = " & lngPYDID & ";"
        Debug.Print stSQL
        DoCmd.RunSQL stSQL
        End If
    
    'EQUATION 3 Test 3.
    
        If RS3 > RS2 Then
        stSQL = "Update dbo_PropertyYearDetail" & " set AOreReviewResult = '" & stIncError & "' WHERE PropYearDetID = " & lngPYDID & ";"
       Debug.Print stSQL
        DoCmd.RunSQL stSQL
    '    Exit Sub
        End If
    
    'EQUATION 4, RS4 BOR Result-4
    
        RS4 = DSum("total", "TotalBORInitialAVResult")
        If IsNull((RS4) = True Or (RS4) = 0) Then
        Exit Sub
        End If
        stSQL = "Update dbo_PropertyYearDetail" & " set BORInitialAVResult = " & RS4 & " WHERE PropYearDetID = " & lngPYDID & ";"
        Debug.Print stSQL
        DoCmd.RunSQL stSQL
        
    'EQUATION 4, Test 1.
    
       If RS4 = RS3 Then
       stSQL = "Update dbo_PropertyYearDetail" & " set BORResult = '" & stNOChange & "' WHERE PropYearDetID = " & lngPYDID & ";"
       Debug.Print stSQL
       DoCmd.RunSQL stSQL
       End If
    
    'EQUATION 4, Test 2.
    
        If RS4 < RS3 Then
        stSQL = "Update dbo_PropertyYearDetail" & " set BORResult = '" & stReduced & "' WHERE PropYearDetID = " & lngPYDID & ";"
        Debug.Print stSQL
        DoCmd.RunSQL stSQL
        End If
    
    'EQUATION 4, Test 3.
    
        If RS4 > RS3 Then
        stSQL = "Update dbo_PropertyYearDetail" & " set BORResult = '" & stIncError & "' WHERE PropYearDetID = " & lngPYDID & ";"
        Debug.Print stSQL
        DoCmd.RunSQL stSQL
        End If
    
    'EQUATION 5, Test 1.
    
       RS5 = DSum("total", "TotalBORFinalAVResult")
       If IsNull((RS5) = True Or (RS5) = 0) Then
       Exit Sub
       End If
       stSQL = "Update dbo_PropertyYearDetail" & " set BORFinalAV = " & RS5 & " WHERE PropYearDetID = " & lngPYDID & ";"
       Debug.Print stSQL
       DoCmd.RunSQL stSQL
        
    'EQUATION 5, Test 1.
    
      If RS5 = RS4 Then
      stSQL = "Update dbo_PropertyYearDetail" & " set BORreReviewResult = '" & stNOChange & "' WHERE PropYearDetID = " & lngPYDID & ";"
       Debug.Print stSQL
      DoCmd.RunSQL stSQL
      End If
    
    'Equation 5, Test 2.
    
       If RS5 < RS4 Then
       stSQL = "Update dbo_PropertyYearDetail" & " set BORreReviewResult = '" & stReduced & "' WHERE PropYearDetID = " & lngPYDID & ";"
       Debug.Print stSQL
       DoCmd.RunSQL stSQL
       End If
    
    'Equation 5, Test 3.
    
        If RS5 > RS4 Then
        stSQL = "Update dbo_PropertyYearDetail" & " set BORreReviewResult = '" & stIncError & "' WHERE PropYearDetID = " & lngPYDID & ";"
        Debug.Print stSQL
        DoCmd.RunSQL stSQL
        End If
        
        DoCmd.SetWarnings True
                
       ' Me.Requery
        Me.Refresh
            
    End Sub
    Here are the results from the debug.print. All values are correct to the data entered.



    Update dbo_PropertyYearDetail set AOProposedAV = 140000 WHERE PropYearDetID = 25;
    Update dbo_PropertyYearDetail set AOinitialAVresult = 150000 WHERE PropYearDetID = 25;
    Update dbo_PropertyYearDetail set AOresult = 'Increase Error' WHERE PropYearDetID = 25;
    Update dbo_PropertyYearDetail set AOcertifiedAV = 160000 WHERE PropYearDetID = 25;
    Update dbo_PropertyYearDetail set AOreReviewResult = 'Increase Error' WHERE PropYearDetID = 25;
    Update dbo_PropertyYearDetail set BORInitialAVResult = 170000 WHERE PropYearDetID = 25;
    Update dbo_PropertyYearDetail set BORResult = 'Increase Error' WHERE PropYearDetID = 25;
    Update dbo_PropertyYearDetail set BORFinalAV = 150000 WHERE PropYearDetID = 25;
    Update dbo_PropertyYearDetail set BORreReviewResult = 'Reduced' WHERE PropYearDetID = 25;

    Each runs fine when I step through. I copied one of the Update statements and pasted it in Access Query Builder and executed it and here is what I get.

    Click image for larger version. 

Name:	QueryBuilder Error.JPG 
Views:	21 
Size:	30.2 KB 
ID:	21552

    I can't tell where my error is. I cant get it to execute correctly. The screen that displays the data from above code does not refresh some times.

    Thanks,

    Phred

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The first & is not necessary but should not cause error. The ; is also not necessary but again should not cause error.

    stSQL = "UPDATE dbo_PropertyYearDetail SET BORreReviewResult = '" & stIncError & "' WHERE PropYearDetID = " & lngPYDID

    The error from the query builder is due to the stSQL=

    Try statement without that.

    Unfortunately, I've never had to run update action on SQLServer db.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    I just sent a message apparently I didn't click the reply button. I made all the changes you recommended. Apparently they all make a difference! The code seems to be running fine and the refresh is working. I need to test more. I will mark this solved after I test more. Your suggestion made the difference.

    Thanks Phred

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Phred,

    Just a comment for consideration:

    When you set warnings False, you are basically telling Access not to give you messages.
    This was an old practice with DoCmd.RunSQL

    An alternate method is to use db.Execute yourActionSql, dbFailOnError
    You do not have to do the set warnings, and Access will inform you if an error occurs -- if no error, no message.

    You Dim db as DAO.Database, then set db = currentdb

    YourActionSQL represents the name of your Update/DELETE/Insert SQL string.

  5. #5
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246

    Orange, A couple of Questions.

    Thanks Orange. I did not know this. A few Questions. I currently use MZ Tools and I plan to add error handlers when code is finalized. Would your alternate error handler replace the MZ Error Handlers or would your suggestion be incorporated within the Error Handler? How would the end user be notified if an error occurred? I had planned to build a SQL table to log all errors as they occur. So your recommendation would look like this:

    Dim various variables
    Dim DB as DAO.Database
    set db = current db

    Eliminate Dim stSQL as string.
    Eliminate "stSQL =" My SQL Statement
    Eliminate doCMD.RunSQL stSQL

    Add

    db.execute "Update dbo_PropertyYearDetail" & " set BORResult = '" & stNOChange & "' WHERE PropYearDetID = " & lngPYDID & ";"

    Is this syntax correct? Where does the ",dbFailOnError" go?

    Thanks, Phred

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Set db = CurrentDb

    db.Execute "UPDATE dbo_PropertyYearDetail SET BORResult = '" & stNOChange & "' WHERE PropYearDetID = " & lngPYDID, dbFailOnError

    or don't declare and set db variable, just:

    CurrentDb.Execute ...
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Eliminate Dim stSQL as string.
    Eliminate "stSQL =" My SQL Statement
    I don't think orange was saying to remove "stSQL = ". You can use "stSQL = " and have a command like June posted.
    Code:
    db.Execute stSQL,  dbFailOnError
    I DO use a string variable to create my SQL strings. The reason being that when I step through the VBA code, I can see what the string looks like before it executes (whether I hover over the variable or use Debug.Print). Much easier to debug the code.

    It is a lot easier to create a query in code using the variable and Debug.Print. Just before the execute command, have the debug line. Break the code on the line after the debug statement. Copy the SQL in the immediate window, then create a query, switch to SQL view and paste in the copied string. Try executing the query. If it executes, the SQL is correct - or at least it doesn't have syntax errors.
    You then can see what records are returned.


    I construct the SQL like this:
    Code:
    sSQL = "UPDATE dbo_PropertyYearDetail"
    sSQL = sSQL & " SET AOProposedAV = " & Rs1 
    sSQL = sSQL & " WHERE PropYearDetID = " & lngPYDID & ";"
    I have the key words in caps and break the like on each clause (UPDATE, SET, WHERE).
    It takes a few more lines but, for me, it is easier reading and understanding.

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Steve has interpreted my post correctly. I always suggest that you Dim a string variable, assign it "" or the beginning of your SQL statement, then modify the variable/string as you build your final SQL.
    And, do a debug.print stSQL as you proceed to check that you code has been rendered by Access as you intended.

    I am a big fan of MZTools--have used it for years. I often recommend using "Add Both" procedure header and error handler. I also recommend Smart Indenter - a free utility to reformat your vba to consistent indents/format.

    When using db.execute.... ,dbFailOnError if there is an error, it will be presented to you.

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by Phred View Post
    ...
    Each runs fine when I step through. I copied one of the Update statements and pasted it in Access Query Builder and executed it and here is what I get...
    Because you are using SQL Server you will get this error when executing via a Module. You can use some Statements in a Query Object that you cannot use via VBA. If you read the error message it will list out what is available. IIRC, the only option is to click the object within the Nav Pane. Any Execute command will analyze the SQL within the object and check for errors prior to executing.

    If you are still working on this I can check my notes to see what I have done as workarounds. Let me know.

  10. #10
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    So is this the proper construction?

    db.Execute stSQL, sSQL = "UPDATE dbo_PropertyYearDetail"
    sSQL = sSQL & " SET AOProposedAV = " & Rs1
    sSQL = sSQL & " WHERE PropYearDetID = " & lngPYDID & ";", dbFailOnError

    What kind of error message is generated. Is it something user friendly?

  11. #11
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246

    This is what I tried on one of my queries.

    So this is what I am trying on one of my queries but I get a Function not defined error. Are you using sSQL in place of my stSQL? Should I make it say stSQL all the way through? I capitalized the UPDATE, SET WHERE and broke it apart.

    Code:
    dbExecute "stSQL = UPDATE dbo_PropertyYearDetail"
    SSQL = SSQL & " SET TotalLandSF = " & lngTotLandSf & ", TotPYD = " & lngTotPYDRec & ", TotSpin = " & lngTotSPINRec & ", TotSPYD = " & lngTotSPYDRec & " "
    SSQL = SSQL & "WHERE PropYearDetID = " & lngPYDID & " dbFailOnError;"
    Thanks.

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    No, your syntax is incorrect.

    Dim stSQL as string
    stSQL = "UPDATE dbo_PropertyYearDetail "
    SSQL = SSQL & " SET TotalLandSF = " & lngTotLandSf & ", TotPYD = " & lngTotPYDRec & ", TotSpin = " & lngTotSPINRec & ", TotSPYD = " & lngTotSPYDRec & " "
    SSQL = SSQL & " WHERE PropYearDetID = " & lngPYDID
    Debug.Print stSQL
    db.execute stSQL, dbFailOnError

  13. #13
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    The original error you were getting was probably because of a syntax error. I believe June mentioned there was a problem there. What you posted in post #11 won't work. I tested an UPDATE query using VBA and was able to get it to work. Here is what I tested. Meanwhile, I will take a look at your code and see what I can come up with.

    Code:
    Dim strSQL As String
    strSQL = "UPDATE lstStates SET lstStates.StateDesc = 'Food' WHERE (((lstStates.StateKey)=81));"
    CurrentDb.Execute strSQL, dbSeeChanges

  14. #14
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    As Orange pointed out in post #12, your syntax is incorrect. Because of the SQL Server thing I added the semicolon and also changed dbfailonerror to dbseechanges.


    Code:
    Dim stSQL As String
    
    stSQL = "UPDATE dbo_PropertyYearDetail " & _
            "SET TotalLandSF = " & lngTotLandSf & ", TotPYD = " & lngTotPYDRec & ", TotSpin = " & lngTotSPINRec & ", TotSPYD = " & lngTotSPYDRec & " " & _
            "WHERE PropYearDetID = " & lngPYDID & ";"
    
     Debug.Print stSQL
    
     db.Execute stSQL, dbSeeChanges

  15. #15
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246

    Getting compile error.

    Is this correct? I get a compile error. Variable not defined. It highlights db in the last line. What kind of variable should this be?

    Code:
    stSQL = "UPDATE dbo_PropertyYearDetail " & _
        "SET TotalLandSF = " & lngTotLandSf & ", TotPYD = " & lngTotPYDRec & ", TotSpin = " & lngTotSPINRec & ", TotSPYD = " & lngTotSPYDRec & " " & _
        "WHERE PropYearDetID = " & lngPYDID & ";"
        
        Debug.Print stSQL
        
        db.Execute stSQL, dbSeeChanges
    Phred

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Update queries
    By ezz in forum Access
    Replies: 4
    Last Post: 01-04-2015, 06:16 PM
  2. Replies: 6
    Last Post: 10-15-2014, 02:45 PM
  3. Replies: 5
    Last Post: 01-13-2014, 11:35 AM
  4. Replies: 11
    Last Post: 12-11-2013, 10:35 AM
  5. Replies: 2
    Last Post: 11-19-2012, 03:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums