问题场景
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