0

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
2
  • 3
    sSQL = "delete FROM [dbo].[SQLtable] where [SQLtable].[Column1] = 'xx'" and execute that without returning a recordset. There's no need to use a recordset to delete records. Commented Feb 28 at 0:00
  • For example: stackoverflow.com/a/33633303/478884 That's for Access, but it will be the same basic process for SQL server. Commented Feb 28 at 0:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.