?
Solved

copying an access table

Posted on 2001-07-30
3
Medium Priority
?
210 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 100 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

839 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