Results 1 to 6 of 6

Thread: VB & VBA - Copy Table Structure Only

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    76

    VB & VBA - Copy Table Structure Only

    Greetings all, my first post on this forum

    My question relates to "code wise" copying of the structure of a table.

    Actually, more completely, what I need to do is to refresh a table with data from 2 fixed length text files.

    My first 3 concepts, I don't like:
    1. Delete the table and programmatically recreate it with tabledef, createindex, createfield.... etc. This is fast, but A lot of code for what should be a simple process.

    2. Select ...into table... with the select being a criteria that would select nothing.
    3. accessing the table. selecting all the rows and deleting them.

    These second two options are simple, but SLOW!!!

    If I was to do this manually, I would ...
    1. rename the table
    2. Copy the table
    3. Paste special/Structure only.
    This is fast and simple...but manual...I need to do it programmactically.

    and then use the code to populate the table from the txt files.


    Does anyone know the "Simple" and "fast" way of completing this task?


    Thanks
    Michael

  2. #2
    PWNettle
    Guest
    If you're doing this in Access VBA you could use DoCmd.CopyObject to make a copy of your table:
    Code:
    DoCmd.CopyObject , "NewTableName", acTable, "SourceTableName"
    Then you could use a quick DELETE SQL statement via the Execute method of the CurrentDb object to delete all the records in the new database:
    Code:
    CurrentDb.Execute "DELETE FROM NewTableName"
    This would give you an empty copy of a table.

    I sort of suspect there's an easier way since you can copy a table structure only within a database, but this works.

    Paul

  3. #3
    PWNettle
    Guest
    Adding another 'empty' reply - because I'm having lots of wierd problems with this forum today (like timeouts and the reply count to this thread showhing zero even though I've replied to it).

    Paul

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    St. Albans, Herts, UK
    Posts
    259
    This code is part of the process of copying a complete database, including tables, indexes, QueryDefs and Relations, but can be adapted to copy a single table. Full code on my site
    VB Code:
    1. Dim dbSrc As Database
    2.     Dim dbNew As Database
    3.     Dim tdefSrc As TableDef
    4.     Dim tdefNew As TableDef
    5.     Dim fdSrc As Field
    6.     Dim fdNew As Field
    7.     Dim ixSrc As Index
    8.     Dim ixNew As Index
    9.  
    10. Set dbSrc = OpenDatabase("SourceDatabase")
    11. Set dbNew = OpenDatabase("NewDatabase")
    12.  
    13. For Each tdefSrc In dbSrc.TableDefs
    14.         'check if system table
    15.         If InStr(1, tdefSrc.Name, "MSys", vbTextCompare) = 0 Then
    16.             'not a system table
    17.             Set tdefNew = dbNew.CreateTableDef(tdefSrc.Name)
    18.             tdefNew.ValidationRule = tdefSrc.ValidationRule
    19.             tdefNew.ValidationText = tdefSrc.ValidationText
    20.             For Each fdSrc In tdefSrc.Fields
    21.                 'check for replication fields s_GUID, s_Generation, s_Lineage, Gen_XXX
    22.                 If InStr(1, fdSrc.Name, "s_", vbTextCompare) = 0 And InStr(1, fdSrc.Name, "Gen_", vbTextCompare) = 0 Then
    23.                     Set fdNew = tdefNew.CreateField(fdSrc.Name, fdSrc.Type, fdSrc.Size)
    24.                     On Error Resume Next
    25.                     fdNew.Attributes = fdSrc.Attributes
    26.                     fdNew.AllowZeroLength = fdSrc.AllowZeroLength
    27.                     fdNew.DefaultValue = fdSrc.DefaultValue
    28.                     fdNew.Required = fdSrc.Required
    29.                     fdNew.Size = fdSrc.Size
    30.                     tdefNew.Fields.Append fdNew
    31.                     On Error GoTo 0
    32.                 End If
    33.             Next
    34.             'now copy indexes
    35.             For Each ixSrc In tdefSrc.Indexes
    36.                 'Check for replication indices s_GUID, s_Generation
    37.                 If InStr(1, ixSrc.Name, "s_", vbTextCompare) = 0 Then
    38.                     'Don't copy indices set as part of Relation Objects
    39.                     If Not ixSrc.Foreign Then
    40.                         Set ixNew = tdefNew.CreateIndex(ixSrc.Name)
    41.                         ixNew.Clustered = ixSrc.Clustered
    42.                         ixNew.IgnoreNulls = ixSrc.IgnoreNulls
    43.                         ixNew.Primary = ixSrc.Primary
    44.                         ixNew.Required = ixSrc.Required
    45.                         ixNew.Unique = ixSrc.Unique
    46.                         'Add Index field(s)
    47.                         For Each fdSrc In ixSrc.Fields
    48.                             Set fdNew = ixNew.CreateField(fdSrc.Name)
    49.                             fdNew.Attributes = fdSrc.Attributes
    50.                             ixNew.Fields.Append fdNew
    51.                         Next
    52.                         tdefNew.Indexes.Append ixNew
    53.                     End If
    54.                 End If
    55.             Next
    56.             dbNew.TableDefs.Append tdefNew
    57.         End If
    58.     Next
    Graham, www.gab2001uk.com VBExplorer Forum Moderator VBExplorer
    www.gab2001uk.com For comparing and contrasting DAO with ADO
    Code for Creating, Copying, Compacting, Replicating, Synchronising Access 97/2000 databases plus showing Schemas and using .Seek

  5. #5
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Better late than never

    Public Sub Copy_Table(ByVal SrcTable As String, _
    ByVal DstTable As String, _
    Optional ByVal SrcDatabase As String = "", _
    Optional ByVal StructureOnly As Boolean = False)
    ' To copy from another Access DB specify the full path\name in SrcDatabase

    SrcDatabase = Trim(UCase(SrcDatabase))

    ' If the source DB wasn't specified use this one
    If SrcDatabase = "" Or SrcDatabase = "CURRENTDB" Then _
    SrcDatabase = CurrentProject.FullName

    DoCmd.TransferDatabase acImport, "Microsoft Access", _
    SrcDatabase, acTable, SrcTable, DstTable, StructureOnly

    End Sub ' Copy_Table

  6. #6
    New Member
    Join Date
    Aug 2005
    Posts
    3

    Angry Re: VB & VBA - Copy Table Structure Only

    I have a DB on SQL server 2008 R2. On my laptop I only have Access 2007. I would like to create an Access Back end so that I can develop on holiday
    If you use the SQL server Copy Wizard you lose the Primary Key and Identity
    ( there is supposed to be Enable Identity option but it does not appear for me)
    If in Access you Import the tables you get the IDENTITY ( Autonumber) but lose the primary key
    If in Access you copy a linked table ( Structure and Data) you get the Primary key and the Identity. But you have to do that manually - I have over 100 tables

    The TransferDatabase copies a linked table to a linked table
    So does CopyObject.

    Is there an easy solution?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width