访问 sub 来更新单个记录,始终更新表中的第一条记录

问题描述 投票:0回答:2

我有一个未绑定的表单,显示我的 d/b 中的表中的字段。用户可以通过单击“浏览...”按钮来选择图像并将它们存储在工作文件夹中。该按钮还应使用单词“Picked”更新属于所选图像的记录中的字段。图像已成功存储,没有问题。执行更新的 Sub 只更新第一条记录。

为每个单独的图像调用 sub,并将图像名称作为参数传递。我使用了一个选择查询,该查询返回该表中的所有记录,然后我尝试对数据应用过滤器。它似乎过滤到单个记录。第一遍更新正确的记录。尽管传递了新的参数值,所有后续传递仍继续更新第一条记录。这可能是一个非常简单的疏忽,但我就是看不到它。

这是未绑定的表格:

这是更新单个记录的VBA:

Sub UpdateRecordInTable(strMyKey As String)
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Debug.Print "Entering sub"
    ' Set the database reference
    Set db = CurrentDB
   
    Set rs = db.OpenRecordset("SELECT * FROM tempAllSrchImg_Result", dbOpenDynaset)
    
    rs.Filter = "ImgFileA = " & Chr(34) & strMyKey & Chr(34)
    
    ' Check if a record matches the filter
    If Not rs.EOF Then
        ' Move to the first matching record
        rs.MoveFirst
        Debug.Print "ImgFileA to be updated as per filter:", strMyKey
        Debug.Print "recordset's ImgFileA value:", rs("ImgFileA").Value
        ' Update the Picked field
        rs.Edit
        rs("Picked").Value = "Picked"  ' Set the new value
        rs.Update
        Debug.Print "Record updated successfully."
    Else
        Debug.Print "No matching record found."
    End If

    ' Clean up
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    Debug.Print "Exiting sub"
End Sub

我期待(并得到)这个子的三个执行。它似乎正在传递正确的参数以在过滤器中使用。但它似乎只更新了一项记录。以下是立即窗口的结果:

Entering sub
ImgFileA to be updated as per filter:     Img000001.A.tiff
recordset's ImgFileA value: Img000001.A.tiff
Record updated successfully.
Exiting sub
Entering sub
ImgFileA to be updated as per filter:     Img000002.A.tiff
recordset's ImgFileA value: Img000001.A.tiff
Record updated successfully.
Exiting sub
Entering sub
ImgFileA to be updated as per filter:     Img000007.A.tiff
recordset's ImgFileA value: Img000001.A.tiff
Record updated successfully.
Exiting sub

我还尝试用直接 SELECT 查询然后编辑或直接 UPDATE 查询表替换

rs.Filter
,但两者都返回错误代码 91“对象变量或 With 块未设置”,即使创建使用查询字符串在数据库中进行 SQL 查询选择或更新正确的记录。

感谢您的帮助。

vba forms ms-access filter unbound
2个回答
0
投票

按如下方式更改您的记录集:

Set rs = db.OpenRecordset("SELECT TOP 1 * FROM tempAllSrchImg_Result WHERE ImgFileA = '" & strMyKey & "';", dbOpenDynaset)

删除以下行:

rs.Filter = "ImgFileA = " & Chr(34) & strMyKey & Chr(34)

0
投票

您误解了 Recordset.Filter 的工作原理 - 它适用于随后打开的 Recordset
仔细查看 docs 中的示例:

    'Now filter the Recordset to return only the customers from that city
    rst.Filter = "City = '" & strCity & "'"
    Set rstFiltered = rst.OpenRecordset

因此,如果您想要或需要走记录集路线,请在打开 rs 时直接对其进行过滤:

strSql = "SELECT * FROM tempAllSrchImg_Result WHERE ImgFileA = " & Chr(34) & strMyKey & Chr(34)
Set rs = db.OpenRecordset(strSql, dbOpenDynaset)

' Check if a record matches the filter
If Not rs.EOF Then
    ' Move to the first matching record <- this is not needed!

    ' Update the Picked field
    rs.Edit
    rs("Picked").Value = "Picked"  ' Set the new value
    rs.Update
    Debug.Print "Record updated successfully."
Else
    Debug.Print "No matching record found."
End If

但通常您只需执行 UPDATE 查询即可。

strSql = "UPDATE tempAllSrchImg_Result SET Picked = 'Picked' " & _
         "WHERE ImgFileA = " & Chr(34) & strMyKey & Chr(34)
db.Execute strSql
© www.soinside.com 2019 - 2024. All rights reserved.