問題場景
Run_No | Source_Address | Source_File | Destination_Address | Destination_File | Copy_Indicator | Run_Start_Time |
---|---|---|---|---|---|---|
1 | C:\Users\EP\path\to\File | SSS-1.MDB | C:\Users\EP\path\to\File | SSC-1.MDB | Y | |
2 | C:\Users\EP\path\to\File | SSS-2.MDB | C:\Users\EP\path\to\File | SSC-2.MDB | Y | |
3 | C:\Users\EP\path\to\File | SSS-3.MDB | C:\Users\EP\path\to\File | SSC-3.MDB | N | |
4 | C:\Users\EP\path\to\File | SSS-4.MDB | C:\Users\EP\path\to\File | SSC-4.MDB | N | |
5 | C:\Users\EP\path\to\File | SSS-5.MDB | C:\Users\EP\path\to\File | SSC-5.MDB | N | |
6 | C:\Users\EP\path\to\File | SSS-6.MDB | C:\Users\EP\path\to\File | SSC-6.MDB | Y |
注意
:表格的標題不僅僅是該單元格的值,更是將該單元格命名為對應的名稱,例如 “Run_No” 所在單元格的名稱是 “Run_No” 、"Copy_Indicator " 所在單元格的名稱是 "Copy_Indicator " 。
執行邏輯
:試圖構建主函數 Sub
和兩個子函數 Function
,實現以下功能:
- 主函數
Sub
:- 將需要復制的資料表和查詢的名稱分別儲存成數組;
- 循環 “Run_No” ,每次循環都需要根據 “Copy_Indicator” 判斷是否需要繼續執行剩余代碼,如果是"Y"就執行,否則就跳過;
- 根據前一步是 “Y”,組合來源文件和目標文件,即將本次循環中同一行的 “Source_Address” 和 “Source_File” 組合以及 “Destination_Address” 和 “Destination_File” 組合,并打開這兩文件;
- for循環資料表和查詢的名稱組成的數組,每次調用兩個子函數
Function
,將需要的資料表和查詢通過這兩個子函數從來源文件完整復制到目標文件。
- 子函數
Function
- 資料表子函數
- 輸入資料表名稱、來源文件和目標文件,實現資料表的完美復制(包括數據結構和SQL)。
- 查詢子函數
- 輸入查詢名稱、來源文件和目標文件,實現查詢的完美復制。
- 資料表子函數
代碼描述
-
主函數
Sub CopyDatabaseObjects
:- 遍歷每一行,檢查 “Copy_Indicator” 是否為 “Y”,如果是,則組合文件路徑,并打開數據庫文件進行復制操作。
-
子函數
CopyTable
和CopyQuery
:- 函數接收資料表和查詢的名稱,然后從源數據庫復制到目標數據庫。
總結
中文
Sub CopyDatabaseObjects()' 定義數組存儲資料表和查詢的名稱Dim tables() As StringDim queries() As String' 示例數據,需要根據實際情況填充tables = Array("Table1", "Table2")queries = Array("Query1", "Query2")Dim i As IntegerDim sourcePath As StringDim destinationPath As String' 循環處理每一行For i = 1 To 6 ' 假設有6次運行,應根據實際行數進行修改(或者根據之前的文章修改,有很多循環方法)' 檢查是否需要復制If Cells(i + 1, 6).Value = "Y" Then' 組合文件路徑sourcePath = Cells(i + 1, 2).Value & "\" & Cells(i + 1, 3).ValuedestinationPath = Cells(i + 1, 4).Value & "\" & Cells(i + 1, 5).Value' 打開源文件和目標文件Dim srcDB As Object, destDB As ObjectSet srcDB = OpenDatabase(sourcePath)Set destDB = OpenDatabase(destinationPath)' 復制資料表Dim j As IntegerFor j = LBound(tables) To UBound(tables)Call CopyTable(tables(j), srcDB, destDB)Next j' 復制查詢For j = LBound(queries) To UBound(queries)Call CopyQuery(queries(j), srcDB, destDB)Next j' 關閉數據庫srcDB.ClosedestDB.CloseEnd IfNext i
End SubFunction CopyTable(tableName As String, srcDB As Object, destDB As Object)' 復制資料表srcDB.TableDefs(tableName).CopyStructureAndData destDB, tableName
End FunctionFunction CopyQuery(queryName As String, srcDB As Object, destDB As Object)' 復制查詢destDB.CreateQueryDef(queryName, srcDB.QueryDefs(queryName).SQL)
End Function
英文
Sub CopyDatabaseObjects()' Define the names of the array storage tables and queriesDim tables() As StringDim queries() As Stringtables = Array("Table1", "Table2")queries = Array("Query1", "Query2")Dim i As IntegerDim sourcePath As StringDim destinationPath As String' Loop through each rowFor i = 1 To 6 ' Check whether replication is requiredIf Cells(i + 1, 6).Value = "Y" Then' Combined file pathsourcePath = Cells(i + 1, 2).Value & "\" & Cells(i + 1, 3).ValuedestinationPath = Cells(i + 1, 4).Value & "\" & Cells(i + 1, 5).Value' OpenDim srcDB As Object, destDB As ObjectSet srcDB = OpenDatabase(sourcePath)Set destDB = OpenDatabase(destinationPath)' Copy TableDim j As IntegerFor j = LBound(tables) To UBound(tables)Call CopyTable(tables(j), srcDB, destDB)Next j' Copy QueryFor j = LBound(queries) To UBound(queries)Call CopyQuery(queries(j), srcDB, destDB)Next j' ClosesrcDB.ClosedestDB.CloseEnd IfNext i
End SubFunction CopyTable(tableName As String, srcDB As Object, destDB As Object)srcDB.TableDefs(tableName).CopyStructureAndData destDB, tableName
End FunctionFunction CopyQuery(queryName As String, srcDB As Object, destDB As Object)destDB.CreateQueryDef(queryName, srcDB.QueryDefs(queryName).SQL)
End Function