如何通过 ms access 中的 VBA 代码更新任何字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19034043/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:44:08  来源:igfitidea点击:

how can i update any field value through VBA code in ms access

vbams-accesscomboboxms-access-2007ms-access-2010

提问by Rizwan Safdar

how can i update any field value through VBA code in ms access. when i enter values in combobox. some of the fields in relevant tables are updated as i enter the data. whereas other does not. what should i do to solve this problem.

如何通过 ms access 中的 VBA 代码更新任何字段值。当我在组合框中输入值时。当我输入数据时,相关表格中的一些字段会更新。而其他没有。我该怎么做才能解决这个问题。

in one combobox i used me.refresh command and its updating data as i enter. whereas in other combobox i did same but got no result where iam making mistakes.

在一个组合框中,我在输入时使用了 me.refresh 命令及其更新数据。而在其他组合框中,我做了同样的事情,但在我犯错误的地方没有得到结果。

further is unregistered software did such problems so that they behave different at different times.

进一步是未注册的软件会出现这样的问题,因此它们在不同的时间表现不同。

采纳答案by Linger

When you change a value of a textbox/combo box/etc on a form the record in the table is not immediately updated. The default way Access handles it is to wait until the record no longer has focus and then it updates the record in the table with any changes you made.

当您更改表单上的文本框/组合框/等的值时,表中的记录不会立即更新。Access 处理它的默认方式是等待记录不再具有焦点,然后它用您所做的任何更改更新表中的记录。

If you want to, you can force an update to the record in the table via the After Update event by using the following:

如果需要,您可以使用以下命令通过 After Update 事件强制更新表中的记录:

Private Sub txtMyFieldName_AfterUpdate()
  Me.Dirty = False
End Sub

However, I would only do this when editing an existing record. If you are entering a new record then you don't want to trigger Me.Dirty = Falseafter every control has been updated. If you do trigger Me.Dirty = Falseon new record entry and you have required fields that haven't been filled in yet, you will get an error stating that a required field cannot contain a null value.

但是,我只会在编辑现有记录时执行此操作。如果您正在输入新记录,那么您不想Me.Dirty = False在每个控件更新后触发。如果您确实Me.Dirty = False在新记录条目上触发并且您有尚未填写的必填字段,您将收到一条错误消息,指出必填字段不能包含空值。

回答by Philippe Grondier

There is a slight conceptual difference between the change of a (bound) control's value on a form and the update of the underlying field's value. The underlying field's value might not be updated before the 'update' event is fired.

表单上(绑定)控件值的更改与基础字段值的更新之间存在细微的概念差异。在触发 'update' 事件之前,可能不会更新基础字段的值。

And, of course, if the control you are dealing with is unbound, there cannot be any field update ...

而且,当然,如果您正在处理的控件未绑定,则不能有任何字段更新......

Edit:

编辑:

If you want to change an unbound control value programmatically:

如果要以编程方式更改未绑定的控件值:

myForm.controls(myControl).value = "whatever"

If you want to change a bound control and its underlying field, working on the field side

如果要更改绑定控件及其底层字段,请在字段端工作

myForm.recordset.fields(myField).value = "whatever" 
myForm.recordset.update

You might then need to refresh your control on the screen so it displays the updated value

然后,您可能需要刷新屏幕上的控件,以便它显示更新后的值

And on the control side

在控制端

myForm.controls(myControl).value = "whatever" 

You might then need to fire the update programmatically (recordset.update)event on your underlying control

然后,您可能需要在底层控件上以编程方式触发更新 (recordset.update) 事件