You are on page 1of 4

Private Sub Command0_Click()

DoCmd.SetWarnings False

DoCmd.RunSQL "DELETE * FROM [Table];"

DoCmd.SetWarnings True

MsgBox "All Records Deleted.", vbOKOnly, "Sendout Reports"

End Sub

1. 'Using ADO

2. Private ConDB As ADODB.Connection

3. Private rs As ADODB.Recordset

4. Private ProviderType As String

5. 'Make sure you know the Provider versions

6. 'PSI = "True" if there is a password to get into the database else "False"

7.

8. 'MS Access: ProviderType = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=


False;Data Source=" & MyDatabase

9.

10. 'MS SQL Server: ProviderType = "Provider=SQLOLEDB.1" & UserPassword & UserID
& ";Integrated Security=SSPI;Persist Security Info=" & PSI & ";Initial Catalog=" & Data
baseName & ";Data Source=" & ServerName

11.

12. Oracle using MS provider: ProviderType = "Provider=MSDAORA.1" & UserPassword


& UserID & ";Data Source=" & ServerName & ";Persist Security Info=" & PSI

13.
14. Oracle using MS provider: ProviderType = "Provider=OraOLEDB.Oracle.1" & UserID
& UserPassword & ";Data Source=" & ServerName & ";DistribTx=0"

15.

16. ConDB.Open ProviderType

17.

18. Private tempTable As String, tempTableType As String, tempSchema As String

19. rs.CursorLocation = adUseClient

20. Set rs = ConDB.OpenSchema(adSchemaTables)

21.

22. 'With Oracle, MySQL or if there are owners to the tables

23. Do Until rs.EOF

24. tempTable = rs!TABLE_NAME

25. tempTableType = rs!TABLE_TYPE

26. If tempTableType = "TABLE" Then

27. tempSchema = rs!TABLE_SCHEMA

28. 'Put you code here using (tempSchema & "." & tempTable) as the table name

29. End If

30. rs.MoveNext

31. Loop

32.

33. 'Many other databases

34. Do Until rs.EOF

35. tempTable = rs!TABLE_NAME

36. tempTableType = rs!TABLE_TYPE


37. If tempTableType = "TABLE" Then

38. 'Put you code here using (tempTable) as the table name

39. End If

40. rs.MoveNext

41. Loop

_Use this to DROP a table

Dim conectar As ADODB.Connection


Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Dim sql As String
Set conectar = New ADODB.Connection
conectar.ConnectionString = "Driver={MySQL ODBC 3.51 Driver};" _
& "SERVER=192.168.0.1;" _
& "DATABASE=<database>;" _
& "UID=<whatever_user>;PWD=<password>;OPTION=35"
conectar.Open

You can use directly--------->>>>>>>>>>>


rs.execute "DROP TABLE IF EXIST <table_name>"
-----Or you can use dinamic-----

sql ="DROP TABLE IF EXIST" & <table_name by a textbox or msgbox or whatever>& "
rs.execute sql

Dim db as database
Set db=OpenDatabase("testing.mdb")
db.Execute "Delete From BadTable"

Sub Form_Load ()

Dim db As database
Dim tds As TableDefs
form1.Show ' Must Show form in Load event for Print to be visible.
form1.WindowState = 2 ' Maximize Form1 to make room for table list.
sourcedb = "c:\VB3\BIBLIO.MDB" ' Original master database.
destdb = "C:\TEST.MDB" ' Path to database with table to delete.
tabletodelete = "Authors"
FileCopy sourcedb, destdb ' Use copy of database; preserve original.
Set db = OpenDatabase(destdb)
Set tds = db.TableDefs ' Open the TableDefs collection.
' Display names of all tables in database:
For j = 0 To tds.Count - 1
Print tds(j).Name
Next
Print

' Delete a table. (This deletes the TableDef and all records):
tds.Delete tabletodelete
' or use: db.TableDefs.Delete tabletodelete

' If you want to delete all records and still preserve the TableDef
' table definition, use the following instead of the above Delete:
' db.Execute "Delete From " & tabletodelete

' Display names of all tables in database:


Print "List of tables after deleting one table:": Print
For j = 0 To tds.Count - 1
Print tds(j).Name
Next

End Sub

You might also like