Back To Top

MS Access VBA Create Table for versions 2000, 2002, 2003, 2007

Go here To Check if Table exists in VBA.
Go here To use VBA to get all Tables.
Go here to Create a Random Sample Table in VBA
Follow the Delete a Table with VBA Link to delete a Table in VBA.


The following Access VBA Create Table code snippet will use VBA to Make a Table within the current database.

The VBA Create Table Sample is available for download here in a zipped MS Access Database. The below picture (click to enlarge) shows the Single form.

MS Access Form 

You should be able to copy/paste the below code right into a Module.


Function
MakeTable(TableName As String , FieldName As String ) As Boolean
  
Dim
Db As Database ' DAO Vars
  
Dim strSql As String
  
'References: Microsoft Access 11.0 Object Library, Microsoft DAO 3.6 Object Library
   'Set references by Clicking Tools and Then References in the Code View window
   '' Creates an empty table (TableName) with a Field (FieldName).
   ' Accepts ' TableName: Name of table to create
   ' FieldName: Name of the field to create in the table
   ' Returns True on success, false otherwise.
   'USAGE: MakeTable "TABLENAME", "FIELDNAME"

  
On Error GoTo errhandler 'If there is an error capture the error. 

   'Create theSQL Code from our String Values
  
strSql = "SELECT '' AS " & FieldName & " INTO " & TableName & ";" 

  
'Print the SQL so we can paste into the query build if there are errors
  
Debug.Print strSql

  
'Use Current Database
  
Set Db = CurrentDb() 

  
'Run the SQL Query
  
Db.Execute strSql

   MakeTable =
True

  
'Notify the user the process is complete.
  
MsgBox "Make Table Complete"

ExitHere:
  
Set Db = Nothing 

   Exit Function

errhandler:
  
'If error set function to false 

  
MakeTable = False
   Resume
ExitHere
End Function



For the above VBA Create Table Function in an usable Form with Make Table Button, download the Sample database in ZIP format.

For further help or suggestions go here: MS Access tutorials, Tables, Queries and later: Forms

You may post this tutorial on your website or in a forum. If you do please maintain a Link to Eraseve AP.



For further MS Access tutorials go here: MS Access tutorials

 

Did you find what you were looking for?
What would you suggest?
mail@eraserve.com
Your Name:
Your Message:
Your Email:

    

 

Custom Search