所以,我从别人那里继承了一些代码(下面),我试图理解它是如何工作的。我理解代码的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
答案 0 :(得分:1)
代码将一行的政策编号与政策进行比较 下一个号码。如果它们相同,则将保费分组。 如果它们不同,请不要。
几乎。
这里有两个循环。一个遍历输入文件的每一行。在输入文件的每一行上,第二个循环遍历(可能是全部)PolicyNumArray,查找与从输入文件中获取的数字匹配的策略编号。
如果我是你,我会使用调试器逐步完成此操作。确保它正在按照您的预期进行操作。我想密切关注这一部分(有些线被剪掉)。
UniqueCount = 0
For i = 1 To NumRecords
k = 0
Do Until k = UniqueCount