+ Reply to Thread
Results 1 to 4 of 4

DELETE Rows in SQL db table matching criteria via EXCEL VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    DELETE Rows in SQL db table matching criteria via EXCEL VBA

    I have fully functional code that takes data in a range on an Excel sheet and ADDS NEW ROWS to a SQL database.

    I am now faced with the need to UPDATE EXISTING ROWS instead of just adding new rows.

    I think it will be best to DELETE ALL EXISTING ROWS matching the criteria then ADD NEW again.
    My concern is, if I only update the rows listed in the table there could be an instance where there were 10 rows matching the criteria in the table but only 9 are required after the update. Better to delete 10 and add 9 than end up with a row of leftover old data; 9 new and 1 old. If I’m wrong, please advise.


    My knowledge of SQL is minimal but I need to delete this:
    Select * FROM TABLE_NAME Where DOC_ID = “123456”
    (DOC_ID is a field linked to another table NOT a primary key/unique ID in this table.)

    This is what I have preceding the ADD ROWS code where I think I should first DELETE ROWS

        ' Object type and CreateObject function are used instead of ADODB.Connection,
        ' ADODB.Command for late binding without reference to
        ' Microsoft ActiveX Data Objects 2.x Library
        ' ADO API Reference
        ' http://msdn.microsoft.com/en-us/library/ms678086(v=VS.85).aspx
        ' Dim con As ADODB.Connection
        
        Dim con As Object
        Set con = CreateObject("ADODB.Connection")
    
        con.ConnectionString = conString
        con.Open
    
        ' Dim cmd As ADODB.Command
        Dim cmd As Object
        Set cmd = CreateObject("ADODB.Command")
    
        ' BeginTrans, CommitTrans, and RollbackTrans Methods (ADO)
        ' http://msdn.microsoft.com/en-us/library/ms680895(v=vs.85).aspx
        
        Dim level As Long
        level = con.BeginTrans
    
        cmd.CommandType = 1             ' adCmdText
    
        ' Dim rst As ADODB.Recordset
        Dim rst As Object
        Set rst = CreateObject("ADODB.Recordset")
    
        With rst
            Set .ActiveConnection = con

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: DELETE Rows in SQL db table matching criteria via EXCEL VBA

    I could be wrong, but from my memory, I do not think you can use the delete command to delete data in database from excel.

    Correction. I was referring to data stored in excel, not data exported in to excel.
    Last edited by AB33; 08-10-2016 at 04:02 PM.

  3. #3
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Re: DELETE Rows in SQL db table matching criteria via EXCEL VBA

    I have figured out how to update existing rows, which is not what I really wanted, so I have a solution but I would still prefer to DELETE ALL ROWS matching FROM TABLE_NAME Where DOC_ID = “123456” if there is a solution.

  4. #4
    Valued Forum Contributor
    Join Date
    02-06-2014
    Location
    N/A
    MS-Off Ver
    N/A
    Posts
    373

    Re: DELETE Rows in SQL db table matching criteria via EXCEL VBA

    DELETE FROM TABLE_NAME WHERE DOC_ID = 123456
    <--- If your DOC_ID is formatted as a string, use '123456'

    http://www.w3schools.com/sql/sql_delete.asp

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Extracting from table based on criteria and auto delete rows after 4 days from database
    By ACALEX in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-18-2014, 10:33 AM
  2. Macro to delete rows based on two different criteria matching
    By Cpower17 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-25-2014, 05:43 PM
  3. Compare two columns from diff excel sheets and delete all non matching rows
    By Girija in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-24-2013, 11:08 PM
  4. [SOLVED] Mark or Delete Rows based on Matching Multiple Criteria in another worksheet
    By clearbluez in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-11-2012, 02:38 PM
  5. delete rows not matching certain criteria
    By sanketgroup in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-08-2011, 04:04 PM
  6. DELETE ROW 3 MATCHING CRITERIA
    By FIRSTROUNDKO via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-02-2006, 10:45 AM
  7. Delete Rows based on criteria in excel
    By Novaglory in forum Excel General
    Replies: 3
    Last Post: 04-22-2005, 01:06 PM

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.6.0 RC 1