[英]How to delete all records of a table in MS Access using VB6 ADO
我想使用VB.6代碼刪除MS Access中表的所有記錄。
實際上我要用它重置我的數據庫,我的意思是不再有記錄了。
我怎樣才能做到這一點?
我已經做了。
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
以下代碼將刪除您連接到的Access數據庫的所有表中的所有記錄。 然而!! 如果您在表之間建立了關系並且未指定“級聯刪除”,則在從“成員”表中刪除之前,您無法刪除“所有者”表。 另一種方法是添加一個錯誤陷阱,以便檢查該條件,然后忽略(Resume Next)。 但是,您需要重復運行此代碼,直到沒有錯誤。 您需要設置連接字符串(請參閱代碼中的注釋)!!
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.