查找具有ID的记录 [英] Find record with ID
问题描述
我正在尝试移至具有特定ID的记录.
我尝试过此帖子中的解决方案: MS通过文本框(而不是下拉列表)访问搜索记录的记录
但没有成功
这是我的代码
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
检查了所有显而易见的事情(例如确保记录集包含要搜索的数据并且没有过滤器之后),您可以考虑表单存在问题.筛选器显示在Access 2010表单的底部,可以通过以下方式在VBA中删除:
Me.FilterOn = False
或通过单击过滤器按钮:
前端发生的奇怪事情通常是由于某种形式的损坏造成的.开发时,需要定期备份,压缩,修复和反编译.如果您有链接表,则通常最好刷新链接.
如果您不希望丢失表单并且它会损坏,则可以通过剪切和粘贴将其复制到新表单中,也可以导出为文本并导入.
反编译:
"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"
I'm trying to move to a record that has a certain ID.
I tried the solution on this post: MS Access search for record by textbox instead of dropdown
but no success
Here's my code
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
It always hit no match but parentID = 1 and there's one record with ID = 1..
Anyone has an idea of what is wrong?
Thank you
The record source is this table:
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,
here's a screenshot of the form properties
Finaly, if I show the navigation bar, I can see there's a filter. Probably because I open the form like this
DoCmd.OpenForm "ProposalsFollowUp", , , "[ID] = " & txtID, acFormEdit, acDialog
If I remove the filter, it works.
Alright Here's the final code
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
After checking everything obvious such as ensuring the recordset includes the data you are searching for and that there are no filters, you can consider a problem with the form. A filter is shown at the bottom of the form in Access 2010 and can be removed in VBA by:
Me.FilterOn = False
Or by clicking the filter button:
Odd things happening in a front-end are often due to corruption of some sort. You need to regularly back-up, compact and repair and decompile when you are developing. If you have linked tables, it is often a good idea to refresh the links.
If you create a form you do not want to lose and it becomes corrupt, you can copy to a new form with cut and paste or you can export to text and import.
Decompile:
"c:\Program Files\Microsoft office\office\msaccess.exe" /decompile "c:\My Documents\MyDatabase.mdb"
-- http://allenbrowne.com/ser-47.html
Save As Text:
Application.SaveAsText acForm, "FormName", "z:\docs\tmp.txt"
Application.LoadFromText acForm, "restoredForm", "z:\docs\tmp.txt"
这篇关于查找具有ID的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!