【问题标题】:Run time error '3164': 'Field cannot be updated' - Access VBA issue运行时错误“3164”:“无法更新字段”-访问 VBA 问题
【发布时间】:2016-10-28 13:15:24
【问题描述】:

我为 Access 编写的 VBA 有问题。我有一个包含连接字段“Concat”(字符串字段)和名为“Age”的字段(具有数值的整数字段)的表。

还有另外 61 个字段(分别命名为“0”、“1”、“2”...“60”),但代码需要在其中工作:我希望代码循环遍历,并且,每个记录条目 -使用 Concat + age 字段将 VBA 用于 Dlookup 到另一个表(称为:tbl_Final_Probabilities)并拉回概率并用正确的概率填充这 61 个字段中的每一个。这些字段设置在数字字段中,数据类型为 Single。

代码提取了正确的概率,但是当我尝试在代码行更新该字段的记录时:“rs.Fields(a) = b”(也在代码中突出显示),我收到错误消息:“运行时间错误“3164”:“无法更新字段”。

欢迎大家帮助我解决这个问题,使用的代码如下。

拳打脚踢。

代码:

Dim rs As DAO.Recordset
Dim a As Integer
Dim b As Single
Dim lookup As String

Set rs = CurrentDb.OpenRecordset("SELECT * FROM tbl_Circuit_plus_prob")

For a = 0 To 60

    If Not (rs.EOF And rs.BOF) Then
        rs.MoveFirst

            Do Until rs.EOF = True

            rs.Edit

            lookup = rs!Concat & (rs!age + a)

            b = DLookup("Prob_Date", "tbl_Final_Probabilities", "Concat2 = '" & lookup & "'")

            rs.Fields(a) = b  '- CODE BREAKS DOWN HERE

            rs.Update

            rs.MoveNext

            Loop

    End If

Next a

rs.Close
Set rs = Nothing

提前感谢所有帮助。

【问题讨论】:

    标签: ms-access vba ms-access-2010


    【解决方案1】:

    你的循环被翻过来了:

    Dim rs As DAO.Recordset
    Dim a As Integer
    Dim b As Single
    Dim lookup As String
    
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM tbl_Circuit_plus_prob")
    
    If Not (rs.EOF And rs.BOF) Then
        rs.MoveFirst
        Do Until rs.EOF = True
            rs.Edit
            For a = 0 To 60
                lookup = rs!Concat & (rs!age + a)
                b = DLookup("Prob_Date", "tbl_Final_Probabilities", "Concat2 = '" & lookup & "'")
                rs.Fields(a).Value = b
            Next
            rs.Update
            rs.MoveNext
        Loop
    End If
    rs.Close
    
    Set rs = Nothing
    

    【讨论】:

      【解决方案2】:

      您的代码:rs.Fields(a) = b 在您的表中使用索引“a”(在第一个循环中为 0)寻址字段,这可能是一个自动递增字段,因此无法更新。如果您想在名称为 '0','1',... 的字段中写入,请使用以下语法:rs.Fields(x + a) = b,其中 x 是字段数+1(因为您的循环在您的字段“0”之前存在的表中以 0) 开头。

      【讨论】:

      • 干杯 - 对此进行了更正,它奏效了!没有意识到这是字段后面的索引值。出于兴趣,如果我想引用字段的名称而不是索引值,请问我应该使用什么语法/公式?
      • 您可以使用rst!Fields("NameOfField")rst!NameOfField 或(如果有空格或奇怪的名称)rst![Name Of The Field]
      猜你喜欢
      • 1970-01-01
      • 2016-08-03
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多