Solved

copying an access table

Posted on 2001-07-30
3
204 Views
Last Modified: 2010-05-19
i have a table called "Table1"

i want to make a copy of this table called "Table2"

then remove "Table1"

and rename "Table2" to "Table1"


0
Comment
Question by:Joshyy
  • 2
3 Comments
 
LVL 6

Expert Comment

by:VK
ID: 6333737
Function CloneTDef(RefTDef As TableDef) As TableDef
    On Local Error GoTo Fehler
    '____________________________________________

    Dim NewTDef As TableDef
   
    Dim RefField As Field
    Dim NewField As Field
   
    Dim RefInd As Index
    Dim NewInd As Index
   
    Dim RefProp As Property
   
    '____________________________________________
    Set NewTDef = New TableDef
   
    '____________________________________________PROPERTIES
    For Each RefProp In RefTDef.Properties
        NewTDef.Properties(RefProp.Name).Type = RefProp.Type
        NewTDef.Properties(RefProp.Name).Value = RefProp.Value
        DoEvents
    Next
    NewTDef.Properties.Refresh
   
    '____________________________________________FIELDS
    For Each RefField In RefTDef.Fields
        Set NewField = NewTDef.CreateField
        For Each RefProp In RefField.Properties
            NewField.Properties(RefProp.Name).Type = RefProp.Type
            NewField.Properties(RefProp.Name).Value = RefProp.Value
            DoEvents
        Next
        NewTDef.Fields.Append NewField
    Next
    NewTDef.Fields.Refresh
   
    '____________________________________________INDEXES
   
    For Each RefInd In RefTDef.Indexes
        Set NewInd = NewTDef.CreateIndex

        For Each RefProp In RefInd.Properties
            NewInd.Properties(RefProp.Name).Type = RefProp.Type
            NewInd.Properties(RefProp.Name).Value = RefProp.Value
            DoEvents
        Next
       
        For Each RefField In RefInd.Fields
            Set NewField = NewInd.CreateField
            For Each RefProp In RefField.Properties
                NewField.Properties(RefProp.Name).Type = RefProp.Type
                NewField.Properties(RefProp.Name).Value = RefProp.Value
                DoEvents
            Next
            NewInd.Fields.Append NewField
        Next
        NewTDef.Indexes.Append NewInd
    Next
   
    NewTDef.Indexes.Refresh
   
    '____________________________________________
           
    Set CloneTDef = NewTDef
   
   
    Exit Function
Fehler:
    Select Case Err
        Case 3001, 3219, 3251, 3267, 3270, 3420
            Resume Next
        Case Else
            MsgBox Error
            Exit Function
    End Select
End Function


Sub ReplaceTable(NewTDef As TableDef, db As Database)
    On Local Error GoTo Fehler
    '____________________________________________
    If Not GetTableDef(db, NewTDef.Name) Is Nothing Then
        db.TableDefs.Delete (NewTDef.Name)
        db.TableDefs.Refresh
    End If
    db.TableDefs.Append NewTDef
    db.TableDefs.Refresh
    '____________________________________________
    Exit Sub
Fehler:
    Select Case Err
        Case Else
            MsgBox Error
            Exit Sub
    End Select
End Sub

Call ReplaceTable(CloneTDef(TDef), db)
0
 
LVL 6

Expert Comment

by:VK
ID: 6333739
sorry, a function is missing:

Public Function GetTableDef(db As Database, tabelle As String) As TableDef
    On Local Error GoTo Fehler
    '------------------------------------------------------------------------------------------
    Dim TDef As TableDef
    Set GetTableDef = Nothing
   
    For Each TDef In db.TableDefs
        If TDef.Name = tabelle Then
            Set GetTableDef = TDef
            Exit For
        End If
    Next
   
    '------------------------------------------------------------------------------------------
    Exit Function
Fehler:
    Select Case Err
        Case Else
            MsgBox Error
    End Select
End Function
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 25 total points
ID: 6333750
1) Select Table1.* Into Table2 From Table1
2) Drop Table Table1
3) Various ways
i) Using access automation (DoCmd Rename)
ii) Using DAO (tabledefs collection)
iii) Using ADOX (Tables collection)

I can give you an example for any of the three.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to open Waze.com/livemap from address saved in DB? 26 211
MsgBox 2 59
VBA: Select SQL query based on a config Sheet v2 11 49
Visual Studio 2005 text editor 10 44
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question