允许在表单中的自动编号字段中输入以在 Access 中创建新记录
我有一个表单,它将显示表的最后一条记录,以及其他表中的一些附加字段。主表的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在用户界面中公开自动编号字段通常不是一个好主意。但是,如果您仍然想提供所描述的界面,则需要执行以下操作:
IDLookup
)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:
IDLookup
)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!).