【问题标题】:VBA Access Database - Clean up code by the use of a loop with 2 variablesVBA Access Database - 通过使用带有 2 个变量的循环来清理代码
【发布时间】:2015-08-02 23:06:18
【问题描述】:

我正在尝试编写在 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:\370\Hyperviseur\SITUATIE\Macro\Stock_getdata.xlsm"
WipsPath = "F:\370\Hyperviseur\SITUATIE\Macro\Wips_getdata.xlsm"
CcaPath = "F:\370\Hyperviseur\SITUATIE\Macro\SLAcc.xls"
EpsPath = "F:\370\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

【问题讨论】:

    标签: arrays vba import ms-access-2010 excel-2010


    【解决方案1】:

    我对 Access 的兴趣不大,但如果你将 4 条路径放入一个数组中,然后围绕应该工作的数组循环。

    Dim Paths(7)
    Paths(0) = "Stock_CC"
    Paths(1) = "F:\370\Hyperviseur\SITUATIE\Macro\Stock_getdata.xlsm"
    Paths(2) = "Wips_CC"
    Paths(3) = "F:\370\Hyperviseur\SITUATIE\Macro\Wips_getdata.xlsm"
    Paths(4) = "CCA_cc"
    Paths(5) = "F:\370\Hyperviseur\SITUATIE\Macro\SLAcc.xls"
    Paths(6) = "Eps_cc"
    Paths(7) = "F:\370\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
    

    你可能想在消息框中放一些东西来区分你所处的循环。

    【讨论】:

    • 你发这个帖子秒杀我! :cP 我给了你 +1 评论你对消息框注释的评论 - 就在那里!
    • 干杯,只是为了澄清任何人都看这两个答案的方式会略有不同。
    • 下一行出现错误:If FileExist(StockPath) Then - StockPath 未定义,我不知道需要用什么替换它。
    • 我已经做出了改变。好地方。
    【解决方案2】:
    Private Sub Command5_Click()
        Dim fileInfoToBeImported(3, 1)
    
        fileInfoToBeImported(0, 0) = "Stock_CC"
        fileInfoToBeImported(0, 1) = "F:\370\Hyperviseur\SITUATIE\Macro\Stock_getdata.xlsm"
    
        fileInfoToBeImported(1, 0) = "Wips_CC"
        fileInfoToBeImported(1, 1) = "F:\370\Hyperviseur\SITUATIE\Macro\Wips_getdata.xlsm"
    
        fileInfoToBeImported(2, 0) = "CCA_cc"
        fileInfoToBeImported(2, 1) = "F:\370\Hyperviseur\SITUATIE\Macro\SLAcc.xls"
    
        fileInfoToBeImported(3, 0) = "Eps_cc"
        fileInfoToBeImported(3, 1) = "F:\370\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
    

    【讨论】:

    • 下一行出现错误:If FileExist(StockPath) Then - StockPath 未定义
    • 感谢 Nathalii,代码现在已更改 - 应该传入实际的文件名!
    • 我选择这个解决方案,因为我认为它在未来更容易改变。
    猜你喜欢
    • 2016-01-06
    • 2014-04-09
    • 2022-10-25
    • 2016-02-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    相关资源
    最近更新 更多