Results 1 to 7 of 7
  1. #1
    cotri is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Jan 2010
    Posts
    32

    Delete all records in a field

    I am looking to put a “Command Button” on a form to delete all records in a table. I found a Macro that deletes the whole table but I just want to delete the records. Any Ideals? Thanks

  2. #2
    ConneXionLost's Avatar
    ConneXionLost is offline Simulacrum
    Windows XP Access 2003
    Join Date
    Jan 2010
    Location
    Victoria, Canada
    Posts
    291
    Hi cotri,

    I recommend you use a delete query for that. No need for a macro.

    Cheers,

  3. #3
    cotri is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Jan 2010
    Posts
    32
    Quote Originally Posted by ConneXionLost View Post
    Hi cotri,

    I recommend you use a delete query for that. No need for a macro.

    Cheers,
    Hello, Thanks for the help here. I went ahead and used a delete query here and it works just the way I wanted except I wanted to run it from a command button on a form. When I go to set it up with a button and specify which query is to be run I see all of my available queries except that one. Is there something I can do to correct this or another way to run it from the Form View? Thanks

  4. #4
    ConneXionLost's Avatar
    ConneXionLost is offline Simulacrum
    Windows XP Access 2003
    Join Date
    Jan 2010
    Location
    Victoria, Canada
    Posts
    291
    Hi cotri,

    When you add a button to a form using a wizard, Access writes a VBA code snippet to the code page of the form to handle the action resulting from clicking the button. Here's a typical example of this:

    Code:
    Private Sub cmdRunAnyQuery_Click()
    On Error GoTo Err_cmdRunAnyQuery_Click
    
        Dim stDocName As String
    
        stDocName = "qryToTestButton"
        DoCmd.OpenQuery stDocName, acNormal, acEdit
    
    Exit_cmdRunAnyQuery_Click:
        Exit Sub
    
    Err_cmdRunAnyQuery_Click:
        MsgBox Err.Description
        Resume Exit_cmdRunAnyQuery_Click
        
    End Sub

    You're right in saying that Access won't show you a delete query while running the wizard. (I never noticed that before, sorry!)

    But it isn't a problem, just pick any query and follow through the wizard as though it were the delete query, then when the wizard is done, open the form's code page and modify the button code stDocName and DoCmd line as follows.

    Code:
    Private Sub cmdDelete_Click()
    On Error GoTo Err_cmdDelete_Click
    
        Dim stDocName As String
    
        stDocName = "qryDeleteData"  'Change the query name here
        DoCmd.OpenQuery stDocName  'Remove the extra bits here
    
    Exit_cmdDelete_Click:
        Exit Sub
    
    Err_cmdDelete_Click:
        MsgBox Err.Description
        Resume Exit_cmdDelete_Click
        
    End Sub
    Note that all I did was change the target query name at stDocname, and removed the ", acNormal, acEdit" from the DoCmd line.

    Cheers,

  5. #5
    cotri is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Jan 2010
    Posts
    32
    Quote Originally Posted by ConneXionLost View Post
    Hi cotri,

    When you add a button to a form using a wizard, Access writes a VBA code snippet to the code page of the form to handle the action resulting from clicking the button. Here's a typical example of this:

    Code:
    Private Sub cmdRunAnyQuery_Click()
    On Error GoTo Err_cmdRunAnyQuery_Click
     
        Dim stDocName As String
     
        stDocName = "qryToTestButton"
        DoCmd.OpenQuery stDocName, acNormal, acEdit
     
    Exit_cmdRunAnyQuery_Click:
        Exit Sub
     
    Err_cmdRunAnyQuery_Click:
        MsgBox Err.Description
        Resume Exit_cmdRunAnyQuery_Click
     
    End Sub

    You're right in saying that Access won't show you a delete query while running the wizard. (I never noticed that before, sorry!)

    But it isn't a problem, just pick any query and follow through the wizard as though it were the delete query, then when the wizard is done, open the form's code page and modify the button code stDocName and DoCmd line as follows.

    Code:
    Private Sub cmdDelete_Click()
    On Error GoTo Err_cmdDelete_Click
     
        Dim stDocName As String
     
        stDocName = "qryDeleteData"  'Change the query name here
        DoCmd.OpenQuery stDocName  'Remove the extra bits here
     
    Exit_cmdDelete_Click:
        Exit Sub
     
    Err_cmdDelete_Click:
        MsgBox Err.Description
        Resume Exit_cmdDelete_Click
     
    End Sub
    Note that all I did was change the target query name at stDocname, and removed the ", acNormal, acEdit" from the DoCmd line.

    Cheers,

    Hello, So far all has worked well. One minor problem that I would like to fix if its not too much trouble. When I click the command button that runs the delete query the records are deleted but each field on each record (that was just deleted) shows “# Deleted”. I have to close the form and reopen it to clear all of these fields filled with “# Deleted”. Is this easily cured? Thanks

  6. #6
    ConneXionLost's Avatar
    ConneXionLost is offline Simulacrum
    Windows XP Access 2003
    Join Date
    Jan 2010
    Location
    Victoria, Canada
    Posts
    291
    Hi cotri,

    There's no need to quote my entire message in this thread, just use the "Post Reply" button.

    For the problem, add:

    Me.Requery

    after the DoCmd line.

    Cheers,

  7. #7
    cotri is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Jan 2010
    Posts
    32
    Worked perfectly! Will use “Post Reply” Thanks for all the help and easy to follow instructions.

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

Similar Threads

  1. Button on form to delete all records
    By bbylls in forum Forms
    Replies: 2
    Last Post: 12-08-2009, 12:38 PM
  2. Delete duplicate records
    By Zukster in forum Queries
    Replies: 1
    Last Post: 08-26-2009, 03:14 AM
  3. Running a Delete (Records) Query on Close
    By NoiCe in forum Queries
    Replies: 1
    Last Post: 07-12-2009, 06:17 PM
  4. #delete records
    By supernova122 in forum Access
    Replies: 0
    Last Post: 07-08-2009, 08:41 AM
  5. Delete all records in a table?
    By bob646 in forum Access
    Replies: 1
    Last Post: 05-20-2007, 11:41 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