Solved

Drop and Copy Tables

Posted on 2000-02-23
4
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
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…
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…
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…
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

628 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