vba Access 2010 VBA手动保存记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17597572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access 2010 VBA manually save record
提问by JonBlumfeld
an Access question this time. I have a form that is bound to a certain table and I want this form to only allow adding new entries (not editing or deleting) by clicking on a "Save" button. First problem was that the record was updated while editing the textboxes. The Solution I have has several problems
这次是访问问题。我有一个绑定到某个表的表单,我希望这个表单只允许通过单击“保存”按钮添加新条目(而不是编辑或删除)。第一个问题是在编辑文本框时更新了记录。我的解决方案有几个问题
Option Compare Database
Option Explicit
Private bSaveRecord As Boolean
Private Sub btCreateRecord_Click()
bSaveRecord = True
Me.tblUMgmtUser_UserDetailsID.Value = Me.tblUMgmtUserDetails_UserDetailsID.Value
Me.tbSetUserHashPW = "12312"
Me.cbSetInitPW = True
DoCmd.GoToRecord , , acNext
End Sub
Private Sub btResetRecord_Click()
ResetRecord
End Sub
Private Sub Form_AfterUpdate()
bSaveRecord = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not bSaveRecord Then
Cancel = True
Me.Undo
End If
End Sub
Private Sub Form_Load()
Me.Username.SetFocus
DoCmd.GoToRecord , , acNewRec
bSaveRecord = False
End Sub
Private Sub ResetRecord()
Dim cControl As Control
bSaveRecord = False
For Each cControl In Me.Controls
If cControl.Name Like "Text*" Then cControl = vbNullString
Next
Me.cbResponsible.Value = False
Me.Undo
End Sub
Problem 1: I have to add hidden textboxes to save values to the record I want to be generated automatically
问题 1:我必须添加隐藏文本框以将值保存到我想自动生成的记录中
Problem 2: the ID-column counts up every time I open the form, even if I haven't added a record previously
问题 2:每次打开表单时 ID 列都会计数,即使我之前没有添加过记录
Generally spoken my solution doesn't feel very robust and elegant. Any suggestions are greatly appreciated
一般来说,我的解决方案感觉不是很健壮和优雅。非常感谢任何建议
Many thanks Jon
非常感谢乔恩
采纳答案by PowerUser
You are binding the data entry form directly to the target table. As a result, when the user enters data, they edit the table directly including creating new records. This is why the ID auto-increments (because the user is immediately creating new records). Your current partial work-around is to use hidden textboxes to store the values.
您将数据输入表单直接绑定到目标表。结果,当用户输入数据时,他们直接编辑表,包括创建新记录。这就是 ID 自动递增的原因(因为用户正在立即创建新记录)。您当前的部分解决方法是使用隐藏的文本框来存储值。
Your goal of a data entry form sounds like a common scenario. Try this:
数据输入表单的目标听起来像是一个常见的场景。尝试这个:
First, do NOT set your target table as your form's source. Leave the source blank for now and all your fields unbound. Since they are now separate, the user can't edit existing records or add new ones just by entering data. In fact, nothing will happen if the user just enters data and then closes the form.
Go ahead and make your Save button. This button will validate the data to make sure it is what you want, then create and execute a SQL Insert query to add the data to your target table.
首先,不要将目标表设置为表单的源。现在将源保留为空白,所有字段都未绑定。由于它们现在是分开的,因此用户无法仅通过输入数据来编辑现有记录或添加新记录。事实上,如果用户只是输入数据然后关闭表单,什么都不会发生。
继续制作您的保存按钮。此按钮将验证数据以确保它是您想要的,然后创建并执行 SQL 插入查询以将数据添加到您的目标表中。
回答by hoopzbarkley
This is not my solution, but it allows you to use bound fields while preventing them from automatically updating.
这不是我的解决方案,但它允许您使用绑定字段,同时防止它们自动更新。
http://bytes.com/topic/access/insights/891249-how-stop-bound-forms-updating-automatically
http://bytes.com/topic/access/insights/891249-how-stop-bound-forms-updating-automatically