iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 22
1
自我挑戰組

Access VBA 之 iT管理實做系列 第 22

Access VBA 之 iT管理實做Day22: 資料轉出成文字檔

有時候,會需要把MDB的內容轉出來比較,這部份筆者是用來對照其他程式使用的MDB資料檔,其中程式動作時,實際資料檔有什麼異動,此時,可以透過以下程式把資料表都轉成純文字檔,再透過Windows下的sort指令來排序,最後再透過程式來對照,看其中的差異。

ExportAllTables:

Sub ExportAllTables(Optional strMDB = "", Optional strPassword = "", Optional strBase = "", Optional bnOnlyNativeTable = True, Optional bnSortTextContent = False, Optional bnSilence = False, Optional strFolder = "")
'匯出所有資料表
'strMDB 如為外部檔案,指定檔案位置
'strPassword 外部MDB檔案的密碼
'strBase 資料存放位置,未指定則存在跟現有MDB檔案相同位置
'bnOnlyNativeTable 是否只有本地資料表
'bnSortTextContent 是否使用Windows command line的sort指令排序
'bnSilence 是否為安靜模式,此模式下不會有任何MsgBox訊息
'strFolder 檔案存放的資料夾名稱,如不自訂,則以資料檔的名稱當做資料夾名稱

'    Dim myDatabase As Database
'    Dim myTableDef As TableDef
    Dim strTableName As String
    Dim CheckPath As String
    Dim strCMD As String
    
    On Error GoTo errHandle:
    
    Dim appAccess As New Access.Application

    If strMDB <> "" Then
        'Set myDatabase = appAccess.DBEngine.Workspaces(0).OpenDatabase(strMDB, False, True, strConnect)
        '改採用OpenCurrentDatabase的方式處理,以便轉出外部MDB檔案
        appAccess.OpenCurrentDatabase strMDB, True, strPassword
        Set myDatabase = appAccess.CurrentDb
    Else
        Set appAccess = Application
        Set myDatabase = appAccess.CurrentDb
    End If
    
    If strBase = "" Then strBase = CurrentProject.Path
'    strFolder = CurrentProject.Name
'    strFolder = Mid(strFolder, 1, InStrRev(strFolder, ".") - 1)

    If strFolder = "" Then
        strFolder = myDatabase.Name
        strFolder = Mid(strFolder, 1, InStrRev(strFolder, ".") - 1)
        strFolder = Mid(strFolder, InStrRev(strFolder, "\") + 1, Len(strFolder))
    End If
    
    CheckPath = strBase & "\" & strFolder
    If Dir(CheckPath, vbDirectory) = "" Then    '目錄不存在時
        MakeDir (CheckPath)                       '建立目錄
    End If
    
    i = 1
    For Each myTableDef In myDatabase.TableDefs
        DoEvents
        strTableName = myTableDef.Name
        Debug.Print i & "/" & myDatabase.TableDefs.Count & ": " & strTableName
        bnDo = False
        If bnOnlyNativeTable = False Then
            bnDo = True
            
        ElseIf Len(myTableDef.Connect) = 0 Then
            bnDo = True
            
        End If
                    
        If bnDo = True Then
            strTempFile = Environ("TEMP") & "\" & "TEMP" & ".CSV"
            strDestFile = CheckPath & "\" & strTableName & ".CSV"
            
            If bnSortTextContent = True Then
                strTranFile = strTempFile
            Else
                strTranFile = strDestFile
            End If
            
            appAccess.DoCmd.TransferText _
                acExportDelim, _
                , _
                strTableName, _
                strTranFile, _
                True
                
            If bnSortTextContent = True Then
                strTempFile = Chr(34) & strTempFile & Chr(34)
                strDestFile = Chr(34) & strDestFile & Chr(34)
                strCMD = "sort " & strTempFile & " /o " & strDestFile
                RunCMD strCMD, True, True, 0
                
            End If
            
        End If
        i = i + 1
    Next myTableDef
    If bnSilence = False Then MsgBox "Done"
    
    Exit Sub
    
errHandle:
    Debug.Print err.Number & ": " & err.DESCRIPTION
    Resume Next
    
End Sub

上一篇
Access VBA 之 iT管理實做Day21: 整合外部程式-AutoHotKey
下一篇
Access VBA 之 iT管理實做Day23: 整合外部程式-DiffMerge
系列文
Access VBA 之 iT管理實做30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言