Man is a slow, sloppy, and brilliant thinker; computers are fast, accurate, and stupid.
- John Pfeiffer

Top Ten Tags

Who's Online

Here is a simple example of creating a table using DAO. Be sure that your DAO reference is set.

You can also use SQL to create a table. I've got an example here.

If you wanted to you could use DAO to execute that SQL or even DoCmd.RunSQL.

I've also got some simple code (DAO and ADO) to manipulating recordsets, which you will need to know how to do in order to add new records to a table (see here). Again, you could use an SQL statement (i.e. INSERT INTO) from VBA but I think using a DAO recordset is easier to code an understand.

Be sure that you execute the .Update statement after adding a new record (using. AddNew) and creating the values for the fields. Remember if you use the field number, that .Field is 0-indexed; i.e. the first field is .Field(0) not .Field(1).

VBA CODE

Sub CreateNewTblDef() 
    On Error GoTo ErrMsg:
     
    Dim db As DAO.Database 
    Dim tdf As DAO.TableDef 

    'Specify database to add file to     
    'We will use the current file for this example
    Set db = CurrentDb() 
     
    'Create a new TableDef object
    Set tdfNew = db.CreateTableDef("tblNew") 
     
    With tdfNew 
        'Create fields and append (i.e. add) them 
        'to the new TableDef object.
        .Fields.Append .CreateField("strText", dbText) 
        .Fields.Append .CreateField("memMemo", dbMemo) 
        .Fields.Append .CreateField("lngLong", dbLong) 
        .Fields.Append .CreateField("dblDouble", dbDouble) 
        .Fields.Append .CreateField("blnBoolean", dbBoolean) 
    End With 
     
    'Append the new table only after adding the fields
    db.TableDefs.Append tdfNew 
    db.Close 

ExitHere:
    Exit Sub
    
ErrMsg:
    'Handle Errors
    MsgBox "Uh-oh! An error occured!" & vbNewLine & vbNewLine & _
           "Error Number: " & Err.Number & vbNewLine & _
           "Error Description: " & Err.Description, vbCritical
    Resume ExitHere:
End Sub