VBA访问运行时错误440自动化错误

时间:2013-12-26 17:06:33

标签: vba ms-access automation ms-access-2007 access-vba

我不确定是什么导致了这个错误,并希望能帮助我理解我所犯的错误,以及如何纠正问题的帮助或建议 下面是我收到错误的代码部分。 调试标记为第7行“2月=(Me.BillRate * DayNum)* Me.Util _”

    Set dayRs = db.OpenRecordset("SELECT WrkDays FROM WrkDays ORDER BY WrkMonth;")
    dayRs.MoveFirst
    Set DayNum = dayRs.Fields("WrkDays")
    While Not dayRs.EOF
        Jan = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Feb = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Mar = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Apr = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        May = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Jun = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Jul = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Aug = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Sep = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Oct = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Nov = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Dec = (Me.BillRate * DayNum) * Me.Util_
    Wend

我猜测基于我是如何构建这个代码的,我很可能在“Feb”行之后的代码行上得到类似的错误。所以我想更清楚地理解这个错误,以便我可以纠正未来的发生。

UPDATE 在与Hans合作之后,他指出我使用了recordset.getrows方法,该方法完成了我尝试做的同一个过程,减少了头痛。非常感谢汉斯

3 个答案:

答案 0 :(得分:1)

如果您只是每月寻找几天(或工作日),您可以使用VBA日期功能做得更好。例如,信息here

您的要求可能更复杂 - 也许用户指定给定月份的天数。在这种情况下,您需要修复循环结构。 While...Wend正在将光标推进记录集,MoveNext也是如此。

While Not dayRs.EOF
    ...
    dayRs.MoveNext
    ...
    dayRs.MoveNext
    ...
    dayRs.MoveNext
    ...
Wend

尝试评论WhileWend行。你的代码是否运行相同?

答案 1 :(得分:1)

不幸的是,该特定错误消息在细节上相当薄弱。

我的第一个建议是禁用此行......

'Set DayNum = dayRs.Fields("WrkDays")

然后,如果您在其余代码中使用DayNum,请直接引用字段值。

'Jan = (Me.BillRate * DayNum) * Me.Util_  ' use the following instead
Jan = (Me.BillRate * dayRs!WrkDays.Value) * Me.Util_ ' .Value should not be needed here; use it anyway

但是,我不相信这个建议是解决方法。如果不是, 在Feb =行设置一个断点并调查记录集当前行的状态以及所有实体的值......

While Not dayRs.EOF
    Jan = (Me.BillRate * dayRs!WrkDays.Value) * Me.Util_
    dayRs.MoveNext
    Feb = (Me.BillRate * dayRs!WrkDays.Value) * Me.Util_ ' <-- set break point on this line

在立即窗口中......

' are we perhaps at EOF already?
? dayRs.EOF
' confirm you still get the same error with this ...
? (Me.BillRate * dayRs!WrkDays.Value) * Me.Util_
' examine the components
? (Me.BillRate * dayRs!WrkDays.Value)
? Me.BillRate
? dayRs!WrkDays.Value
? Me.Util_

希望这项努力能够揭示可以导致修复的事情。

答案 2 :(得分:1)

这将适用于12条记录,或者更重要的是使用更少或更多。 过了一段时间,你可能会发现你有更多的记录。

   Set dayRs = db.OpenRecordset("SELECT  WrkMonth, SUM( WrkDays )FROM WrkDays GROUP BY  WrkMonth  ORDER BY WrkMonth;")
   dayRs.MoveFirst
   Do While not dayRs.EOF
    DayNum = dayRs.Fields("WrkDays")
    SELECT CASE WrkMonth;
     CASE 1
      Jan = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 2
      Feb = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 3
      Mar = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 4
      Apr = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 5
      May = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 6
      Jun = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 7
      Jul = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 8
     Aug = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 9
      Sep = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 10
      Oct = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 11
      Nov = (Me.BillRate * DayNum) * Me.Util_daysRs
     CASE 12
      Dec = (Me.BillRate * DayNum) * Me.Util_daysRs
    SELECT END
   daysRs.movenext
   Loop