Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse DAO and SQL to create and drop database tables
KeywordsSQL, DROP TABLE, CREATE TABLE
CategoriesDatabase
 
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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated