【问题标题】:MS Access form to edit specific record from a form by providing input through text boxMS Access 表单通过文本框提供输入来编辑表单中的特定记录
【发布时间】:2012-01-15 19:42:28
【问题描述】:

有人可以帮我找到特定记录通过 MS 访问表单编辑

我有 Frmfind 表单,其中我有一个提交“ticket#”是一个输入文本框 另一个文件是按钮“查找”

当我输入ticket# 时,它是我的表的主键。我需要使用 VBA 代码在 FormEdit 模式下打开特定的票证记录...

所以我有另一种形式的特定记录“frmEdit”,必须从 frmfind -> 特定输入调用..

注意:Ticket# 是我的表中的列,拥有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

【问题讨论】:

  • 如果您不熟悉 VBA,使用向导在 frmEdit 上放置一个组合框可能更容易,它将允许您选择“在我的表单上查找记录”,因此用户只有选择要显示的记录的票号。

标签: ms-access vba ms-access-2007


【解决方案1】:

您可以使用此代码:

Private Sub Command112_Click()

   Me.txtSeach.SetFocus
   On Error Resume Next
   DoCmd.OpenForm "frmEdit", , , "TicketNumber = '" & Nz(Me.text79, "") & "'"

End Sub

如果 TicketNumber 实际上是一个数字列而不是文本,那么请删除单引号并使用它:

   DoCmd.OpenForm "aa", , , "TicketNumber = " & Nz(Me.text79, "")

然后对于您的消息,只需将该代码放在具有取消的打开事件的表单中:

例如:

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()。我认为上面的代码更少,但是:

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

所以任何一种方式都应该足够了。

【讨论】:

  • 很棒的解释。上述解决方案运行良好:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
相关资源
最近更新 更多