1

我正在尝试移动到具有特定 ID 的记录。

我在这篇文章中尝试了解决方案:MS Access search for record by textbox instead of dropdown

但没有成功

这是我的代码

Private Sub btnShowPrevious_Click()
    Dim rs As DAO.Recordset
    Set rs = Me.RecordsetClone
    rs.FindFirst "[ID]=" & ParentID
    If rs.NoMatch Then
        MsgBox "Sorry, no such record '" & ParentID & "' was found.", _
               vbOKOnly + vbInformation
    Else
        Me.Recordset.Bookmark = rs.Bookmark
    End If
    rs.Close
End Sub

它总是没有匹配,但 parentID = 1 并且有一条 ID = 1 的记录..

有人知道出了什么问题吗?

谢谢

记录来源是这张表:

CREATE TABLE [dbo].[ProposalFollowUp](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ProposalID] [int] NOT NULL,
[MillID] [int] NULL,
[ClientID] [int] NULL,
[Comment] [nvarchar](max) NULL,
[Method] [nvarchar](128) NULL,
[Contact] [int] NULL,
[ContactDate] [datetime] NULL,
[Done] [bit] NOT NULL,
[CreatedBy] [nvarchar](50) NULL,
[CreatedDate] [datetime] NULL,
[ModifiedBy] [nvarchar](50) NULL,
[ModifiedDate] [datetime] NULL,
[EAIEmployee] [nvarchar](50) NULL,
[PersonInCharge] [nvarchar](50) NULL,
[ParentID] [int] NULL,

这是表单属性的屏幕截图

在此处输入图像描述

最后,如果我显示导航栏,我可以看到有一个过滤器。可能是因为我这样打开表格

DoCmd.OpenForm "ProposalsFollowUp", , , "[ID] = " & txtID, acFormEdit, acDialog

如果我删除过滤器,它可以工作。

好的,这是最终的代码

Private Sub btnShowPrevious_Click()
    Dim parent As Integer
    parent = ParentID
    Me.Filter = ""
    Dim rs As DAO.Recordset
    Set rs = Me.RecordsetClone
    rs.FindFirst "[ID]=" & parent
    If rs.NoMatch Then
        MsgBox "Sorry, no such record '" & parent & "' was found.", _
               vbOKOnly + vbInformation
    Else
        Me.Recordset.Bookmark = rs.Bookmark
    End If
    rs.Close
End Sub
4

1 回答 1

1

在检查了所有明显的内容(例如确保记录集包含您正在搜索的数据并且没有过滤器)之后,您可以考虑表单存在问题。过滤器显示在 Access 2010 中的表单底部,可以通过以下方式在 VBA 中删除:

Me.FilterOn = False

或者通过单击过滤器按钮:

表单过滤器访问 2010

前端发生的奇怪事情通常是由于某种形式的损坏。开发时需要定期备份、压缩、修复和反编译。如果您有链接表,刷新链接通常是个好主意。

如果您创建了一个不想丢失的表单并且它已损坏,您可以通过剪切和粘贴复制到一个新表单,或者您可以导出为文本并导入。

反编译:

"c:\Program Files\Microsoft office\office\msaccess.exe" /decompile "c:\My Documents\MyDatabase.mdb"

-- http://allenbrowne.com/ser-47.html

另存为文本:

Application.SaveAsText acForm, "FormName", "z:\docs\tmp.txt"
Application.LoadFromText acForm, "restoredForm", "z:\docs\tmp.txt"
于 2012-09-25T15:01:36.140 回答