Access:在第二种表单中创建新记录后,如何在一种表单中显示新记录

发布于 2024-11-06 11:41:04 字数 881 浏览 0 评论 0原文

Access 2007:我有一个表单,其中显示了数百条记录。我有第二种形式用于编辑或创建新记录。当我在添加新记录后返回到第一个表单时,我会执行“激活:Me.Requery”,以便将新记录添加到列表中,但我希望它显示在屏幕上,并在新记录上显示记录选择器。有办法做到这一点吗?我假设我将 ID 保存在全局变量中,但不确定下一步要做什么。谢谢。

回复: 谢谢//t。你的回答让我朝着正确的方向前进。我将发布我的解决方案,我认为这更像是一种解决方法。必须有更好的解决方案,但这似乎可行。

表格1(列表)->表格 2(编辑/新建)并创建新记录。

Private Sub Form_Current ()
  glngID = Me.ID.Value
End Sub

Private Sub Form_Close
  gstrLastForm = "Form2"
End Sub

当我关闭表格 2 时,表格 1 处于活动状态。

Private lngSelectedRecord as Long

Private Sub Form_Activate()
  Me.Requery
  FindSelectedRecord
  If gstrLastForm = "Form2" Then
    DoCmd.GoToRecord acDataForm, "Form1", acGoTo, lngSelectedRecord
  End If 
End Sub

Private Sub FindSelectedRecord()
  ...
  Open recordset, move through records, increment counter, exit when ID found
  ...
  lngSelectedRecord = intCounter
  ...
End Sub 

Access 2007: I have one form with 100s of records displayed. I have a second form for editing or creating new records. When I return to the first form after adding a new record, I do On Activate: Me.Requery so the new record is added to the list, but I would like it to display on the screen with the record selector on the new record. Is there a way to do this? I am assuming that I save the ID in a global variable, but not sure what to do next. Thanks.

RESPONSE:
Thanks //t. Your answer got me going in the right direction. I will post my solution, which I think is more of a work-around. There has to be a better solution, but this seems to work.

Form 1 (list) -> Form 2 (edit/new) and create new record.

Private Sub Form_Current ()
  glngID = Me.ID.Value
End Sub

Private Sub Form_Close
  gstrLastForm = "Form2"
End Sub

When I close Form 2, Form 1 is active.

Private lngSelectedRecord as Long

Private Sub Form_Activate()
  Me.Requery
  FindSelectedRecord
  If gstrLastForm = "Form2" Then
    DoCmd.GoToRecord acDataForm, "Form1", acGoTo, lngSelectedRecord
  End If 
End Sub

Private Sub FindSelectedRecord()
  ...
  Open recordset, move through records, increment counter, exit when ID found
  ...
  lngSelectedRecord = intCounter
  ...
End Sub 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风和你 2024-11-13 11:41:04

这通常是通过在对话框模式下打开的表单来完成的。在大多数情况下,主窗体上有一个命令按钮“添加新记录”,单击该按钮将运行如下代码:

  DoCmd.OpenForm "MyAddForm", , , , acFormAdd, acDialog 

这将打开用于将新记录添加到新的空记录的窗体,并暂停代码。

但是,您需要知道添加的记录的 PK,因此您不能只是关闭表单并让代码继续执行。因此,通常的做法是将对话框窗体的 Visible 属性设置为 False,从中获取所需的数据,然后关闭它并执行您想要的操作:

  Dim lngPK As Long
  DoCmd.OpenForm "MyAddForm", , , , acFormAdd, acDialog 
  If Forms!MyAddForm.Tag <> "Cancel" Then
     lngPK = Forms!MyAddForm!PK
     Application.Echo False 
     Me.Requery
     With Me.RecordsetClone
       .FindFirst "[PK]=" & lngPK
       If Not .NoMatch Then
          If Me.Dirty Then
             Me.Dirty = False
          End If
          Me.Bookmark = .Bookmark
       End If
     End With
     Application.Echo True
  End If
  DoCmd.Close acForm, "MyAddForm"

在对话框窗体中,您必须隐藏默认的窗口控件,以便用户无法关闭它。相反,有两个命令按钮“保存”和“取消”。 “保存”按钮执行此操作:

  If Me.Dirty Then
     Me.Dirty = False
  End If.
  Me.Visible = False

...而“取消”按钮执行此操作:

  Me.Undo
  Me.Tag = "Cancel"
  Me.Visible = False

结果是您知道记录未保存,因此您不想对调用表单执行任何操作。

这都是标准的 Access 用户界面设计,并且是迄今为止执行此类操作最简单、最可靠的方法。

This is usually done with a form opened in dialog mode. In most cases, you'd have a command button on your main form ADD NEW RECORD, that when clicked would run code like this:

  DoCmd.OpenForm "MyAddForm", , , , acFormAdd, acDialog 

This opens the form you use for adding a new record to a new, empty record, and pauses the code.

However, you need to know the PK of the record that was added, so you can't just close the form and let the code continue. So, the usual practice is to set the dialog form's Visible property to False, get the data out of it that you need, then close it and do what you want:

  Dim lngPK As Long
  DoCmd.OpenForm "MyAddForm", , , , acFormAdd, acDialog 
  If Forms!MyAddForm.Tag <> "Cancel" Then
     lngPK = Forms!MyAddForm!PK
     Application.Echo False 
     Me.Requery
     With Me.RecordsetClone
       .FindFirst "[PK]=" & lngPK
       If Not .NoMatch Then
          If Me.Dirty Then
             Me.Dirty = False
          End If
          Me.Bookmark = .Bookmark
       End If
     End With
     Application.Echo True
  End If
  DoCmd.Close acForm, "MyAddForm"

In the dialog form, you have to hide the default window controls so the user can't close it. Instead, have two command buttons SAVE and CANCEL. The SAVE button does this:

  If Me.Dirty Then
     Me.Dirty = False
  End If.
  Me.Visible = False

...and the CANCEL button does this:

  Me.Undo
  Me.Tag = "Cancel"
  Me.Visible = False

The result is that you know that a record was not saved so you don't want to do anything to the calling form.

This is all standard Access user interface design, and it's by far the easiest and most bulletproof method for doing this kind of thing.

北方。的韩爷 2024-11-13 11:41:04

在弹出窗口中使用 on_close-method 转到插入的记录,
类似于:(

private sub on_close()
  'maby first check we're not undoing..
  docmd.gotoRecord yourform,yournewid
  me.close

目前在 osx 上,但我希望你明白这个概念......)

Use the on_close-method in your popup window to go to the inserted record,
something like:

private sub on_close()
  'maby first check we're not undoing..
  docmd.gotoRecord yourform,yournewid
  me.close

(on osx currently, but I hope you get the concept...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文