Clear table in Access from Excel

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to clear the contents of a table in Access from Excel, using VBA.

This code works:

Rich (BB code):
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Option Explicit[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
Sub DeleteDataUsingActiveX()
    
    Dim MoviesConn As ADODB.Connection
    Set MoviesConn = New ADODB.Connection
    
    Dim MoviesCmd As ADODB.Command
    Set MoviesCmd = New ADODB.Command
    
    MoviesConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Mydb\Database2.accdb;Persist Security Info=False;"[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    MoviesConn.Open
    
    MoviesCmd.ActiveConnection = MoviesConn
    
    MoviesCmd.CommandText = "DELETE * FROM Table3"
    
    MoviesCmd.Execute
    
    MoviesConn.Close
    
    Set MoviesConn = Nothing
    
End Sub

but this fails:

Rich (BB code):
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub DeleteDataNotUsingActiveX()[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    Dim MoviesConn As ADODB.Connection
    Set MoviesConn = New ADODB.Connection
    
    Dim MoviesData As ADODB.Recordset
    Set MoviesData = New ADODB.Recordset
    
    MoviesConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MydbDatabase2.accdb;Persist Security Info=False;"[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    MoviesConn.Open
    
    With MoviesData
    
        .ActiveConnection = MoviesConn
        .Source = "DELETE * FROM Table3"
        .LockType = adLockOptimistic
        .CursorType = adOpenKeyset
        .Open
        
    End With
    
    MoviesData.Close
    MoviesConn.Close
    
    Set MoviesData = Nothing
    Set MoviesConn = Nothing
    
End Sub

with an error on this line:

Rich (BB code):
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]MoviesData.Close

and the data is not cleared from that table.

My question is:

1. Why is the second method erroring
2. What is the difference between the two methods?

Thanks


[/FONT]<strike></strike>
[/FONT]
<strike></strike><strike></strike>
[/FONT]
<strike></strike>
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hello tired,

This is all you really need.

Code:
Sub DeleteDataUsingActiveX()
    
    Dim MoviesConn As New ADODB.Connection
    
    With MoviesConn
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Mydb\Database2.accdb;"
        .Open
        .Execute "DELETE * FROM Table3", , adCmdText
        .Close
    End With
    
End Sub

Both of your procedures return a closed recordset...

Code:
Replace:
    MoviesData.Close
With:
    Debug.Print MoviesData.State = adStateClosed
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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