vba Access VBA如何选择最后添加的记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12864551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:06:31  来源:igfitidea点击:

Access VBA how to select last added record?

vbams-accessaccess-vba

提问by MAW74656

I have Access form with a subform. The subform has a datasheet. The main form has drop down box and button which adds records to the subform. This all works fine (VBA code). Now I want to highlight the most recently inserted record as if I clicked on the row header.

我有一个带有子表单的 Access 表单。子窗体有一个数据表。主窗体有下拉框和按钮,可将记录添加到子窗体。这一切正常(VBA 代码)。现在我想突出显示最近插入的记录,就像单击行标题一样。

How should I go about doing this? The main form's combobox should still be set to the proper record at tpoint, so how do I highlight the subform record?

我该怎么做呢?主窗体的组合框仍应设置为 tpoint 处的正确记录,那么如何突出显示子窗体记录?

回答by HansUp

Make your target record the subform's current record. Then use RunCommandto "select" it ... which will also highlight the record.

使您的目标记录子窗体的当前记录。然后使用RunCommand“选择”它......这也将突出显示该记录。

DoCmd.RunCommand acCmdSelectRecord

Seems like your question is morphing into "how do I make the last added record the current record".

似乎您的问题正在演变为“如何将最后添加的记录设为当前记录”。

If the last added record is still the current record, then you're there already, so no problem.

如果最后添加的记录仍然是当前记录,那么您已经在那里了,所以没问题。

If the user navigated to a different record after adding the last one, there are at least 2 ways to get back to it.

如果用户在添加最后一条记录后导航到另一条记录,则至少有 2 种方法可以返回到该记录。

  1. DoCmd.GoToRecord
  2. Use a find method on the recordset clone.
  1. DoCmd.GoToRecord
  2. 在记录集克隆上使用 find 方法。

Choose the approach which best fits your situation. If the bound value of your combo box is the numeric primary key for the row last added, you can try that second suggestion like this by replacing pkey_field, YourComboNameHere, and SubformControlwith the actual names you're using.

选择最适合您情况的方法。如果您的组合框的限值是最后加入该行的数字主键,您可以通过更换尝试像这样的第二个建议pkey_fieldYourComboNameHereSubformControl与你正在使用的实际名称。

Private Sub FindLastRecordAdded()
    Dim rst As DAO.Recordset
    Dim strCriteria As String

    strCriteria = "[pkey_field] =" & Me.YourComboNameHere

    Set rst = Me.SubformControl.Form.RecordsetClone
    rst.FindFirst strCriteria
    If rst.NoMatch Then
        MsgBox "Oops. This shouldn't happen.", vbInformation
    Else
        Me.SubformControl.Form.Bookmark = rst.Bookmark
    End If

    Set rst = Nothing
End Sub