MS Access 表单通过文本框提供输入来编辑表单中的特定记录 [英] MS Access form to edit specific record from a form by providing input through text box
问题描述
有人可以帮助我找到特定记录通过 MS 访问表单编辑
Can someone please help me on this find specific record Edit through MS access Forms
我有 Frmfind 表单,其中我提交的ticket#"是一个输入文本框另一个字段是按钮查找"
I have Frmfind form where I have one filed "ticket#" is a input text box another filed was button "find"
当我输入 ticket# 时,它是我的表的主键.我需要使用 VBA 代码在 FormEdit 模式下打开特定的票号记录...
When I enter ticket# which is primary key for my table. I need get the the specific ticket# record should be opened in FormEdit mode using VBA code...
所以我有另一种特定记录的形式frmEdit",必须从 frmfind -> 特定输入中调用..
So I have another form "frmEdit" of specific record which has to be called from frmfind -> specific input..
注意:Ticket# 是我表中的列,其中最重要的是拥有 Ticket#.
note: Ticket# is column in my table whcih it is primary to have the ticket#.
代码:
Option Compare Database
Private Sub find_Click()
If IsNull(Me.Text79) Or Me.Text79 = "" Then
MsgBox "You must enter a Ticket #", vbOKOnly, "Required Data"
Me.Text79.SetFocus
Exit Sub
End If
If [Ticket#] = Me.Text79.Value Then
MsgBox "Record found"
DoCmd.Close
DoCmd.OpenForm "frmEdit"
Else
MsgBox "not matching record"
Me.Text79.SetFocus
End If
End Sub
Private Sub Form_Open(cancel As Integer)
'On open set focus to text box
Me.Text79.SetFocus
End Sub
推荐答案
您可以使用以下代码:
Private Sub Command112_Click()
Me.txtSeach.SetFocus
On Error Resume Next
DoCmd.OpenForm "frmEdit", , , "TicketNumber = '" & Nz(Me.text79, "") & "'"
End Sub
注意上面如果 TicketNumber 实际上是一个数字列而不是文本,那么删除单引号并使用这个:
Note in above if TicketNumber is in fact a number column and not text, then remove the single quotes and use this:
DoCmd.OpenForm "aa", , , "TicketNumber = " & Nz(Me.text79, "")
然后对于您的消息,只需将该代码放在具有取消的表单打开事件中:
Then for your message, just place that code in the forms on-open event that has a cancel:
例如:
Private Sub Form_Open(Cancel As Integer)
If IsNull(Me!TicketNumberID) Then
MsgBox "Please enter a valid Ticket #", vbOKOnly, "Required Data"
Cancel = True
End If
End Sub
以上假设您的搜索栏是票号.您还可以使用 dlookup(),甚至 dcount().我认为上面的代码较少,但是:
The above assumes your search column is ticket number. You can also use dlookup(), or even dcount(). I think above is less code, but:
Dim strWhere As String
strWhere = "TicketNumber = " & Me.text79
If DCount("*", "tblBookings", strWhere) > 0 Then
code for Record found goes here
Else
code reocrd not found code here
End If
所以无论哪种方式都应该足够了.
So either way should suffice here.
这篇关于MS Access 表单通过文本框提供输入来编辑表单中的特定记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!