创建绑定搜索文本框

时间:2018-03-15 16:31:45

标签: vba ms-access access-vba

我有一个名为RMA Information的表单绑定到具有多个控件和子表单的表。表单绑定到表[RMAMaster]。相关字段名称为RMANbr。该字段绑定到[RMAMaster]表的[rma_nbr]字段。 [rma_nbr]字段为索引,不能包含重复项。该表具有自己的[ID]字段,该字段是自动递增的,不能包含重复项。

我希望用户能够在表单上选择RMANbr字段,键入新值,按Enter键,绑定表单将转到他们键入的记录,或者显示MsgBox如果它找不到。

我在RMANbr文本框的“更新后”事件中有以下内容,但它无效:

Private Sub RMANbr_AfterUpdate()
  If (Me.RMANbr & vbNullString) = vbNullString Then Exit Sub
    Dim rs As DAO.Recordset
    Set rs = Me.RecordsetClone
    rs.FindFirst "[rma_nbr]=" & Me.RMANbr
    If rs.NoMatch Then
        Response = MsgBox("Sorry, no such record was found.", vbOKOnly)
    Else
        Me.Recordset.Bookmark = rs.Bookmark
  End If
  rs.Close
End Sub

然而,它失败并显示错误消息:

  

运行时错误'3426':此操作已被关联取消   对象

对此错误消息进行调试会将我指向行Me.Recordset.Bookmark = rs.Bookmark

我是否单击上面的结束或调试仍然会产生以下错误消息:

The changes you requested to the table were not successful because they would create duplicate values in the index, primary key or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again

所以看来Access正在尝试使用更改后的RMANbr保存记录,但我不希望它这样做。我希望RMANbr字段保持对当前查看的RMA记录的引用,但也可以使用它,以便用户可以在他们进入RMANbr时立即“跳转”到特定记录。

0 个答案:

没有答案