• Creating an Access Database (on the fly) Using VBA and SQL

    I recently had reason to create a database on the fly if one did not exist. Since it took me some trial, error and searching (and then more trial and error,) I decided to share the method to do this. The following routine will create an Access database from any VBA enabled application, such as Word, Excel, Outlook, etc...

    About the Example:
    The example below creates a database at the root of the C: drive, using your MS Office Username. It also creates a new table "tblSample" with six fields in it. The most interesting part about this is that the code I provided below actually turns on the Unicode compression setting. Why is this important?
    • With Unicode Compression turned off, Access will pad all field inputs with trailing spaces to the maximum number of characters in the field.
    • It saves space in the database, since those spaces are not stored.
    • Without Unicode compression turned on, you will probably need to Trim (remove spaces) from all field values you work with in code.
    Unicode compression is, by default, turned on when you create a table through the UI in Access. It is by default turned OFF though, when you create a table using SQL's CREATE TABLE statement. The "With Compression" (or "With Comp") setting enables Unicode Compression on your database fields. What I found really interesting about this flag, though, is that it ONLY works when you send your SQL statement from an ADO connection. I prefer to test all my SQL through the Access UI before I pull it into VBA code, so this really threw me for a loop for quite a while.

    Code Required:
    The code below goes in a standard module. Don't forget to set a reference to the Microsoft ActiveX Data Objects Library. The following code was developed using the 2.8 version.

    NOTE: There is no error handling in this routine. Running it more than once will result in an error about the database already being created.

    Code:
    Private Sub CreateDatabase()
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: Create an Access database on the fly
        Dim dbConnectStr As String
        Dim Catalog As Object
        Dim cnt As ADODB.Connection
        Dim dbPath As String
    
        'Set database name here
        dbPath = "C:\" & Application.UserName & ".mdb"
        dbConnectStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ";"
    
        'Create new database
        Set Catalog = CreateObject("ADOX.Catalog")
        Catalog.Create dbConnectStr
        Set Catalog = Nothing
    
        'Connect to database and insert a new table
        Set cnt = New ADODB.Connection
        With cnt
            .Open dbConnectStr
            .Execute "CREATE TABLE tblSample ([Name] text(50) WITH Compression, " & _
                     "[Address] text(150) WITH Compression, " & _
                     "[City] text(50) WITH Compression, " & _
                     "[ProvinceState] text(2) WITH Compression, " & _
                     "[Postal] text(6) WITH Compression, " & _
                     "[Account] decimal(6))"
        End With
        Set cnt = Nothing
    End Sub
    Additional Info:
    One challenge with creating Access tables via ADO is that the data types are not named consistently between Access and ADO. If you are trying to create a table via ADO and SQL, you may want to check out this article for a bit of help.

    Also... if you know of, or come across, any better targeted articles for this purpose, please leave a comment below.

    Syndication:
    This article has also been published at Professional Office Developers Association.
    A portugese translation of this article can also be found at MS Office Gurus - Brazil.
  • MVP Logo
  •  Donations

    If you like our website and would like to give something in return, you can make a donation. All donations are gratefully received and go to support the site.


    Select your preferred currency and donation amount, then click the donate button.

  • Recent Forum Posts

    Colo

    How to Rename a File from English to Other Language?

    Yeah, some massive HTML conversion is my line. In other words, most of difficult things can be done with Excel alone like this time. Well done, Excel!...

    Colo Today, 07:36 AM Go to last post
    Ken Puls

    Link a series name to a cell using Excel 2010 VBA

    Hi there,

    I recorded linking the title to a cell and it came back with the following. Does this help?

    Code:
        ActiveChart.SetElement
    ...

    Ken Puls Today, 04:06 AM Go to last post
    Ken Puls

    How to Rename a File from English to Other Language?

    Colo, that was way too easy... I was expecting some massive HTML conversion, or a huge engine to compare each character against a library of Chr codes!...

    Ken Puls Today, 04:02 AM Go to last post
    Ken Puls

    Shared file\macro & IP address

    Oh, and as for the max number of users who can access the file in the shared folder at once...

    • For reading, I believe it's unlimited. (The second and
    ...

    Ken Puls Today, 03:59 AM Go to last post
    Ken Puls

    Shared file\macro & IP address

    ibrahimaa,

    There is no one-line way to get your IP address the way you are getting the username. So you're going to need more code than...

    Ken Puls Today, 03:56 AM Go to last post