Reputation: 65
I want to delete all records of a table in MS Access using VB.6 code.
Actually I'm going to reset my database with it, by that I mean to have NO records anymore.
How can I do that?
Upvotes: 1
Views: 11551
Reputation: 65
I have done it.
On Error Resume Next
Dim cnn As New ADODB.Connection
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\DB.mdb"
cnn.Open
cnn.Execute "delete * from LogTBL"
cnn.Close
Upvotes: 1
Reputation: 4312
Here is code that will delete all records in all tables of the Access database you are connected to. HOWEVER!! If you have built relationships between tables and have not specified 'Cascade Delete', then you can't delete the 'owner' table until deleting from the 'member' table. One way around that is to add an error trap so that you check that condition, then ignore (Resume Next). But then you would need to repeat running this code until no errors. You need to set your connection string (see comment in code)!!
Function Delete_All_Data()
Dim cnLocalData As ADODB.Connection
Dim catLocal As ADOX.Catalog
Dim tdf As ADOX.Table
Dim strSQL As String
Dim strTableName As String
Set cnLocalData = New ADODB.Connection
MsgBox " Add code to connect to your database either using Connection cnLocalData or your own and change this code!!!"
Set catLocal = New ADOX.Catalog
catLocal.ActiveConnection = cnLocalData
For Each tdf In catLocal.Tables
If UCase(tdf.Type) = "TABLE" And UCase(Left(tdf.Name, 4)) <> "MSYS" Then
if tdf.Name <> "INFOTBL" Then
strTableName = tdf.Name
strSQL = "delete * from [" & strTableName & "];"
cnLocalData.Execute strSQL
End If
End If
Next tdf
Set tdf = Nothing
Set catLocal = Nothing
cnLocalData.Close
Set cnLocalData = Nothing
End Function
Upvotes: 1