Results 1 to 11 of 11
  1. #1
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Create tables from VBA

    I've been researching the whole day, trying to find a complete way to create tables.


    These two Microsoft helps come up very short.
    CREATE TABLE statement (Microsoft Access SQL) | Microsoft Docs
    Microsoft Access Data Types - Open Database Connectivity (ODBC) | Microsoft Docs

    This code works, but I can't find what to use to get a decimal.

    Code:
    Public Sub CreateTableX2()
      Dim wrkDb As Database
      Set wrkDb = CurrentDb
      wrkDb.Execute "CREATE TABLE SqlCrTableX2 " _
        & "(aShortText varchar(23), " _
        & "aLongText LONGTEXT, " _
        & "aNumberByte BYTE, " _
        & "aNumberInteger SHORT, " _
        & "aNumberLongInteger LONG, " _
        & "aNumberSingle SINGLE, " _
        & "aNumberDouble DOUBLE, " _
        & "aNumberReplication GUID, " _
        & "aNumberDecimal NUMERIC, " _
        & "aDateTime DATETIME, " _
        & "aCurrency CURRENCY, " _
        & "aAutoNumber COUNTER, " _
        & "aYesNo BINARY, " _
        & "CONSTRAINT SqlCrTableX2Constraint PRIMARY KEY " _
        & "(aShortText)" _
        & ");"
      wrkDb.Close
    End Sub
    What I also can't find is if there is a way to add all the rest of the properties to each field, like format, caption, default value, etc. to CREATE TABLE.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Review
    https://www.tek-tips.com/viewthread.cfm?qid=1172710
    https://stackoverflow.com/questions/...th-alter-table

    Some properties cannot be edited with SQL and requires VBA using DAO TableDefs. Example:
    Code:
            If rs!DataType = "Text" Then
                'change the AllowZeroLength default Yes to No
                db.TableDefs(Me.tbxTestNum).Fields(rs!DataField).AllowZeroLength = False
            ElseIf rs!DataType = "Number" Then
                'make sure number field does not have 0 as DefaultValue
                db.TableDefs(Me.tbxTestNum).Fields(rs!DataField).DefaultValue = ""
            End If
    Those properties you do reference, I would advise not to set in table.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,002
    This thread has some thoughts on this issue.
    https://www.access-programmers.co.uk.../#post-1797089
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    there shouldnt be any reason to create tables in code.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Also, here are a couple of subs I retrieved from some older testing

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : AlterTable
    ' Author    : Jack
    ' Created   : 7/10/2011
    ' Purpose   : To alter a table with a field of Long Integer datatype to Decimal.
    ' Must use ADO based on a lot of Google work.
    '---------------------------------------------------------------------------------------
    ' Last Modified:
    '
    ' Inputs: N/A
    ' Dependency: N/A
    '------------------------------------------------------------------------------
    '
    Private Sub AlterTable()
          Dim cnn As New ADODB.Connection
    10       On Error GoTo AlterTable_Error
    
    20    Set cnn = CurrentProject.Connection
    30    cnn.Execute ("Alter TABLE [TEST] ALTER COLUMN [ID] Decimal(7,3)")
    
    40    Set cnn = Nothing
    
    50       On Error GoTo 0
    60       Exit Sub
    
    AlterTable_Error:
    
    70        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure AlterTable of Module AWF_Related"
    
    End Sub
    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : CreateTable
    ' Author    : Jack
    ' Created   : 7/10/2011
    ' Purpose   : Create a table using ADO and SQL
    '---------------------------------------------------------------------------------------
    ' Last Modified:
    '
    ' Inputs: N/A
    ' Dependency: N/A
    '------------------------------------------------------------------------------
    '
    Private Sub CreateTable()
          Dim cnn As New ADODB.Connection
    
    10       On Error GoTo CreateTable_Error
    
    20    Set cnn = CurrentProject.Connection
    30    cnn.Execute ("CREATE TABLE [TEST] ([ID] Long NOT NULL, [MyTextField] Text (2), [MyDecimalField] Decimal(10,6))")
    
    40    Set cnn = Nothing
    
    50       On Error GoTo 0
    60       Exit Sub
    
    CreateTable_Error:
    
    70        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CreateTable of Module AWF_Related"
    
    End Sub

  7. #7
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Sample didn't work, need to turn something on?

    Quote Originally Posted by June7 View Post
    Review
    https://www.tek-tips.com/viewthread.cfm?qid=1172710
    https://stackoverflow.com/questions/...th-alter-table
    ...
    Those properties you do reference, I would advise not to set in table.
    I tried the sample in the first link. I was getting this compile error in another accdb yesterday, in code that had previously run.
    Is there something that needs to be enabled for this to work, or do I have to look for corruption in Access, or should it not work on Access 2021?
    Click image for larger version. 

Name:	220810CompErr1.jpg 
Views:	15 
Size:	107.9 KB 
ID:	48485

    I'm not sure what you mean by the last sentence.

  8. #8
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    Quote Originally Posted by ranman256 View Post
    there shouldnt be any reason to create tables in code.
    If you remember the old Table Wizard from Access 1.0, even though it's been removed, it did what I want to do, only on steroids (mine, not the one from M$).
    Building tables by hand is an invitation for inconsistency in naming, design and errors (I like my errors to be consistent).

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,906
    You likely need the ADO reference?
    You appear to be obsessed with access corruption?
    Why use a product you do not appear to have any faith in?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @ Orange Oh yes, those are very good examples. Why can't I find those in my searches? Gotta know the correct lingo I guess. Thanks!

  11. #11
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @ Welshgasman answered in post #8 and created forty-plus years ago in another dbs. Served me very well over the years when dealing with dozens of new enterprise clients wanting mods to things like captions in a db with 100+ tables. Don't they teach to write programs to write programs anymore? Or does everyone want to twitch with a mouse for weeks and call that productivity? See this for a small example of how to write programs to write programs: https://www.accessforums.net/showthr...728#post498728

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 17
    Last Post: 07-18-2022, 10:49 AM
  2. Create a table from 2 other tables
    By Cyclops in forum Access
    Replies: 8
    Last Post: 05-12-2020, 12:15 PM
  3. Replies: 3
    Last Post: 07-24-2015, 06:39 AM
  4. How many Tables to create for data
    By cpill in forum Access
    Replies: 3
    Last Post: 04-26-2011, 05:50 PM
  5. what tables to create
    By mecca in forum Access
    Replies: 9
    Last Post: 11-04-2010, 02:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums