Solved

Drop and Copy Tables

Posted on 2000-02-23
4
291 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
Comment Utility
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
Comment Utility
ADO
cn.Execute "SELECT INTO ..."
cn.Execute "DROP TABLE ..."
0
 
LVL 2

Author Comment

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

Expert Comment

by:waty
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

11 Experts available now in Live!

Get 1:1 Help Now