Deleting records in Access with VBA in Excel

carlleese24

Board Regular
Joined
Mar 15, 2005
Messages
108
hi

I am trying to delete all records in Access using a VBA in excel by typing a persons name in cell A1 and then the VBA will delete all the records in MS Access that contains that name. The Field in access is called names

I would really appreciate it if someone could help me with this please
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
hi

I managed to get some sort of code for it now

Code:
Sub DeleteAccessData22()
    Dim cnn As ADODB.Connection
    Dim sql As String
    Dim sPath As String
    Dim sTblName As String
    
    Set cnn = New ADODB.Connection
    
    'Define Database Path
    sPath = "E:\Files ready for disc inclu sp2\Files Ready for disc\racing data\Football Data\Football database\Football_Spread_Betting_Analysis.mdb"
    
    'Define Table Name
    sTblName = "TableName"
        
    'Define Connection String
    cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
                         & "Data Source=" & sPath
    'Open Connection
    cnn.Open
    
'Create Sql
    sql = "DELETE " & sTblName & ".FieldName FROM " & sTblName & " WHERE (((" & sTblName & ".FieldName)=Sheets("Sheet2").Range("a1")));"
    

    
    'If you Want to delete All data from the table uncomment below
    'sql = "DELETE FROM " & sTblName
    
    'Execute Sql
    cnn.Execute (sql)

    'Close
    cnn.Close
    Set cnn = Nothing
End Sub



I am getting this error message when I run it

compile error
Syntax error

Pointing to this line of coding



Code:
WHERE (((" & sTblName & ".FieldName)=Sheets("Sheet2").Range("a1")));"


Hope someone can help me with this it would appreciated
 
Upvote 0
Maybe others were reluctant to post due to the "deletion" nature, but I corrected your code and found it helpful in my own project.

Try

sSQL = "DELETE " & sTblName & ".FieldName FROM " & sTblName & " WHERE (" & sTblName & ".FieldName) =" & Sheets("Sheet2").Range("a1") & ";"

Assuming you have a field named "FieldName" and it is not a variable.
 
Upvote 0

Forum statistics

Threads
1,215,335
Messages
6,124,326
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top