Access 2010 VBA 不保存对字段的更改

Posted

技术标签:

【中文标题】Access 2010 VBA 不保存对字段的更改【英文标题】:Access 2010 VBA not saving changes to fields 【发布时间】:2013-12-04 20:32:56 【问题描述】:

我在 Access 2010 中使用 VBA 使用一个表中的数据计算一些公式,并更新另一个表中的字段。我遇到的问题是第二个表中的字段没有按应有的方式更新。我已经插入了一个断点并单步执行了 VBA 代码的每一行,并且计算都正确执行,并且我没有收到任何错误。任何人都可以提供一些关于寻找什么的建议吗?

在提供的代码中,最后一个 If Not rsPenetration.EOF 块是未更新的部分。

Private Sub Form_Load()
DoCmd.SetFilter WhereCondition:="[Hour] > 7"

Dim rsRecords As New ADODB.Recordset
Dim rsPenetration As New ADODB.Recordset
Dim rsProjected As New ADODB.Recordset
Dim rsHourlyDials As New ADODB.Recordset
Dim currentDate As Date

Dim goal8 As Double
Dim goal9 As Double
Dim goal10 As Double
Dim goal11 As Double
Dim goal12 As Double
Dim goal13 As Double
Dim goal14 As Double
Dim goal15 As Double
Dim goal16 As Double
Dim goal17 As Double
Dim goal18 As Double
Dim goal19 As Double
Dim goal20 As Double
Dim goal21 As Double
Dim goal22 As Double

Dim hourly8 As Double
Dim hourly9 As Double
Dim hourly10 As Double
Dim hourly11 As Double
Dim hourly12 As Double
Dim hourly13 As Double
Dim hourly14 As Double
Dim hourly15 As Double
Dim hourly16 As Double
Dim hourly17 As Double
Dim hourly18 As Double
Dim hourly19 As Double
Dim hourly20 As Double
Dim hourly21 As Double
Dim hourly22 As Double

Dim recordPct8 As Integer
Dim recordPct9 As Integer
Dim recordPct10 As Integer
Dim recordPct11 As Integer
Dim recordPct12 As Integer
Dim recordPct13 As Integer
Dim recordPct14 As Integer
Dim recordPct15 As Integer
Dim recordPct16 As Integer
Dim recordPct17 As Integer
Dim recordPct18 As Integer
Dim recordPct19 As Integer
Dim recordPct20 As Integer
Dim recordPct21 As Integer
Dim recordPct22 As Integer

Dim cumulativeGoal As Integer
Dim totalRecords As Integer
Dim projectedPenetration As Double

currentDate = Date

'Get daily records total from NewESDialsPerList table
rsRecords.Open "SELECT * FROM NewESDialsPerList WHERE CallDate = #" & currentDate & "#", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If Not rsRecords.EOF Then
    totalRecords = rsRecords.Fields("CombinedRecords")
End If
rsRecords.Close

'Get hourly dials from ESDialsPerHour table and calculate hourly percent
rsHourlyDials.Open "SELECT * FROM ESDialsPerHour WHERE ESDate = #" & currentDate & "#", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If Not rsHourlyDials.EOF Then
    hourly8 = (rsHourlyDials.Fields("Eight") / totalRecords)
    hourly9 = (rsHourlyDials.Fields("Nine") / totalRecords)
    hourly10 = (rsHourlyDials.Fields("Ten") / totalRecords)
    hourly11 = (rsHourlyDials.Fields("Eleven") / totalRecords)
    hourly12 = (rsHourlyDials.Fields("Twelve") / totalRecords)
    hourly13 = (rsHourlyDials.Fields("Thirteen") / totalRecords)
    hourly14 = (rsHourlyDials.Fields("Fourteen") / totalRecords)
    hourly15 = (rsHourlyDials.Fields("Fifteen") / totalRecords)
    hourly16 = (rsHourlyDials.Fields("Sixteen") / totalRecords)
    hourly17 = (rsHourlyDials.Fields("Seventeen") / totalRecords)
    hourly18 = (rsHourlyDials.Fields("Eighteen") / totalRecords)
    hourly19 = (rsHourlyDials.Fields("Nineteen") / totalRecords)
    hourly20 = (rsHourlyDials.Fields("Twenty") / totalRecords)
    hourly21 = (rsHourlyDials.Fields("TwentyOne") / totalRecords)
    hourly22 = (rsHourlyDials.Fields("TwentyTwo") / totalRecords)
End If

'Get projected penetration from ESProjectedPenetration table
rsProjected.Open "SELECT * FROM ESProjectedPenetration WHERE ProjectedDate = #" & currentDate & "#", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If Not rsProjected.EOF Then
    projectedPenetration = rsProjected.Fields("ProjectedPenetration")
End If

goal8 = (totalRecords * projectedPenetration) * (hourly8 / totalRecords)
goal9 = (totalRecords * projectedPenetration) * (hourly9 / totalRecords)
goal10 = (totalRecords * projectedPenetration) * (hourly10 / totalRecords)
goal11 = (totalRecords * projectedPenetration) * (hourly11 / totalRecords)
goal12 = (totalRecords * projectedPenetration) * (hourly12 / totalRecords)
goal13 = (totalRecords * projectedPenetration) * (hourly13 / totalRecords)
goal14 = (totalRecords * projectedPenetration) * (hourly14 / totalRecords)
goal15 = (totalRecords * projectedPenetration) * (hourly15 / totalRecords)
goal16 = (totalRecords * projectedPenetration) * (hourly16 / totalRecords)
goal17 = (totalRecords * projectedPenetration) * (hourly17 / totalRecords)
goal18 = (totalRecords * projectedPenetration) * (hourly18 / totalRecords)
goal19 = (totalRecords * projectedPenetration) * (hourly19 / totalRecords)
goal20 = (totalRecords * projectedPenetration) * (hourly20 / totalRecords)
goal21 = (totalRecords * projectedPenetration) * (hourly21 / totalRecords)
goal22 = (totalRecords * projectedPenetration) * (hourly22 / totalRecords)

rsPenetration.Open "SELECT * FROM NewPIDGoals WHERE PenetrationDate = #" & currentDate & "#", CurrentProject.Connection, adOpenKeyset, adLockOptimistic

If Not rsPenetration.EOF Then
    rsPenetration.Fields("Eight") = goal8
    rsPenetration.Fields("Nine") = goal9
    rsPenetration.Fields("Ten") = goal10
    rsPenetration.Fields("Eleven") = goal11
    rsPenetration.Fields("Twelve") = goal12
    rsPenetration.Fields("Thirteen") = goal13
    rsPenetration.Fields("Fourteen") = goal14
    rsPenetration.Fields("Fifteen") = goal15
    rsPenetration.Fields("Sixteen") = goal16
    rsPenetration.Fields("Seventeen") = goal17
    rsPenetration.Fields("Eighteen") = goal18
    rsPenetration.Fields("Nineteen") = goal19
    rsPenetration.Fields("Twenty") = goal20
    rsPenetration.Fields("TwentyOne") = goal21
    rsPenetration.Fields("TwentyTwo") = goal22
    'rsPenetration.Save
End If

End Sub

【问题讨论】:

我遇到的问题是第二个表中的字段没有按照应有的方式更新它们应该如何以及如何不更新?顺便说一句,(totalRecords * projectedPenetration) * (hourly22 / totalRecords) 可以分解为(projectedPenetration * hourly22 ) 这也有帮助,我知道该公式存在问题,您的回复澄清了问题所在。谢谢! 【参考方案1】:

分配新的rsPenetration.Fields() 值后,您需要使用rsPenetration.Update 方法提交更改(将它们写回表中)。

【讨论】:

以上是关于Access 2010 VBA 不保存对字段的更改的主要内容,如果未能解决你的问题,请参考以下文章

可以通过 VBA 更改 MS Access 子表单字段吗?

如何使用 VBA 从 Access 中的表单更改保存的导出路径?

在 Access VBA 中禁止写入冲突消息

Access VBA - 触发组合框更改事件

使用 Access VBA 使用循环填充 Word 表单

Access 2010 VBA:为啥这个表单打开和关闭序列不起作用?