Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    513

    simple delete SQL statement not working

    I've got this quirk that occassionally creates a blank record, so to clean them up, I've put in a snippet that should delete all of those blanks on the next form current event, but it's not working...

    the records are in a table related to the form's source table and populate a subform; the user can add new records on the subform
    Public Sub DoSQLDeleteBlankInstallationNotes(frm As Access.Form)
    Dim sSQL As String
    sSQL = "Delete from tbeInstallationNotes" & _
    " WHERE len(nz([InstallationNote],"")) =0;"


    DoCmd.SetWarnings False
    DoCmd.RunSQL sSQL
    DoCmd.SetWarnings True
    End Sub

    so to check myself, on the sub form right next to the text box with the value [InstallationNote], I added another text box whose value is:
    =len(nz([InstallationNote],""))
    low and behold, blank records have a zero value

    and then another text box
    =len(nz([InstallationNote],"")) = 0
    and I get -1's

    dispite this, the blank records do not delete (?!)

    preplexed
    (but with thanks in advance),
    mark

  2. #2
    Beetle is offline Unrelatable
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    Camp Swampy (Denver, CO)
    Posts
    207
    I've got this quirk that occassionally creates a blank record,
    Sort of wondering why you don't fix the "quirk" rather than continually deleting records after the fact. However, be that as it may...

    the records are in a table related to the form's source table and populate a subform
    What's the nature of the relationship? If you have RI enforced and you do not have Cascade Delete Related Records set, then you cannot delete a record from the parent table because it would leave orphaned records in the child table. Normally you would get a message informing you of this but you have turned off warnings in the code so you have no idea that something went wrong. I recommend you use -

    CurrentDb.Execute sSQL, dbFailOnError

    instead of

    DoCmd.RunSQL sSQL

    - because it will bypass the unnecessary (and annoying) standard warnings about "you are about to delete x records in a table" but will still inform you if there is an error during the process (provided that you make sure and include the dbFailOnError argument). So you don't need to worry about turning the warnings off and on, which is a potential problem in and of itself because if you are not careful how you structure the code then the warnings could get turned off but not get turned back on again. So the code would just be;

    Code:
    Public Sub DoSQLDeleteBlankInstallationNotes(frm As Access.Form)
    
        Dim sSQL As String
    
        sSQL = "Delete from tbeInstallationNotes" & _
        " WHERE len(nz([InstallationNote],"")) =0;"
    
        CurentDb.Execute sSQL, dbFailOnError
    
    End Sub

  3. #3
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    513
    (pardonte pause... my attentiosn on this is, unfortunately, intermittent)

    to your ?'s
    why you don't fix the "quirk"
    I'd like to, and have tried; haven give up for the time being.... I thought a work-around was worth venturing

    the problem is that on a subform, I have a command button that flips the background color of the sfrm (generally to yellow) as a user triggered alert that they need to do something to the information on the form (finish editing, correct, verify... something); if the subform has no current records, when the background color is changed (by VBA), a blank record is autamatically added ?!?!?


    What's the nature of the relationship?
    IR is not reinforced; the table that I am deleting the records from has a many-to-one retaionship to the table datasource for the parent table; i am deleting from the many table ONLY

    CurrentDb.Execute sSQL, dbFailOnError
    good point, I'll correct here, and adheare to your suggestion in all instances forthcoming (THANX);
    having changed the code, no errors were flagged; however, adding msg boxes as a crude debugger: the first (sSQL) showed... the second ("no error") does not

    Dim sSQL As String
    sSQL = "Delete from tbeInstallationNotes" & _
    " WHERE len(nz([InstallationNote],"")) = 0;"
    MsgBox (sSQL)
    CurrentDb.Execute sSQL, dbFailOnError
    MsgBox "no error"

    ---------------------
    curiously, a similar block of code executes correctly:
    Dim sSQL As String
    sSQL = "Delete from tbeInstallationNotes" & _
    " WHERE [BaseInstallationNote] = true and [Type] = '" & frm.Type.Value & "';"
    CurrentDb.Execute sSQL, dbFailOnError

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Have you tried setting a breakpoint on the line

    sqL = "Delete........

    then stepped (F8 key) through the code to see if you reach the 2nd message box line? A little debugging goes a long way...
    Do you have an error handler in this code?

  5. #5
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    513
    as has been said before:
    "when nothing else works... check the obvious"

    syntax error in string query expression 'len(nz([InstallationNote],"")) = 0;'.

    -now I have no idea what that could be, but at least I know more than I did before

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
    Dim sSQL As String
        sSQL = "Delete from tbeInstallationNotes" & _
            " WHERE len(nz([InstallationNote],"")) = 0;"
    MsgBox (sSQL)
    I must be brain dead today....

    Try this:
    Code:
        Dim sSQL As String
    
        sSQL = "DELETE * FROM tbeInstallationNotes" & _
               " WHERE (((Len(nz([InstallationNote],"")))=0));"
        
        MsgBox (sSQL)
        CurrentDb.Execute sSQL, dbFailOnError

  7. #7
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    513
    nope... sorry... same error

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The code worked in my test dB.....

    Do you have a reference set for Microsoft DAO 3.6 Object Library?

    Since the records in the many table that are created have no data, how about this:
    Code:
        Dim sSQL As String
    
        sSQL = "DELETE *"
        sSQL = sSQL & " FROM tbeInstallationNotes"
        sSQL = sSQL & " WHERE (((tbeInstallationNotes.InstallationNote) Is Null));"
    
        MsgBox (sSQL)
        CurrentDb.Execute sSQL, dbFailOnError

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    I wondered about this, too:
    Quote Originally Posted by ssanfu View Post

    ...Do you have a reference set for Microsoft DAO 3.6 Object Library...
    because of

    Quote Originally Posted by markjkubicki View Post

    ...syntax error in string query expression 'len(nz([InstallationNote],"")) = 0;'
    The 'syntax error,' plus the fact that Intellisense apparently didn't recognize Len and Nz as valid functions (if it had, it would have capitalized the first letter of each one) tends to support the possibility of a Missing Reference.

    If you haven't checked for this kind of thing before, here are Doug Steele's detailed instructions on how to troubleshoot the problem:

    Access Reference Problems

    Linq ;0)>

  10. #10
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    513
    trying to enable Microsoft DAO 3.6 Object Library results in the error: " Name conflicts with existing module, project, or object library "

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Delete fileds in tables by using sql statement
    By BorisGomel in forum Access
    Replies: 4
    Last Post: 11-14-2012, 04:38 PM
  2. Simple SQL statement in VB
    By JFo in forum Programming
    Replies: 5
    Last Post: 10-05-2011, 09:55 PM
  3. Replies: 1
    Last Post: 07-30-2011, 07:58 AM
  4. Delete SQL statement not working properly
    By Alexandre Cote in forum Programming
    Replies: 3
    Last Post: 10-18-2010, 12:56 PM
  5. Simple delete button on form
    By chessico in forum Forms
    Replies: 9
    Last Post: 10-15-2009, 03:14 PM

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