允许在表单中的自动编号字段中输入以在 Access 中创建新记录

发布于 2024-11-05 17:23:08 字数 916 浏览 0 评论 0原文

我有一个表单,它将显示表的最后一条记录,以及其他表中的一些附加字段。主表的PK是自动编号的。我需要能够允许用户更改自动编号字段,然后确定记录是否存在,如果存在 - 更新,如果不存在 - 插入新记录。我尝试在字段更改时向字段添加事件,但每当我尝试单击表单上的字段时,它只会向我发出蜂鸣声。这是代码:

Private Sub JobID_Change()

'Check Bid#, if already exists open selected record for editing
    rstOpenOrder.FindFirst "JobID = " & Me![JobID]
    Do Until rstOpenOrder.NoMatch
    With rstOpenOrder
        'Add new record to end of Recordset object.
        .Edit
        'Edit data.
        !LocationID = Me![LocationID]
        !Description = Me![JobName]
        !BaseBid = Me![BaseBid]
        !GrossMargin = Me![GrossMargin]
        !MDs = Me![ManDays]
        !BidDate = Me![BidDate]
        !ShortDate = Me![ShortDate]
        !EmployeeID = Me![EmployeeID]
        !GC = Me![GCs]
        .Update
        .FindNext "JobID = " & Me![JobID]
        'Save changes.
    End With
    Loop

End Sub

如果有人可以提供帮助,我将不胜感激!

I have a form that will display the last record of a table, with a few additional fields from other tables. The PK of the main table is auto-numbering. I need to be able to allow the user to make a change to the auto-numbering field, then determine if the record exists or not, if so - update, if not - insert new. I tried adding an event to the field on field change but whenever I try to click in the field on the form, it just beeps at me. Here is the code:

Private Sub JobID_Change()

'Check Bid#, if already exists open selected record for editing
    rstOpenOrder.FindFirst "JobID = " & Me![JobID]
    Do Until rstOpenOrder.NoMatch
    With rstOpenOrder
        'Add new record to end of Recordset object.
        .Edit
        'Edit data.
        !LocationID = Me![LocationID]
        !Description = Me![JobName]
        !BaseBid = Me![BaseBid]
        !GrossMargin = Me![GrossMargin]
        !MDs = Me![ManDays]
        !BidDate = Me![BidDate]
        !ShortDate = Me![ShortDate]
        !EmployeeID = Me![EmployeeID]
        !GC = Me![GCs]
        .Update
        .FindNext "JobID = " & Me![JobID]
        'Save changes.
    End With
    Loop

End Sub

If anyone can help, I would greatly appreciate it!

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

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

发布评论

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

评论(1

粉红×色少女 2024-11-12 17:23:08

在用户界面中公开自动编号字段通常不是一个好主意。但是,如果您仍然想提供所描述的界面,则需要执行以下操作:

  1. 添加一个未绑定文本框(我将其称为IDLookup
  2. 将 AfterUpdate 事件添加到文本框以检查现有记录
  3. 如果记录存在,则过滤表单以显示该记录
  4. 如果不存在,则将表单移动到新记录 (Me.Recordset.AddNew

这里的关键是你的查找必须完成通过未绑定文本框。 Access 不允许您编辑绑定到自动编号字段的文本框中的任何内容(有充分的理由!)。

It's not generally a good idea to expose autonumber fields in the user interface. However, if you still want to provide the interface you've described you'll need to do the following:

  1. Add an unbound text box (I'll call it IDLookup)
  2. Add an AfterUpdate event to the text box to check for an existing record
  3. If the record exists, filter the form to display that one record
  4. If not, move the form to a new record (Me.Recordset.AddNew)

The key here is that your lookup must be done via an unbound text box. Access won't let you edit anything inside a textbox bound to an AutoNumber field (for good reason!).

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