|
|
Title | Use DAO and SQL to create and drop database tables |
Keywords | SQL, DROP TABLE, CREATE TABLE |
Categories | Database |
|
|
Use the SQL DROP TABLE and CREATE TABLE statements.
|
|
' Drop table TargetTable.
Private Sub cmdDropTable_Click()
Dim db As Database
Dim dbname As String
Dim dbopen As Boolean
On Error GoTo DropError
dbname = App.Path
If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
dbname = dbname & "droptabl.mdb"
Set db = OpenDatabase(dbname)
dbopen = True
db.Execute "DROP TABLE TargetTable"
db.Close
MsgBox "Table dropped"
Exit Sub
DropError:
MsgBox "Error " & Format$(Err.Number) & _
" dropping table." & vbCrLf & _
Err.Description
If dbopen Then db.Close
End Sub
' Drop table TargetTable.
Private Sub cmdCreateTable_Click()
Dim db As Database
Dim dbname As String
Dim dbopen As Boolean
On Error GoTo DropError
dbname = App.Path
If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
dbname = dbname & "droptabl.mdb"
Set db = OpenDatabase(dbname)
dbopen = True
db.Execute _
"CREATE TABLE TargetTable (Field1 INTEGER)"
db.Close
MsgBox "Table created"
Exit Sub
DropError:
MsgBox "Error " & Format$(Err.Number) & _
" creating table." & vbCrLf & _
Err.Description
If dbopen Then db.Close
End Sub
|
|
|
|
|
|