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 to create and delete tables
KeywordsDAO, database, table, CREATE TABLE, DROP TABLE
CategoriesDatabase
 
To create a table, use a TableDef object to define the table. Use the TableDef's Fields.Append method to add field definitions. Then use the TableDefs collection's Append method to add the TableDef to the collection.
 
Private Sub cmdCreate_Click()
Dim tdef As TableDef
Dim field_names As String
Dim field_name As String
Dim pos As Integer

    On Error GoTo NotCreated
    
    ' Create a new TableDef object.
    Set tdef = db.CreateTableDef(txtTableName.Text)

    field_names = txtFieldNames.Text
    
    ' Create fields and append them to the
    ' new TableDef. We must do this before
    ' adding the TableDef to the TableDefs
    ' collection.
    Do While Len(field_names) > 0
        pos = InStr(field_names, vbCrLf)
        If pos > 0 Then
            field_name = Left$(field_names, pos - 1)
            field_names = Right$(field_names, _
                Len(field_names) - pos - 1)
        Else
            field_name = field_names
            field_names = ""
        End If
        field_name = Trim$(field_name)
        If Len(field_name) > 0 Then
            tdef.fields.Append tdef.CreateField( _
                field_name, dbText)
        End If
    Loop

    ' Add the TableDef to the database.
    db.TableDefs.Append tdef

    MsgBox "Ok"
    Exit Sub
    
NotCreated:
    MsgBox "Error" & Str$(Err.Number) & _
        " creating table." & vbCrLf & _
        Err.Description
End Sub
 
To delete a table, TableDefs collection's Delete method.
 
' Delete a table.
Private Sub cmdDelete_Click()
    On Error GoTo NotDeleted
    
    ' Delete the table.
    db.TableDefs.Delete txtTableName.Text

    MsgBox "Ok"
    Exit Sub
    
NotDeleted:
    MsgBox "Error" & Str$(Err.Number) & _
        " deleting table." & vbCrLf & _
        Err.Description
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated