Solved

Drop and Copy Tables

Posted on 2000-02-23
4
294 Views
Last Modified: 2010-05-02
Hi,

   Any Idea how I can drop a table and
   copy another table to the current database from the current database in VB?
0
Comment
Question by:seahpc
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
waty earned 200 total points
ID: 2553546
sSQL = "Drop yourtable"
DB.Execut sSQL

Then copy the struct of another table :
ADO sample
http://www.vb-helper.com/HowTo/dbstruct.zip

DAO sample :

Function CopyDBStruct(DBFrom As Database, sDBTo As String) As Boolean
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site         : www.geocities.com/ResearchTriangle/6311/
   ' * E-Mail           : waty.thierry@usa.net
   ' * Date             : 26/03/1999
   ' * Time             : 23:22
   ' * Module Name      : Database_Module
   ' * Module Filename  : Database.bas
   ' * Procedure Name   : CopyDBStruct
   ' * Parameters       :
   ' *                    DBFrom As Database
   ' *                    sDBTo As String
   ' **********************************************************************
   ' * Comments         : Copy the entire structure of a table
   ' *  from a database to another one
   ' *
   ' **********************************************************************

   ' #VBIDEUtilsERROR#
   On Error GoTo ERROR_CopyDBStruct

   Dim DBTo             As Database
   Dim nI               As Integer
   Dim nJ               As Integer
   Dim tblTableDefObj   As TableDef
   Dim fldFieldObj      As Field
   Dim indIndexObj      As Index
   Dim tdf              As TableDef
   Dim fld              As Field
   Dim idx              As Index

   ' *** Delete the eventual existing output DB
   On Error Resume Next
   Kill sDBTo
   On Error GoTo ERROR_CopyDBStruct

   ' *** Open the output database
   Set DBTo = CreateDatabase(sDBTo, dbLangGeneral)

   ' *** For Each tdf In DB.Tabledefs
   For nI = 0 To DB.TableDefs.count - 1
      Set tdf = DB.TableDefs(nI)

      ' *** Create a new table
      Set tblTableDefObj = DB.CreateTableDef(nI)

      ' *** Strip off owner if needed
      tblTableDefObj.Name = StripOwner(tdf.Name)

      ' *** Create the fields
      For nJ = 0 To tdf.Fields.count - 1
         Set fld = tdf.Fields(nJ)

         ' *** Create this new field
         Set fldFieldObj = tdf.CreateField(fld.Name)
         fldFieldObj.Type = fld.Type
         fldFieldObj.Size = fld.Size
         fldFieldObj.DefaultValue = fld.DefaultValue
         fldFieldObj.Required = fld.Required

         ' *** Add this field
         tblTableDefObj.Fields.Append fldFieldObj
      Next

      ' *** Create the indexes
      For nJ = 0 To tdf.Indexes.count - 1
         Set idx = tdf.Indexes(nJ)

         ' *** Create the index
         Set indIndexObj = tdf.CreateIndex(idx.Name)
         indIndexObj.Fields = idx.Fields
         indIndexObj.Unique = idx.Unique
         indIndexObj.Primary = idx.Primary

         ' *** Add this index
         tblTableDefObj.Indexes.Append indIndexObj
      Next

      ' *** Append this new table
      DBTo.TableDefs.Append tblTableDefObj
Next_Table:
   Next

   DBTo.Close
   Set DBTo = Nothing

   CopyDBStruct = True
   Exit Function

   ' #VBIDEUtilsERROR#
ERROR_CopyDBStruct:
   If Err = 3110 Then
      Resume Next_Table
   End If
   MsgBox "Can not create the output database " & Error, vbCritical
   CopyDBStruct = False
   Exit Function

End Function

0
 
LVL 4

Expert Comment

by:TigerZhao
ID: 2553563
ADO
cn.Execute "SELECT INTO ..."
cn.Execute "DROP TABLE ..."
0
 
LVL 2

Author Comment

by:seahpc
ID: 2553675
hi waty,
  does your example transafer data over to the new table also ?
0
 
LVL 14

Expert Comment

by:waty
ID: 2553713
I have the code at home, not here, but it is very easy to do, 4 lines

for nI = 1 to tblSrc.Recordcount
   for nJ = 1 to tbsrc.Fields.Count
      tblDst.AddNew
      tblDst(nJ) = tblSrc(nJ)
      tblDst.Update
   Next
Next
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Script Host failed (Access is denied.) error 6 223
VBA filters 2 65
VB6 ListBox Question 4 48
Zip Folders Using Chilkat Routines 1 58
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 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