Sonia
Sonia

Reputation: 1

Delete rows in SQL table that meet a condition using VBA macro

I'm pushing data from Excel to SQL server using a macro. Right now, every time I push new data, my code deletes everything in the SQL table and pastes the new data. I want to be able to delete only specific rows where value in the first column (let's call it Column1) of my SQL table equals "xx", but I don't know how to refer to that column within my VBA code.

Here's part of my current VBA code which is working fine but, again, not quite what I need as it deletes everything:

    sSQL = "Select * FROM [dbo].[SQLtable]"
    Set oTBLRst = DB_Recordset(sSQL)
    
    ' This is how I delete the select query from above'
        With oTBLRst
            Do While Not .EOF
                .Delete
                .MoveNext
            Loop
        End With

I also tried this to only delete what I need but I don't think it worked:

sSQL = "Select * FROM [dbo].[SQLtable] where [SQLtable].[Column1] = 'xx'"
    Set oTBLRst = DB_Recordset(sSQL)
    
    ' This is how I delete the select query from above'
        With oTBLRst
            Do While Not .EOF
                .Delete
                .MoveNext
            Loop
        End With

Upvotes: 0

Views: 61

Answers (0)

Related Questions