Results 1 to 13 of 13
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097

    Using SQL to delete all records from a table

    In opening a form designed to serve two purposes, the RecordSource is changed at open time. When a temporary query is used, the intent is to clear all the records that would have lingered from previous sessions of the app. The SQL DELETE statement fails with a FROM clause error 3131. In the current situation, the query (table) is already empty.



    Code:
    Private Sub Form_Open(Cancel As Integer)
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    '  If we're opening for a new subscriber, we need to bind the controls to
    '  a temporary empty duplicate query.  The rs.AddNew code will put the new
    '  subscriber where he/she belongs depending on what actions they choose.
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    
    
    If Len(Me.OpenArgs & "") = 0 Then
        bolNewSubscriber = True
        Me.lblHeader.Caption = "Profile for ""New Subscriber"""
        DoCmd.RunSQL "DELETE * FROM QTempSubscribers)"
        Me.RecordSource = "QTempSubScribers"
    Else
        Me.lblHeader.Caption = "Edit Subscriber Profile"     'EDit an existing subscriber
        Me.RecordSource = "QSubscribers"                     'Our current collection
        Me.Filter = "SubID = " & Me.OpenArgs                 'The subscriber being serviced
        Me.Requery                                           'Okay, just the one record of interest
        bolNewSubscriber = False                             'Boolean flag for the SAVE code
    End If
    
    
    '*=*=*=*=(Initialize required fields)=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    strCompl = Split(strReqCtls, ";")
    TestCompl
    
    
    
    
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    What is exact message of error 3131?

    If you delete from a query, then it should delete from table the query is based on so why not delete from table?

    What exactly is "a temporary empty duplicate query"? What is the SQL statement?

    Why do you need to even change RecordSource for a new subscriber? Open form to add mode:
    DoCmd.OpenForm "formname", , , , acFormAdd


    Last edited by June7; 04-14-2025 at 05:40 PM.
    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
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Perhaps do a dcount on that query first.
    I have never tried to delete from an empty query, so no idea what it might do.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    See the syntax error now? ... there shouldn't be a closing parenthesis at the end.

    DoCmd.RunSQL "DELETE * FROM QTempSubscribers)"

    should be

    DoCmd.RunSQL "DELETE * FROM QTempSubscribers"

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    What is exact message of error 3131?
    Invalid FROM clause

    If you delete from a query, then it should delete from table the query is based on so why not delete from table?
    Correct. The SQL fails even if a substitute the name of the table the query is based.

    What exactly is a "a temporary empty duplicate query"? What is the SQL statement?
    Sort of a goofy way of putting it. The temporary table is essentially a structure copy of the subscriber table. The query based on that table is what I referred to as the "temporary empty duplicate query"

    DoCmd.OpenForm "formname", , , , acFormAdd
    The difficulty I had in doing exactly as you suggest is that new subscriber was being entered into the subscriber table with incomplete information. As I have it now, the new entry is subjected to a field-by-field check before being allowed to save. Maybe there's a better way to control that?

  6. #6
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    I think copy/paste did it to me again!!!

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    No, not really. It should show us what you have. If you type it out again, you might get it right.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Just gives the usual Access warning "About to delete........"

  9. #9
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    No, not really. It should show us what you have. If you type it out again, you might get it right.
    Not sure which post you're referring?

  10. #10
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Okay June, the whole scheme be it the best approach or not, was not functioning properly because the code needs to run at the end of the OnLoad event. Too soon in the OnOpen.

  11. #11
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    if you can specify the table name where the data is, you can pass it kind of as a parameter and build the Delete statement on the fly.
    using CurrentDb.Execute instead of DoCmd.RunSQL will let you trap errors and allow some to bubble up if you want...

    <insert aircode siren here>

    public function ClearTable( byval strTableName as string )

    '-- should likely check here to see if the table exists but there's code around that does that. I think the error message is 3265 if it doesn't exist

    public function ClearTable (byval strTableName as String) as boolean

    blnSuccessful = False
    strSQL = "DELETE FROM [" & strTableName & "];"
    CurrentDb.Execute strSQL, dbFailOnError
    blnSuccessful = True

    end function

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by GraeagleBill View Post
    Not sure which post you're referring?
    #6, right after your post.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    When a temporary query is used, the intent is to clear all the records that would have lingered from previous sessions of the app.
    Sounds like you're not cleaning up properly in your code... at the end of the code that uses the temporary table, you should run the delete or drop the table there.

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

Similar Threads

  1. Replies: 3
    Last Post: 10-06-2020, 02:09 AM
  2. Replies: 4
    Last Post: 12-13-2016, 09:48 AM
  3. Delete does not delete records in evey table
    By LaughingBull in forum Access
    Replies: 5
    Last Post: 09-01-2015, 04:05 PM
  4. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  5. Replies: 6
    Last Post: 09-30-2010, 11:12 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