+ Reply to Thread
Results 1 to 3 of 3

Thread: Build a Delete Query in VBA

  1. #1
    Registered User
    Join Date
    12-15-2010
    Location
    Greenville, South Carolina
    MS-Off Ver
    Excel 2007
    Posts
    55

    Build a Delete Query in VBA

    Hello,

    I am trying to build a delete query that will run off of the transaction numbers listed in another query. So far I have not been able to do this with a simple query. I thought it might be easier/possible in VBA. Currently I am running into errors when I try and set my recordset. Any tips would be greatly appreciated!!

    Sub DeleteForm()
        
        Dim rs As DAO.Recordset
        Dim fld As DAO.Field
        Dim sql As String
        Dim sql2 As String
        
        sql = "SELECT q_BackDateI.TransNum FROM q_BackDateI"
    
        Set rs = CurrentDb.OpenRecordset(sql)
        
        For Each fld In rs.Fields
        
            sql2 = "DELETE t_Withhelds.* FROM t_Withhelds Where(((t_Withhelds.TransNum) = " & fld & "));"
            
        Next
    
        
    End Sub

  2. #2
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Build a Delete Query in VBA

    You don't want to (I think you can't) run SQL statement on individual records in a recordset. If you want to use a recordset, you need to use recordset functions to alter your data. In general, it'd be something like this:

    Sub DeleteForm()
        
        Dim rs As DAO.Recordset
        Dim fld As DAO.Field
        Dim sql As String
        Dim sql2 As String
        
        sql = "SELECT q_BackDateI.TransNum FROM q_BackDateI"
    
        Set rs = CurrentDb.OpenRecordset(sql)
        
        rs.movefirst
        Do Until rs.EOF
            If rs!TransNum = fld Then rs.Delete
            rs.movenext
        Loop
     
    End Sub
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Registered User
    Join Date
    03-05-2009
    Location
    Vail, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    82

    Post Re: Build a Delete Query in VBA

    The code example above is great, I use it often.
    Just in case your looking for a slightly different point of view, this is something I used to create large custom reports in Excel.
    The Access user interface takes selected inputs and then builds multiple custom local Access tables. These are later called in a final SQL statement.
    170 deletes all records (*) from the table. It could also be modified for more selection.
    In my unique case, an autocounter links multiple tables.
    So, after deleting all records, the autocounter needs to be reset. Otherwise, the deleted records last autocounter number persist. Here is an example of that code.


    160 DoCmd.SetWarnings False
    170 DoCmd.RunSQL ("Delete * From 08_Team_Permit_Federal_Excel_Local")
    180 Call ChangeSeed("08_Team_Permit_Federal_Excel_Local", "CounterField", 1, 1)
    ' remember to turn SetWarning back to True later
    '   WARNING **** This will NOT work on linked tables
    '   Use to reset the Autocounter after deleting records in a LOCAL temp table
    '   08_Team_Permit_Federal_Excel_Local
    '   08_Team_Permit_State_Excel_Local and six others
    '   for a very complex Permit report
    Public Sub ChangeSeed(strTableName As String _
                               , strFieldName As String _
                               , lngStartAt As Long _
                               , intIncrementBy As Integer)
     
        Dim strSql As String
     
        'Build the SQL statement
        strSql = "ALTER TABLE " & strTableName & _
                 " ALTER COLUMN " & strFieldName & _
                 " COUNTER (" & lngStartAt & "," & intIncrementBy & ")"
    ' Debug.Print strSql           ' uncomment for testing purposes
        'Execute the SQL statement
        CurrentProject.Connection.Execute strSql, , adCmdText
     
    End Sub
    If this code was useful, a thank you would be appreciated

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0