Solved

Drop and Copy Tables

Posted on 2000-02-23
4
292 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

912 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now