\r\n\r\n
Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > got a way to copy db table structure at runtime


\r\n \r\n
 
 
Thread Tools Display Modes

\r\n\r\n\r\n
Hi i found this code in this forum courtesy of Lory from Singapore (thanks!)
\r\n
\r\nPrivate Sub Command1_Click()
\r\nOn Error GoTo CSErr
\r\n
\r\nDim i As Integer
\r\nDim tblTableDefObj As TableDef
\r\nDim fldFieldObj As Field
\r\nDim tdf As TableDef
\r\nDim fld As Field
\r\n
\r\nDim CurrentDatabase As Database
\r\nDim FTableName As String
\r\nDim ToTableName As String
\r\n
\r\n\'search to see if table exists
\r\n
\r\nSet CurrentDatabase = DBEngine(0).OpenDatabase(App.Path + "\\data1.mdb")
\r\nSet tblTableDefObj = CurrentDatabase.CreateTableDef()
\r\n
\r\nFTableName = "Hang" \'table you want to copy structure
\r\nToTableName = "Copy_of_Hang" \' Name for New table
\r\ntblTableDefObj.Name = ToTableName
\r\n
\r\n\'create the fields
\r\nFor i = 0 To CurrentDatabase.TableDefs(FTableName).Fields.Count - 1
\r\nSet fld = CurrentDatabase.TableDefs(FTableName).Fields(i)
\r\nSet fldFieldObj = CurrentDatabase.TableDefs(FTableName).CreateField(fld.Name, fld.Type, fld.Size)
\r\ntblTableDefObj.Fields.Append fldFieldObj
\r\nNext
\r\n
\r\n\'append the new table
\r\nCurrentDatabase.TableDefs.Append tblTableDefObj
\r\n
\r\nExit Sub
\r\nCSErr:
\r\n\'Err trap
\r\n
\r\nEnd Sub
\r\n
\r\n
\r\nOk, so this works (but its too long), is there any short method to this? and how can I delete the table in case using this same code?
\r\n \r\n\r\n
\r\n \r\n\r\n \r\n \r\n\r\n \r\n \r\n
\r\n __________________
\r\n angel bear
\r\n
\r\n"Don\'t go there!"\r\n
\r\n \r\n \r\n\r\n \r\n\r\n \r\n\r\n
\r\n \r\n \r\n \r\n \r\n Reply With Quote\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n\r\n \r\n\r\n
\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n\r\n\r\n\r\n\r\n'; pd[79285] = '\r\n\r\n \r\n\r\n
\r\n
\r\n
\r\n\r\n
\r\n \r\n\r\n\r\n\r\n\r\n\r\n \r\n\r\n\r\n\r\n \r\n
\r\n
\r\n  \r\n #2  \r\n \r\n \r\n \r\n \r\n \r\n
\r\n\r\n
\r\n \r\n Old\r\n \r\n 01-30-2002, 03:11 AM\r\n \r\n \r\n \r\n
\r\n
\r\n \r\n \r\n \r\n \r\n \r\n
Flyguy\'s Avatar\r\n\r\n
\r\n \r\n Flyguy\r\n Flyguy is offline\r\n\r\n\r\n
Prev Previous Post   Next Post Next
  #1  
Old 01-29-2002, 10:59 PM
angelbear's Avatar
angelbear angelbear is offline
Centurion
 
Join Date: Jan 2002
Posts: 119
Default got a way to copy db table structure at runtime


Hi i found this code in this forum courtesy of Lory from Singapore (thanks!)

Private Sub Command1_Click()
On Error GoTo CSErr

Dim i As Integer
Dim tblTableDefObj As TableDef
Dim fldFieldObj As Field
Dim tdf As TableDef
Dim fld As Field

Dim CurrentDatabase As Database
Dim FTableName As String
Dim ToTableName As String

'search to see if table exists

Set CurrentDatabase = DBEngine(0).OpenDatabase(App.Path + "\data1.mdb")
Set tblTableDefObj = CurrentDatabase.CreateTableDef()

FTableName = "Hang" 'table you want to copy structure
ToTableName = "Copy_of_Hang" ' Name for New table
tblTableDefObj.Name = ToTableName

'create the fields
For i = 0 To CurrentDatabase.TableDefs(FTableName).Fields.Count - 1
Set fld = CurrentDatabase.TableDefs(FTableName).Fields(i)
Set fldFieldObj = CurrentDatabase.TableDefs(FTableName).CreateField(fld.Name, fld.Type, fld.Size)
tblTableDefObj.Fields.Append fldFieldObj
Next

'append the new table
CurrentDatabase.TableDefs.Append tblTableDefObj

Exit Sub
CSErr:
'Err trap

End Sub


Ok, so this works (but its too long), is there any short method to this? and how can I delete the table in case using this same code?
__________________
angel bear

"Don't go there!"
Reply With Quote
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->