具有7个集合或1个数组的MS Access更新表?

时间:2018-10-17 11:31:47

标签: vba ms-access access-vba

因此,请尽量使本节简短: 我有两张桌子。 Tbl1按年分别列出每个公司的财务帐目。 Table2每个公司只有一次,所有财务数据现在都在一行中。 我怎么做?当前尝试使用Collections进行第二次尝试使用Arrays。

嗨,大家好,我有两个桌子Figs1和Sabi。图1设置如下:NIF,期末,物料,折旧,非交易收入,总利息,税前利润,TotalEmpRemu。

因此,您将获得重复的公司ID,并将每个公司的财务数据作为行。

在Sabi中,每个公司都有1行,所有数据都在列中,例如PeriodEnding_Latest,PeriodEnding -1,PeriodEnding -2等,直到-6。我为图1中的每一列进行了收集,我想以正确的顺序更新表Sabi。

因此PeriodEnding集合将具有{(31/12/2018),(31/12/2017),(31/12/2016),(31/12/2015),(31/12/2014),( 2013年12月31日)}

这些值需要转到PeriodEnding_Latest,PeriodEnding -1,PeriodEnding -2等。

我有更新的SQL语句,并用变量填充了它:

    SQL = "UPDATE SabiFigures1 SET SabiFigures1.[Closing Date Last avail yr] = '& DateFiled1 &', SabiFigures1.[Closing Date Year - 1] = '& DateFiled2 &', " & _
          "SabiFigures1.[Closing Date Year - 2] = '& DateFiled3 &', SabiFigures1.[Closing Date Year - 3] = '& DateFiled4 &', SabiFigures1.[Closing Date Year - 4] = '& DateFiled5 &, " & _
          "SabiFigures1.[Closing Date Year - 5] = '& DateFiled6 &', SabiFigures1.[Material costs th EUR Last avail yr] = '2933', SabiFigures1.[Material costs th EUR Year - 1] " & _
          "= '2791', SabiFigures1.[Material costs th EUR Year - 2] = '3721', SabiFigures1.[Material costs th EUR Year - 3] = '3021', SabiFigures1.[Material costs th EUR Year - 4] " & _
         "= '3005', SabiFigures1.[Material costs th EUR Year - 5] = '1890', SabiFigures1.[Depreciation th EUR Last avail yr] = '49', SabiFigures1.[Depreciation th EUR Year - 1] = " & _
         "'52', SabiFigures1.[Depreciation th EUR Year - 2] = '47', SabiFigures1.[Depreciation th EUR Year - 3] = '42', SabiFigures1.[Depreciation th EUR Year - 4] = '54', " & _
        "SabiFigures1.[Depreciation th EUR Year - 5] = '63', SabiFigures1.[Financial revenue th EUR Last avail yr] = Null, SabiFigures1.[Financial revenue th EUR Year - 1] " & _
        "= Null, SabiFigures1.[Financial revenue th EUR Year - 2] = Null, SabiFigures1.[Financial revenue th EUR Year - 3] = Null, SabiFigures1.[Financial revenue th EUR Year " & _
        "- 4] = Null, SabiFigures1.[Financial revenue th EUR Year - 5] = Null, SabiFigures1.[Financial expenses th EUR Last avail yr] = Null, SabiFigures1.[Financial expenses " & _
       "th EUR Year - 1] = Null, SabiFigures1.[Financial expenses th EUR Year - 2] = Null, SabiFigures1.[Financial expenses th EUR Year - 3] = Null, " & _
       "SabiFigures1.[Financial expenses th EUR Year - 4] = Null, SabiFigures1.[Financial expenses th EUR Year - 5] = Null, SabiFigures1." & _
     "[P/L before tax th EUR Last avail yr] = '407', SabiFigures1.[P/L before tax th EUR Year - 1] = '252', SabiFigures1.[P/L before tax th EUR Year - 2] " & _
     "= '1076', SabiFigures1.[P/L before tax th EUR Year - 3] = '597', SabiFigures1.[P/L before tax th EUR Year - 4] = '329', SabiFigures1.[P/L before tax th EUR Year - 5] = " & _
                                            "'102', SabiFigures1.[Cost of employees th EUR Last avail yr] = '1226', SabiFigures1.[Cost of employees th EUR Year - 1] = '1205', SabiFigures1.[Cost of employees th EUR Year - 2] " & _
   "= '1310', SabiFigures1.[Cost of employees th EUR Year - 3] = '1157', SabiFigures1.[Cost of employees th EUR Year - 4] = '1319', SabiFigures1.[Cost of employees th EUR Year - 5] = '1342' " & _
   "WHERE (((SabiFigures1.[NIF Code])='A01011550'));"
   db.Execute SQL

代码几乎转到第一个表:Figs1并获取第一个regnumber,然后将其转到Sabi,在其中我已预先填充了不合格的NIF。如果它在图1和Sabi中找到对应的NIF,则应该填写集合中的变量,但是,我不知道如何执行变量“变量”。例如。变量的基本结构应为DateFiled,但在遍历PeriodEndings的集合时,应从DateFiled1更改为Datefiled6。

Set rsFigs1 = CurrentDb.OpenRecordset("Select * FROM Figs1Ready ORDER BY NIF, PeriodEnding DESC;")
    If Not (rsFigs1.EOF And rsFigs1.BOF) Then
        rsFigs1.MoveFirst
        Do Until rsFigs1.EOF = True
            NIF = rsFigs1!NIF
            Set rsFormat = CurrentDb.OpenRecordset("Select * FROM SabiFigures1;")
            If Not (rsFormat.EOF And rsFormat.BOF) Then
                rsFormat.MoveFirst
                Do Until rsFormat.EOF = True
                    nIFF = rsFormat![NIF Code]
                    If NIF = nIFF Then
                        Set qdfDef = CurrentDb.QueryDefs("PopulateSabiFigures1")
                        qdfDef.Parameters("NIF: ").Value = nIFF
                        Set rstDef = qdfDef.OpenRecordset()
                        Set PeriodEnding = RSToColl(rstDef, "PeriodEnding")
                        Set Materials1 = RSToColl(rstDef, "Materials")
                        Set Depreciation1 = RSToColl(rstDef, "Depreciation")
                        Set NonTrading1 = RSToColl(rstDef, "Non-Trading Income")
                        Set TotalInterest = RSToColl(rstDef, "Total_Interest_Charges")
                        Set Pretax = RSToColl(rstDef, "Pretax_Profit")
                        Set TotalRemu = RSToColl(rstDef, "Total_Empl_Remu_000")

                        For i = 1 To 6
                        VariableName = "DateFiled" & i
                        Next i

                        For Each Period In PeriodEnding
                            Debug.Print TypeName(Period)
                        Next


                    End If
                           SQL = 'The massive SQL statement above
                           db.Execute SQL
            rsFormat.MoveNext
            Loop
        End If
    rsFigs1.MoveNext
    Loop
End If


rstDef.Close
Set rstDef = Nothing
rsFormat.Close
Set rsFormat = Nothing
rsFigs1.Close
Set rsFigs1 = Nothing

也许我以错误的方式看待问题,将不胜感激任何帮助和指示。提前致谢。希望添加一些图片有帮助 Pretty much the layout and design of Figs Design and layout of sabi

对于首先提出阵列的古斯塔夫,我可以做这样的事情: 这将每年带回一行。我想一次更新一行以节省时间,而不是更新每个年度值。我几乎每行更新6次,这将花费更长的时间。

Set rsFormat = CurrentDb.OpenRecordset("Select * FROM SabiFigures1;")
    If Not (rsFormat.EOF And rsFormat.BOF) Then
        rsFormat.MoveFirst
        Do Until rsFormat.EOF = True
            nIFF = rsFormat![NIF Code]
            Set qdfDef = CurrentDb.QueryDefs("PopulateSabiFigures1")
            qdfDef.Parameters("NIF: ").Value = nIFF
            Set rstDef = qdfDef.OpenRecordset()
            rstDef.MoveLast
            rstDef.MoveFirst
            varRecord = rstDef.GetRows(rstDef.RecordCount)
            For intI = 0 To 5 'UBound(varRecord, 2)
                For intJ = 0 To UBound(varRecord, 1)
                    Debug.Print varRecord(intJ, intI)
                Next intJ
            Next intI
                    'whole row updated here after each value of the array is passed to variables for each year and financial value.
            rstDef.Close
            Set rstDef = Nothing
        rsFormat.MoveNext
        Loop
    End If

1 个答案:

答案 0 :(得分:0)

你好,所以我弄清楚了。该代码非常庞大,因此任何人都可以使其更加简化和灵活,例如我需要更改的列将不胜感激。

Dim db As DAO.Database
Set db = CurrentDb
Dim rsFormat As DAO.Recordset
Dim rsFigs1 As Object
Dim qdfDef As DAO.QueryDef
Dim rstDef As Object
Dim varRecord As Variant
Dim NIF As String
Dim nIFF As String
Dim intI As Integer
Dim intJ As Integer
Dim RegNum, LatestDate, Date1, Date2, Date3, Date4, Date5, LatestMaterial, Material1, Material2, Material3, Material4, Material5, LatestDepreciation, Depreciation1, Depreciation2 As String
Dim Depreciation3, Depreciation4, Depreciation5, LatestTrading, Trading1, Trading2, Trading3, Trading4, Trading5, LatestTotalInterest, TotalInterest1, TotalInterest2, TotalInterest3 As String
Dim TotalInterest4, TotalInterest5, LatestPreTaxProfit, PreTaxProfit1, PreTaxProfit2, PreTaxProfit3, PreTaxProfit4, PreTaxProfit5, LatestTotEmpRem, TotEmpRem1, TotEmpRem2, TotEmpRem3 As String
Dim TotEmpRem4, TotEmpRem5, SQL As String

Set rsFormat = CurrentDb.OpenRecordset("Select [NIF Code] FROM SabiFigures1;")
    If Not (rsFormat.EOF And rsFormat.BOF) Then
        rsFormat.MoveFirst
        Do Until rsFormat.EOF = True
            nIFF = rsFormat![NIF Code]
            Set qdfDef = CurrentDb.QueryDefs("PopulateSabiFigures1")
            qdfDef.Parameters("NIF: ").Value = nIFF
            Set rstDef = qdfDef.OpenRecordset()
            rstDef.MoveLast
            rstDef.MoveFirst
            varRecord = rstDef.GetRows(rstDef.RecordCount)
            For intI = 0 To 5
                For intJ = 0 To UBound(varRecord, 1)
                    Debug.Print varRecord(intJ, intI)
                    On Error Resume Next
                    If intI = 0 Then
                        If intJ = 0 Then
                            RegNum = varRecord(intJ, intI)
                        ElseIf intJ = 1 Then
                            LatestDate = varRecord(intJ, intI)
                        ElseIf intJ = 2 Then
                            LatestMaterial = varRecord(intJ, intI)
                        ElseIf intJ = 3 Then
                            LatestDepreciation = varRecord(intJ, intI)
                        ElseIf intJ = 4 Then
                            LatestTrading = varRecord(intJ, intI)
                        ElseIf intJ = 5 Then
                            LatestTotalInterest = varRecord(intJ, intI)
                        ElseIf intJ = 6 Then
                            LatestPreTaxProfit = varRecord(intJ, intI)
                        ElseIf intJ = 7 Then
                            LatestTotEmpRem = varRecord(intJ, intI)
                        Else
                            MsgBox "Error in Loop"
                            Exit Sub
                        End If
                ElseIf intI = 1 Then
                        If intJ = 0 Then
                            RegNum = varRecord(intJ, intI)
                        ElseIf intJ = 1 Then
                            Date1 = varRecord(intJ, intI)
                        ElseIf intJ = 2 Then
                            Material1 = varRecord(intJ, intI)
                        ElseIf intJ = 3 Then
                            Depreciation1 = varRecord(intJ, intI)
                        ElseIf intJ = 4 Then
                            Trading1 = varRecord(intJ, intI)
                        ElseIf intJ = 5 Then
                            TotalInterest1 = varRecord(intJ, intI)
                        ElseIf intJ = 6 Then
                            PreTaxProfit1 = varRecord(intJ, intI)
                        ElseIf intJ = 7 Then
                            TotEmpRem1 = varRecord(intJ, intI)
                        Else
                            MsgBox "Error in Loop"
                            Exit Sub
                        End If
                ElseIf intI = 2 Then
                        If intJ = 0 Then
                            RegNum = varRecord(intJ, intI)
                        ElseIf intJ = 1 Then
                            Date2 = varRecord(intJ, intI)
                        ElseIf intJ = 2 Then
                            Material2 = varRecord(intJ, intI)
                        ElseIf intJ = 3 Then
                            Depreciation2 = varRecord(intJ, intI)
                        ElseIf intJ = 4 Then
                            Trading2 = varRecord(intJ, intI)
                        ElseIf intJ = 5 Then
                            TotalInterest2 = varRecord(intJ, intI)
                        ElseIf intJ = 6 Then
                            PreTaxProfit2 = varRecord(intJ, intI)
                        ElseIf intJ = 7 Then
                            TotEmpRem2 = varRecord(intJ, intI)
                        Else
                            MsgBox "Error in Loop"
                            Exit Sub
                        End If
                ElseIf intI = 3 Then
                        If intJ = 0 Then
                            RegNum = varRecord(intJ, intI)
                        ElseIf intJ = 1 Then
                            Date3 = varRecord(intJ, intI)
                        ElseIf intJ = 2 Then
                            Material3 = varRecord(intJ, intI)
                        ElseIf intJ = 3 Then
                            Depreciation3 = varRecord(intJ, intI)
                        ElseIf intJ = 4 Then
                            Trading3 = varRecord(intJ, intI)
                        ElseIf intJ = 5 Then
                            TotalInterest3 = varRecord(intJ, intI)
                        ElseIf intJ = 6 Then
                            PreTaxProfit3 = varRecord(intJ, intI)
                        ElseIf intJ = 7 Then
                            TotEmpRem3 = varRecord(intJ, intI)
                        Else
                            MsgBox "Error in Loop"
                            Exit Sub
                        End If
                ElseIf intI = 4 Then
                        If intJ = 0 Then
                            RegNum = varRecord(intJ, intI)
                        ElseIf intJ = 1 Then
                            Date4 = varRecord(intJ, intI)
                        ElseIf intJ = 2 Then
                            Material4 = varRecord(intJ, intI)
                        ElseIf intJ = 3 Then
                            Depreciation4 = varRecord(intJ, intI)
                        ElseIf intJ = 4 Then
                            Trading4 = varRecord(intJ, intI)
                        ElseIf intJ = 5 Then
                            TotalInterest4 = varRecord(intJ, intI)
                        ElseIf intJ = 6 Then
                            PreTaxProfit4 = varRecord(intJ, intI)
                        ElseIf intJ = 7 Then
                            TotEmpRem4 = varRecord(intJ, intI)
                        Else
                            MsgBox "Error in Loop"
                            Exit Sub
                        End If
                ElseIf intI = 5 Then
                        If intJ = 0 Then
                            RegNum = varRecord(intJ, intI)
                        ElseIf intJ = 1 Then
                            Date5 = varRecord(intJ, intI)
                        ElseIf intJ = 2 Then
                            Material5 = varRecord(intJ, intI)
                        ElseIf intJ = 3 Then
                            Depreciation5 = varRecord(intJ, intI)
                        ElseIf intJ = 4 Then
                            Trading5 = varRecord(intJ, intI)
                        ElseIf intJ = 5 Then
                            TotalInterest5 = varRecord(intJ, intI)
                        ElseIf intJ = 6 Then
                            PreTaxProfit5 = varRecord(intJ, intI)
                        ElseIf intJ = 7 Then
                            TotEmpRem5 = varRecord(intJ, intI)
                        Else
                            MsgBox "Error in Loop"
                            Exit Sub
                        End If
                Else
                    MsgBox "Error in Loop"
                    Exit Sub
                End If
                Next intJ

            Next intI
    SQL = "UPDATE SabiFigures1 SET SabiFigures1.[Closing Date Last avail yr] = '" & LatestDate & "', SabiFigures1.[Closing Date Year - 1] = '" & Date1 & "', " & _
          "SabiFigures1.[Closing Date Year - 2] = '" & Date2 & "', SabiFigures1.[Closing Date Year - 3] = '" & Date3 & "', SabiFigures1.[Closing Date Year - 4] = '" & Date4 & "', " & _
          "SabiFigures1.[Closing Date Year - 5] = '" & Date5 & "', SabiFigures1.[Material costs th EUR Last avail yr] = '" & LatestMaterial & "', SabiFigures1.[Material costs th EUR Year - 1] " & _
          "= '" & Material1 & "', SabiFigures1.[Material costs th EUR Year - 2] = '" & Material2 & "', SabiFigures1.[Material costs th EUR Year - 3] = '" & Material3 & "', SabiFigures1.[Material costs th EUR Year - 4] " & _
          "= '" & Material4 & "', SabiFigures1.[Material costs th EUR Year - 5] = '" & Material5 & "', SabiFigures1.[Depreciation th EUR Last avail yr] = '" & LatestDepreciation & "', SabiFigures1.[Depreciation th EUR Year - 1] = " & _
          "'" & Depreciation1 & "', SabiFigures1.[Depreciation th EUR Year - 2] = '" & Depreciation2 & "', SabiFigures1.[Depreciation th EUR Year - 3] = '" & Depreciation3 & "', SabiFigures1.[Depreciation th EUR Year - 4] = '" & Depreciation4 & "', " & _
          "SabiFigures1.[Depreciation th EUR Year - 5] = '" & Depreciation5 & "', SabiFigures1.[Financial revenue th EUR Last avail yr] = '" & LatestTrading & "', SabiFigures1.[Financial revenue th EUR Year - 1] " & _
          "= '" & Trading1 & "', SabiFigures1.[Financial revenue th EUR Year - 2] = '" & Trading2 & "', SabiFigures1.[Financial revenue th EUR Year - 3] = '" & Trading3 & "', SabiFigures1.[Financial revenue th EUR Year " & _
          "- 4] = '" & Trading4 & "', SabiFigures1.[Financial revenue th EUR Year - 5] = '" & Trading5 & "', SabiFigures1.[Financial expenses th EUR Last avail yr] = '" & LatestTotalInterest & "', SabiFigures1.[Financial expenses " & _
          "th EUR Year - 1] = '" & TotalInterest1 & "', SabiFigures1.[Financial expenses th EUR Year - 2] = '" & TotalInterest2 & "', SabiFigures1.[Financial expenses th EUR Year - 3] = '" & TotalInterest3 & "', " & _
          "SabiFigures1.[Financial expenses th EUR Year - 4] = '" & TotalInterest4 & "', SabiFigures1.[Financial expenses th EUR Year - 5] = '" & TotalInterest5 & "', SabiFigures1." & _
          "[P/L before tax th EUR Last avail yr] = '" & LatestPreTaxProfit & "', SabiFigures1.[P/L before tax th EUR Year - 1] = '" & PreTaxProfit1 & "', SabiFigures1.[P/L before tax th EUR Year - 2] " & _
          "= '" & PreTaxProfit2 & "', SabiFigures1.[P/L before tax th EUR Year - 3] = '" & PreTaxProfit3 & "', SabiFigures1.[P/L before tax th EUR Year - 4] = '" & PreTaxProfit4 & "', SabiFigures1.[P/L before tax th EUR Year - 5] = " & _
          "'" & PreTaxProfit5 & "', SabiFigures1.[Cost of employees th EUR Last avail yr] = '" & LatestTotEmpRem & "', SabiFigures1.[Cost of employees th EUR Year - 1] = '" & TotEmpRem1 & "', SabiFigures1.[Cost of employees th EUR Year - 2] " & _
          "= '" & TotEmpRem2 & "', SabiFigures1.[Cost of employees th EUR Year - 3] = '" & TotEmpRem3 & "', SabiFigures1.[Cost of employees th EUR Year - 4] = '" & TotEmpRem4 & "', SabiFigures1.[Cost of employees th EUR Year - 5] = '" & TotEmpRem5 & "' " & _
          "WHERE (((SabiFigures1.[NIF Code])='" & RegNum & "'));"
    db.Execute SQL
    SQL = ""
    RegNum = ""
    LatestDate = ""
    Date1 = ""
    Date2 = ""
    Date3 = ""
    Date4 = ""
    Date5 = ""
    LatestMaterial = ""
    Material1 = ""
    Material2 = ""
    Material3 = ""
    Material4 = ""
    Material5 = ""
    LatestDepreciation = ""
    Depreciation1 = ""
    Depreciation2 = ""
    Depreciation3 = ""
    Depreciation4 = ""
    Depreciation5 = ""
    LatestTrading = ""
    Trading1 = ""
    Trading2 = ""
    Trading3 = ""
    Trading4 = ""
    Trading5 = ""
    LatestTotalInterest = ""
    TotalInterest1 = ""
    TotalInterest2 = ""
    TotalInterest3 = ""
    TotalInterest4 = ""
    TotalInterest5 = ""
    LatestPreTaxProfit = ""
    PreTaxProfit1 = ""
    PreTaxProfit2 = ""
    PreTaxProfit3 = ""
    PreTaxProfit4 = ""
    PreTaxProfit5 = ""
    LatestTotEmpRem = ""
    TotEmpRem1 = ""
    TotEmpRem2 = ""
    TotEmpRem3 = ""
    TotEmpRem4 = ""
    TotEmpRem5 = ""

            'For intI = 6 To UBound(varRecord, 2)
            '    For intJ = 0 To UBound(varRecord, 1)
            '        Debug.Print varRecord(intJ, intI)
            '    Next intJ
            'Next intI

            rstDef.Close
            Set rstDef = Nothing
        rsFormat.MoveNext
        Loop
    End If