Results 1 to 7 of 7

Thread: Making another copy of a table in the same database

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2001
    Location
    New Delhi
    Posts
    34

    Making another copy of a table in the same database

    Hey guyz,
    i need to make a copy of a existing table in a Access database and save it with another name in the same database.

    Example :
    Database : Test1
    Tables : A,B,C,D

    After Making another copy of table C and Naming it as Z the database should be
    DataBase: Test1
    Tables : A,B,C,D,Z

    See if you Guyz can Help...
    Regards

    Amprat
    **** Happens!

  2. #2
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    use :

    Code:
    select * into Newtable from Oldtable

  3. #3
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    This is pretty simple. I'm not sure if there's a way to directly copy a table, but you can use a make-table query to put all the records from the existing table into a new table with one line of code.

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    The Sql statement is the quickest BUT it may not assign the right values on fields (sometimes it assumes things for you). For example .. double in one table defaults to long in another... which can cause problems later.


    If you have time on your hands, try coding it, but you'll need either DAO (Access) or ADOX to read table designs/properties and create new tables/fields... Then read up on index creating too...



    BTW - Anyone know DAO or ADOX coding for indexes ? I tried in DAO but it wouldn't let me complete a coding version of inserting an index.

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I'm not sure about this because I've never done it, but I did a fair amount of reading of the DAO methods in the VB manuals and I think you have to create an index when you create the field. Some things can't be done after the fact.

    Sort of like this:

    dim Fld as Field

    ' create field properties

    tbl.append fld

    after that it's too late to make some types of changes.

    Sorry I can't tell you more.

  6. #6
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    To add an index with ADOX:
    VB Code:
    1. Private Sub CreateTable(ByVal strDB As String)
    2.     Dim cat As ADOX.Catalog
    3.     Dim tbl As ADOX.Table
    4.     Dim col As ADOX.Column
    5.     Dim idx As ADOX.Index
    6.    
    7.     'get the catalog
    8.     Set cat = New ADOX.Catalog
    9.     cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\" & strDB
    10.        
    11.     'create a column
    12.     Set col = New ADOX.Column
    13.     With col
    14.         .Name = "PK"
    15.         .Type = adInteger
    16.     End With
    17.    
    18.     'create the table
    19.     Set tbl = New ADOX.Table
    20.     With tbl
    21.         .Name = "NewTable"
    22.         .Columns.Append col
    23.     End With
    24.     'append the table
    25.     cat.Tables.Append tbl
    26.    
    27.     'create an index
    28.     Set idx = New ADOX.Index
    29.     With idx
    30.         .Name = "idx_PK"
    31.         .Columns.Append col.Name
    32.         '.Clustered = True  'Access doesn't support clustered indexes
    33.     End With
    34.     'append the index to the table
    35.     tbl.Indexes.Append idx
    36.  
    37. End Sub
    Last edited by PilgrimPete; Apr 3rd, 2002 at 08:28 AM.

  7. #7
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    Vince: Here's the same thing in DAO. Looks very similar doesn't it..?

    VB Code:
    1. Sub CreateTable()
    2.     Dim tdf As TableDef
    3.     Dim fld As Field
    4.     Dim idx As Index
    5.    
    6.     'create the tabledef
    7.     Set tdf = CurrentDb.CreateTableDef("tblNew")
    8.    
    9.     'create a field...
    10.     Set fld = New Field
    11.     With fld
    12.         .Name = "PK"
    13.         .Type = dbInteger
    14.     End With
    15.     '...and add it to the tabledef
    16.     tdf.Fields.Append fld
    17.    
    18.     'create an index...
    19.     Set idx = New Index
    20.     With idx
    21.         .Name = "idx_PK"
    22.         .Fields.Append .CreateField("PK")
    23.         .Primary = True
    24.     End With
    25.     '...and add it to the tabledef
    26.     tdf.Indexes.Append idx
    27.    
    28.     'finally append the tabledef to the database
    29.     CurrentDb.TableDefs.Append tdf
    30.    
    31. End Sub

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