How to extract the schema of an Access (.mdb) database?

后端 未结 10 2544
星月不相逢
星月不相逢 2020-11-30 19:39

I am trying to extract the schema of an .mdb database, so that I can recreate the database elsewhere.

How can I pull off something like this?

10条回答
  •  我在风中等你
    2020-11-30 20:04

    Roland's answer above (edited by Tobias) worked for me but with a couple of changes. Firstly I solved the issue of finding all fields in the primary key; then the writing to file of the index sql code was in the wrong place: Option Compare Database

    Function exportTableDefs()
    
    Dim db As Database
    Dim tdf As TableDef
    Dim fld As DAO.Field
    Dim ndx As DAO.Index
    Dim strSQL As String
    Dim strFlds As String
    
    Dim fs, f
    
        Set db = CurrentDb
    
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.CreateTextFile("C:\temp\Schema.txt")
    
        For Each tdf In db.TableDefs
            If Left(tdf.Name, 4) <> "Msys" And Left(tdf.Name, 1) <> "~" Then
                strSQL = "CREATE TABLE [" & tdf.Name & "] (" & vbCrLf
    
                strFlds = ""
    
                For Each fld In tdf.Fields
    
                    strFlds = strFlds & ",[" & fld.Name & "] "
    
                    Select Case fld.Type
    
                        Case dbText
                            'No look-up fields
                            strFlds = strFlds & "varchar (" & fld.SIZE & ")"
    
                        Case dbLong
                            If (fld.Attributes And dbAutoIncrField) = 0& Then
                                strFlds = strFlds & "bigint"
                            Else
                                strFlds = strFlds & "int IDENTITY(1,1)"
                            End If
    
                        Case dbBoolean
                            strFlds = strFlds & "bit"
    
                        Case dbByte
                            strFlds = strFlds & "tinyint"
    
                        Case dbInteger
                            strFlds = strFlds & "int"
    
                        Case dbCurrency
                            strFlds = strFlds & "decimal(10,2)"
    
                        Case dbSingle
                            strFlds = strFlds & "decimal(10,2)"
    
                        Case dbDouble
                            strFlds = strFlds & "Float"
    
                        Case dbDate
                            strFlds = strFlds & "DateTime"
    
                        Case dbBinary
                            strFlds = strFlds & "binary"
    
                        Case dbLongBinary
                            strFlds = strFlds & "varbinary(max)"
    
                        Case dbMemo
                            If (fld.Attributes And dbHyperlinkField) = 0& Then
                                strFlds = strFlds & "varbinary(max)"
                            Else
                                strFlds = strFlds & "?"
                            End If
    
                        Case dbGUID
                            strFlds = strFlds & "?"
                        Case Else
                            strFlds = strFlds & "?"
    
                    End Select
                    strFlds = strFlds & vbCrLf
    
                Next
    
                ''  get rid of the first comma
                strSQL = strSQL & Mid(strFlds, 2) & " )" & vbCrLf
    
                f.WriteLine strSQL
    
                strSQL = ""
    
                'Indexes
                For Each ndx In tdf.Indexes
    
                    If Left(ndx.Name, 1) <> "~" Then
                        If ndx.Primary Then
                            strSQL = "ALTER TABLE " & tdf.Name & " ADD  CONSTRAINT " & tdf.Name & "_primary" & " PRIMARY KEY CLUSTERED ( " & vbCrLf
                        Else
                            If ndx.Unique Then
                                strSQL = "CREATE UNIQUE NONCLUSTERED INDEX "
                            Else
                                strSQL = "CREATE NONCLUSTERED INDEX "
                            End If
                            strSQL = strSQL & "[" & tdf.Name & "_" & ndx.Name & "] ON [" & tdf.Name & "] ("
                        End If
    
                        strFlds = ""
    
                        '''  use the ndx collection rather than tdf
                        For Each fld In ndx.Fields
                            strFlds = strFlds & ",[" & fld.Name & "] ASC "
                            Exit For
                        Next
    
                        strSQL = strSQL & Mid(strFlds, 2) & ") "
                    End If
                    ''' write to file for each iteration of the loop to get multiple indexes
                    f.WriteLine strSQL & vbCrLf
                Next
            End If
        Next
    
        f.Close
    
    End Function
    

提交回复
热议问题