This post will show you how to create an empty table in access using sql and vba
This is a simplified version of my other post,
That post was the more elaborate way of table creation with VBA. This one will just show how to generate an empty table.
In the Sub Procedure called “CreateThis”, we essentially are saying we want a table created called “Customers” that has 6 fields.
The sub will “hand off” the work to the function called “CreateTable”. This function will do all the work for us. We just tell it what we want in the sub procedure.
Sub CreateThis()
'Produced by http://loeblcomservices.com
Dim strFields As String
Dim blnTableCreated As Boolean
strFields = "CustomerID,CustomerName,Address,City,State,ZipCode"
blnTableCreated = CreateTable(strFields, 6, "Customers")
MsgBox blnTableCreated
End Sub
Public Function CreateTable(table_fields As String, num_fields As Integer, table_name As String) As Boolean
Dim strCreateTable As String
Dim intCount As Integer
Dim strFields() As String
Dim strValues() As String
Dim strInsertSQL As String
Dim intCounter As Integer
Dim intData As Integer
On Error GoTo errHandler
'split the string on the comma delimiter
strFields = Split(table_fields, ",")
'this creates the table structure...
strCreateTable = "CREATE TABLE " & table_name & "("
For intCounter = 0 To num_fields - 1
strCreateTable = strCreateTable & "[" & strFields(intCounter) & "] varchar(150),"
Next
If Right(strCreateTable, 1) = "," Then
strCreateTable = Left(strCreateTable, Len(strCreateTable) - 1)
strCreateTable = strCreateTable & ")"
End If
CurrentDb.Execute strCreateTable
intCounter = 0 'reset
intData = 0 'reset
If Err.Number = 0 Then
CreateTable = True
Else
CreateTable = False
End If
Exit Function
errHandler:
CreateTable = False
MsgBox Err.Number & " " & Err.Description
End Function
In the function called “CreateTable” we are just formulating a SQL string (CREATE TABLE) comprised of the 6 fields, and then having the Access database engine create the table.
Watch me do it:
Let me know if you need help.
Find Error Line Number in VBA With Erl
In this post you are going to find out a way of handling errors in your code with the Erl function. Erl displays the last label before the error Another alternative error handling solution is using labels at each point in long procedure. Here we get a random error: Here is the code behind the […]
Here Is A Customized Msgbox VBA Example
Here is an example of a customized VBA Msgbox. We giving some richtext and customizable flair to the rather ordinary message box. The following code provided with the code after the screenshot, is going to provide you with the ability to really make the ordinary message box shine! Sub MsgboxVBAExamples() Dialog.Box “VBAHowTo.com is your source […]
How To Use The VBA Input Box Cancel Button To Exit Sub
This post will demonstrate how you can use the VBA input box cancel button to exit the sub procedure. When you click the “Cancel” button on the input box, you return a null (blank) value, and knowing this information, you can exit the sub procedure. Here is an example: Sub VBAInputBoxCancel() Dim strResponse As […]
VBA Debug Part 2
In this video I talk about VBA Debug aspects like the Immediate Window, Local Window and Watch Window. …also we use the “debug.print” statement to print a value to the Immediate window. The Local Window is useful to view the current state of all the variables present in the current sub procedure. The Watch Window […]
Support these sponsors:

