tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,913
- Office Version
- 365
- 2019
- Platform
- Windows
I am trying to clear the contents of a table in Access from Excel, using VBA.
This code works:
but this fails:
with an error on this line:
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>
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>