Advertisement
2002ASP Databases/ Data Access/ DAO/ ADO #49

Copy a database table

How to copy a database table. This may require some tweaking.... "Bill Pearson"

AI

AI Samenvatting: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Broncode
original-source
Private Sub Form_Load()
  
  Dim dbFrom As Database
  Dim dbTo  As Database
  
  Set dbFrom = workspaces(0).opendatabase("c:\vb4\biblio.mdb")
  Set dbTo = workspaces(0).opendatabase("c:\vb4\biblio.mdb")
  
  db_Copy_Tabledef dbFrom, dbTo, "Authors", "CopyOfAuthors"
  
  dbFrom.Close
  dbTo.Close
  
End Sub
Public Function db_Copy_Tabledef(dbFrom As Database, dbTo As Database,
TableNameFrom As String, TableNameTo As String) As Boolean
  
  Dim tdFrom    As TableDef
  Dim tdTo     As TableDef
  Dim fldFrom   As Field
  Dim fldTo    As Field
  Dim ndxFrom   As Index
  Dim ndxTo    As Index
  Dim FunctionName As String
  Dim Found    As Boolean
  
  On Error Resume Next
  
  For Each tdFrom In dbFrom.TableDefs
    
    '-----------------------------
    'Loop until find the table def
    '-----------------------------
    If LCase$(tdFrom.Name) = LCase$(TableNameFrom) Then
     
      Found = True
          
     '----------------------
     'Create Table defintion
     '----------------------
      Set tdTo = dbTo.CreateTableDef(TableNameTo)
      
     '------------------------------
     'Copy each field and attributes
     '------------------------------
      For Each fldFrom In dbFrom.TableDefs(tdFrom.Name).Fields
        Set fldTo = tdTo.CreateField(fldFrom.Name)
        
        fldTo.Type = fldFrom.Type
        fldTo.DefaultValue = fldFrom.DefaultValue
        fldTo.Required = fldFrom.Required
        Select Case fldFrom.Type
         Case dbText
           fldTo.Size = fldFrom.Size
           fldTo.Attributes = fldFrom.Attributes
           fldTo.AllowZeroLength = fldTo.AllowZeroLength
         Case dbMemo
           fldTo.AllowZeroLength = fldTo.AllowZeroLength
         Case Else
        End Select
        
        tdTo.Fields.Append fldTo
      
        If Err.Number > 0 Then
         MsgBox "Error adding field to table " & TableNameTo &
".", vbCritical, FunctionName
         Exit Function
        End If
      Next
      
     '-----------------------
     'Copy Index defintion(s)
     '-----------------------
      For Each ndxFrom In dbFrom.TableDefs(tdFrom.Name).Indexes
        Set ndxTo = tdTo.CreateIndex(ndxFrom.Name)
        
        ndxTo.Required = ndxFrom.Required
        ndxTo.IgnoreNulls = ndxFrom.IgnoreNulls
        ndxTo.Primary = ndxFrom.Primary
        ndxTo.Clustered = ndxFrom.Clustered
        ndxTo.Unique = ndxFrom.Unique
        
       '---------------------
       'Copy each index field
       '---------------------
        For Each fldFrom In
dbFrom.TableDefs(tdFrom.Name).Indexes(ndxFrom.Name).Fields
          Set fldTo = ndxTo.CreateField(fldFrom.Name)
          ndxTo.Fields.Append fldTo
          
          If Err.Number > 0 Then
           MsgBox "Error adding field to index in table " &
TableNameTo & ".", vbCritical, FunctionName
           Exit Function
          End If
        Next
        
        tdTo.Indexes.Append ndxTo
        
        If Err.Number > 0 Then
         MsgBox "Error adding index to table " & TableNameTo &
".", vbCritical, FunctionName
         Exit Function
        End If
      Next
      
      dbTo.TableDefs.Append tdTo
      
      If Err.Number > 0 Then
       MsgBox "Error adding table " & TableNameTo & ".", vbCritical,
FunctionName
       Exit Function
      End If
      
      Exit For
    End If
  Next
  If Found Then
    db_Copy_Tabledef = True
  Else
    MsgBox "Table " & TableNameFrom & " not found.", vbExclamation,
FunctionName
  End If
  
  On Error GoTo 0
End Function

/*	The author of this piece of code is Rahul Khanna
	<[email protected]>
	Use this code at your own risk.
*/
/*	This piece of code demonstrates how you can calculate
	the cofactors, minors and the value of a 3x3 determinant
	itself with the smallest possible code. The cofactor, and
	minors are calculated in one line of code each! Can you
	make it any smaller?
*/
/*	Assuming there is an array "matrix[3][3]" that contains
	the values of the matrix in the format rows x columns.
	The cofactors of the repective matrix element is stored
	in its position values in the array "cofactor". Eg, cofactor
	of array element matrix[1][1] will be stored in cofactor[1][1].
	Same as above with minors.
*/

long row, col;
long matrix[3][3];
long cofactor[3][3], minor[3][3];
long determinant = 0;
for (row = 0; row < 3; row++)
{
	for (col = 0; col < 3; col++)
	{
		cofactor[row][col] = matrix[(row + 1) % 3][(col + 1) % 3] * matrix[(row + 2) % 3][(col + 2) % 3] - matrix[(row + 1) % 3][(col + 2) % 3] * matrix[(row + 2) % 3][(col + 1) % 3];
		minor[row][col] = (row + col) % 2 == 0 ? cofactor : -cofactor;
		if (row == 0)
			determinant += matrix[0][col] * cofactor;
	}
}
Originele reacties (3)
Hersteld van de Wayback Machine