You create a table in Access with VBA by running a CREATE TABLE statement through the CurrentDb.Execute method, or by using a TableDef object from the DAO library. Both approaches produce a real, permanent table in the current database. For most people the SQL route is the simplest: open the VBA editor with Alt+F11, insert a module, paste a short Sub that runs a CREATE TABLE string, and run it with F5. The table appears in the Navigation Pane immediately.
This is useful when you need to build a working table on the fly, for example a staging table to hold imported data, a log table, a temporary summary table, or a set of tables that follow the same structure. Menu and dialog labels in Access and its editor can differ slightly between versions, so if a label in your copy reads a little differently, look for the nearest equivalent.
What You'll Need
- A Microsoft Access database file (an .accdb) that you can open on your own machine.
- The database open in Access, since VBA runs against the database that is currently open.
- Permission to modify the database. If the file sits on a shared server or is read-only, the code will fail. Make a backup first, because creating a table changes the file permanently.
- Some care with names. Decide the table name and field names before you start, and use names with no spaces or punctuation (for example
tblOrders,OrderDate).
Step-by-Step
- Back up the database. Close Access, copy the .accdb file to another folder, then reopen the original. Tables created by VBA are permanent, so a backup is your undo button.
- Open the VBA editor. With the database open, press Alt+F11. The Microsoft Visual Basic for Applications window appears. If a macro security warning blocks code later, you will handle it at the running stage, not here.
- Insert a standard module. In the VBA editor choose Insert > Module. A blank code window opens. Standard modules are where standalone procedures live; you do not need a form or a report for this.
- Write the procedure. Type a
Subthat builds a SQL string and executes it. A minimal working example that creates a table calledtblCustomerswith four fields looks like this:Sub CreateCustomersTable() Dim strSQL As String strSQL = "CREATE TABLE tblCustomers (" strSQL = strSQL & "CustomerID COUNTER PRIMARY KEY, " strSQL = strSQL & "FullName TEXT(100), " strSQL = strSQL & "Email TEXT(255), " strSQL = strSQL & "JoinedDate DATETIME)" CurrentDb.Execute strSQL, dbFailOnError MsgBox "Table created." End SubEach part matters.COUNTERgives you an AutoNumber field,PRIMARY KEYmakes it the unique identifier,TEXT(n)sets a maximum length, andDATETIMEstores dates and times. ThedbFailOnErrorargument makes Access raise a trappable error instead of quietly failing. - Run it. Click anywhere inside the procedure and press F5, or use Run > Run Sub/UserForm. If Access shows a security notice about macros, enable content for this database through the message bar or the Trust Center, then run again.
- Check the result. Return to Access with Alt+F11 again or the taskbar. The new table should be listed in the Navigation Pane. If the pane seems stale, press F5 in the Access window or right-click the pane and refresh.
- Confirm the structure. Right-click the table and choose Design View. You should see the four fields, the AutoNumber data type on
CustomerID, and a key icon beside it. If a field type looks wrong, compare it to the SQL you wrote. - Guard against duplicates. Running the Sub a second time fails because the table already exists. Add a check before executing:
If IsNull(DLookup("Name", "MSysObjects", _ "Name='tblCustomers' AND Type=1")) Then CurrentDb.Execute strSQL, dbFailOnError End IfThis is a common and useful pattern when the procedure runs on startup or from a button. - Reuse the pattern. To make several related tables, keep the field names identical across them where it makes sense, and write one procedure per table. Do not loop over arbitrary names; explicit strings are easier to read and debug.
Common Mistakes
Using reserved words as field names. Names such as Date, Name, Value or Type are reserved in Access and cause confusing errors. Wrap them in square brackets if you must use them, but renaming is cleaner.
Forgetting the field size on TEXT. In Access SQL, TEXT without a length defaults to a short length. Specify TEXT(255) or similar so you do not lose data on entry.
Missing or misplaced PRIMARY KEY. Only one field can carry it, and it must come directly after the field definition. A table with no primary key is legal but hard to edit in datasheets and easy to fill with duplicates.
Quoting errors in the SQL string. Each segment of a concatenated string needs its own quotes, and text values inside the SQL use single quotes. A single missing quote produces a syntax error that does not point at the real problem.
Running code against the wrong database. CurrentDb refers to the database currently open in Access. If you open a second database, focus determines which one the code hits.
Tips
- Use a consistent prefix such as
tblfor tables so they group neatly in the Navigation Pane. - Include a date field in log and staging tables. It costs nothing and answers most questions later.
- If you need more control than SQL offers, such as changed field ordering or settings defined in code, build the table with a
TableDefobject: create theTableDef, appendFieldobjects with their type and size, then append the definition toCurrentDb.TableDefs. This is the DAO route and is worth learning once the SQL version feels easy. - Test on a copy of the database, not the live file, especially if other people use it.
- Keep the SQL visible in a well-named procedure so future edits do not require guesswork.
Troubleshooting
Nothing happens when you press F5. The cursor must be inside the procedure. Click within the Sub before running.
A security message blocks the code. Enable content for the database, or add the folder to trusted locations in the Trust Center. Only do this for files you created or trust.
Run-time error about the table already existing. Confirms the table is there. Use the existence check shown above, or delete the table manually if it is a scratch table.
Syntax error in CREATE TABLE. Recheck commas between field definitions, quotes around the whole string, and that the closing parenthesis is present. Break the string into one segment per field while debugging.
The field type came out wrong. The data types are not freely interchangeable: use COUNTER for AutoNumber, LONG for whole numbers, DOUBLE for decimals, CURRENCY for money, TEXT(n) for text, MEMO for long text, DATETIME for dates and YESNO for yes/no. Changing a type after creation means deleting the table and recreating it, since existing data is lost by definition.
If that did not work, close Access, reopen the file, and retry once. If it still fails, copy the exact error number and message into a search, and compare your SQL with the working example above line by line.
Frequently Asked Questions
Can I create a table without using SQL?
Yes. The DAO TableDef approach builds a table field by field in code. It is more verbose but gives finer control, and it is the better choice when field settings matter more than speed.
Will the table be saved permanently?
Yes. A table created with CREATE TABLE or by appending a TableDef is stored in the database file and remains after you close Access.
How do I create a temporary table?
VBA has no true temporary table. The usual pattern is to create a normal table with a name like tblTempImport, fill it, use it, then run DROP TABLE or delete the object when finished.
Can I add fields to a table that already exists?
Yes, with ALTER TABLE tableName ADD COLUMN fieldName TEXT(50). You can only add; removing or retyping a field generally means rebuilding the table.
Why does my field name cause an error?
It is probably a reserved word or contains a space or symbol. Rename it, or wrap it in square brackets inside the SQL string.
Does this work in Access for the web or a linked data source?
Code-driven table creation is aimed at a standard Access database file. Behavior differs for web databases and linked sources, so test on a copy and check the documentation for your Access version before relying on it.