VBA Access 数据库 - 通过使用带有 2 个变量的循环来清理代码

VBA Access Database - Clean up code by the use of a loop with 2 variables

我正在尝试编写一个代码,在 4 个不同的数据库中导入 4 个不同的文件。我想知道是否有一种方法可以通过使用循环使它变得更短更简单?我试过一个,但我不知道如何将一个文件定向到不同的数据库。

Option Compare Database
Option Explicit

Private Sub Command5_Click()

Dim StockPath As String
Dim WipsPath As String
Dim CcaPath As String
Dim EpsPath As String


StockPath = "F:0\Hyperviseur\SITUATIE\Macro\Stock_getdata.xlsm"
WipsPath = "F:0\Hyperviseur\SITUATIE\Macro\Wips_getdata.xlsm"
CcaPath = "F:0\Hyperviseur\SITUATIE\Macro\SLAcc.xls"
EpsPath = "F:0\Hyperviseur\SITUATIE\Macro\eps.xlsm"

If FileExist(StockPath) Then
    DoCmd.TransferSpreadsheet acImport, , "Stock_CC", StockPath, True
    Else
    MsgBox "Bestanden niet gevonden."
    End If

If FileExist(WipsPath) Then
    DoCmd.TransferSpreadsheet acImport, , "Wips_CC", WipsPath, True
    Else
    MsgBox "Bestanden niet gevonden."
    End If

If FileExist(CcaPath) Then
    DoCmd.TransferSpreadsheet acImport, , "CCA_cc", CcaPath, True
    Else
    MsgBox "Bestanden niet gevonden."
    End If

If FileExist(EpsPath) Then
    DoCmd.TransferSpreadsheet acImport, , "Eps_cc", EpsPath, True
    Else
    MsgBox "Bestanden niet gevonden."
    End If


End Sub


Function FileExist(sTestFile As String) As Boolean
   Dim lSize As Long
   On Error Resume Next
   'Preset length to -1 because files can be zero bytes in length
   lSize = -1
   'Get the length of the file
   lSize = FileLen(sTestFile)
   If lSize > -1 Then
      FileExist = True
   Else
      FileExist = False
   End If
End Function

我不太喜欢 Access,但如果您将 4 条路径放入一个数组中,然后绕着该数组循环应该可以工作。

Dim Paths(7)
Paths(0) = "Stock_CC"
Paths(1) = "F:0\Hyperviseur\SITUATIE\Macro\Stock_getdata.xlsm"
Paths(2) = "Wips_CC"
Paths(3) = "F:0\Hyperviseur\SITUATIE\Macro\Wips_getdata.xlsm"
Paths(4) = "CCA_cc"
Paths(5) = "F:0\Hyperviseur\SITUATIE\Macro\SLAcc.xls"
Paths(6) = "Eps_cc"
Paths(7) = "F:0\Hyperviseur\SITUATIE\Macro\eps.xlsm"

for i =0 to ubound(Paths) step 2
    If FileExist(Paths(i+1)) Then
        DoCmd.TransferSpreadsheet acImport, , Paths(i), Paths(i+1), True
    Else
        MsgBox "Bestanden niet gevonden."
    End If
next

您可能希望在消息框中添加一些内容以区分您所处的循环。

Private Sub Command5_Click()
    Dim fileInfoToBeImported(3, 1)

    fileInfoToBeImported(0, 0) = "Stock_CC"
    fileInfoToBeImported(0, 1) = "F:0\Hyperviseur\SITUATIE\Macro\Stock_getdata.xlsm"

    fileInfoToBeImported(1, 0) = "Wips_CC"
    fileInfoToBeImported(1, 1) = "F:0\Hyperviseur\SITUATIE\Macro\Wips_getdata.xlsm"

    fileInfoToBeImported(2, 0) = "CCA_cc"
    fileInfoToBeImported(2, 1) = "F:0\Hyperviseur\SITUATIE\Macro\SLAcc.xls"

    fileInfoToBeImported(3, 0) = "Eps_cc"
    fileInfoToBeImported(3, 1) = "F:0\Hyperviseur\SITUATIE\Macro\eps.xlsm"

    Dim loopIndex As Integer
    For loopIndex = 0 To UBound(fileInfoToBeImported, 1)
        transferSpreadsheetMethod fileInfoToBeImported(loopIndex, 0), fileInfoToBeImported(loopIndex, 1)
    Next loopIndex
End Sub

Private Sub transferSpreadsheetMethod(ByVal tableName As String, ByVal fileName As String)
    If FileExist(fileName) Then
        DoCmd.TransferSpreadsheet acImport, , tableName, fileName, True
    Else
        MsgBox "Bestanden niet gevonden."
    End If
End Sub