0

所以,我从别人那里继承了一些代码(如下),我试图了解它是如何工作的。我了解代码的 msot(尽管我对 Access VBA 很陌生),但我不明白的部分是该代码如何将一个策略的所有信息组合在一起。

情况如下。要从我们公司的数据库中获取特定保单的保费数据,我们必须为每条线路获取一份保险。但是,我希望所有保费,每个保险一个,都在同一条线上。因此,这段代码将多行的所有内容放在了一行中。为简单起见,我将其减少到 3 个总覆盖范围,尽管还有更多。当我阅读代码时,似乎假设一个策略的所有信息都在一起,例如特定策略的 1、2 或 3 行是有序的。但是,例如,即使我按保费(金额)列对表格进行排序,它仍然会在一行中获得一份保单的所有保费。我在代码中看不到任何应该使它工作的地方。该代码将第一行的保单编号与下一行的保单编号进行比较。如果它们相同,则将保费组合在一起。如果它们不同,请不要。同样,我可以对表进行排序,以便一个策略的记录不在一起,但最终结果仍然正确。我错过了什么吗?是 Access 中的某事吗?谢谢你的帮助!

Option Compare Database
Option Explicit

' Premium is imported with one row for each coverage per policy, so possibly several rows per policy.
' This procedure takes several rows per policy and makes them into one row.
Sub ScrubPremium()

Dim i As Long, j As Long, k As Long
Dim NumRecords As Long, found As Long, UniqueCount As Long
Dim tempPolicyNum As String, tempCoverage As String, tempPremium As Single
Dim PolicyNumArray() As String, PremiumArray() As Single, TotalPremiumArray() As Single

Dim db As DAO.Database
Set db = CurrentDb

Dim infile As Variant, outfile As Variant

Set infile = db.OpenRecordset("Imported Premium")
CurrentDb.Execute "DELETE * FROM [Finalized Premium]"
Set outfile = db.OpenRecordset("Finalized Premium")

NumRecords = infile.RecordCount
ReDim PolicyNumArray(NumRecords)
ReDim PremiumArray(NumRecords, 3)
ReDim TotalPremiumArray(NumRecords)

infile.MoveFirst

'initialize PremiumArray
For i = 1 To NumRecords
    For j = 1 To NumPremiums
        PremiumArray(i, j) = 0
    Next j
Next i

'populate arrays
UniqueCount = 0
For i = 1 To NumRecords
    tempPolicyNum = infile![Policy_Number]
    tempCoverage = infile![Coverage]
    tempPremium = infile![Premium]

    k = 0
    found = 0

    Do Until k = UniqueCount Or found = 1 'check for unique policy
        If tempPolicyNum = PolicyNumArray(k + 1) Then
            found = 1
        Else
            k = k + 1
        End If
    Loop

    If found = 0 Then
        UniqueCount = UniqueCount + 1
        PolicyNumArray(k + 1) = tempPolicyNum
    End If

    Select Case tempCoverage
    Case "Comprehensive"
        j = 1
    Case "Collision"
        j = 2
    Case Else
        j = 3
    End Select
    PremiumArray(k + 1, j) = PremiumArray(k + 1, j) + tempPremium
    TotalPremiumArray(k + 1) = TotalPremiumArray(k + 1) + tempPremium
    infile.MoveNext
Next i

'Populate table
For i = 1 To UniqueCount
    outfile.AddNew
        outfile![Full Policy Number] = PolicyNumArray(i)

        outfile![Comp Premium] = PremiumArray(i, 1)
        outfile![Coll Premium] = PremiumArray(i, 2)
        outfile![Other Premium] = PremiumArray(i, 3)
        outfile![Total Premium] = TotalPremiumArray(i)
    outfile.Update
Next i

infile.Close
outfile.Close

End Sub
4

1 回答 1

1

该代码将第一行的保单编号与下一行的保单编号进行比较。如果它们相同,则将保费组合在一起。如果它们不同,请不要。

几乎。

这里有两个循环。遍历输入文件的每一行。在输入文件的每一行,第二个循环遍历(可能是全部)PolicyNumArray,寻找与从输入文件中获取的数字相匹配的策略编号。

如果我是你,我会用调试器逐步完成。确保它正在做你期望它做的事情。我想仔细看看这部分(剪掉了一些线条)。

UniqueCount = 0
For i = 1 To NumRecords
    k = 0
    Do Until k = UniqueCount
于 2012-08-06T01:52:45.243 回答