Add record on button click only

天大地大妈咪最大 提交于 2019-12-20 04:19:06

问题


I have a form that has the 'data entry' property set to yes. It is bound to a table. When I start filling in the form it automatically saves it. I do not want this to happen. I only want the form to save to the table when I press a button. Any easy way to do this? w/o vba. If i can only do this with vba let me know how to do it that what.


回答1:


The best way to do this is with an unbound form. When the user clicks save, you can run a query to update your table from the controls.

Using a recordset

 Dim rs As Recordset
 Set rs=CurrentDB.Openrecordset("MyTable")

 rs.AddNew
 rs!Field1 = Me.Field1
 rs.Update

If you wanted to update a record where you already knew the primary key, you could say:

 Dim rs As Recordset
 Set rs=CurrentDB.Openrecordset("SELECT * FROM MyTable WHERE ID=" & Me.txtID)

 rs.Edit
 rs!Field1 = Me.Field1
 rs.Update

Using a query that you have created in the query design window

SQL for the query

 INSERT INTO MyTable (Field1) 
 VALUES ( Forms!MyForm!Field1 )

VBA

This will give a warning

 DoCmd.OpenQuery "MyQuery"

This will not

 CurrentDb.Execute "Query2", dbFailOnError

You could also use dynamic SQL or a query with parameters that you assign in code.



来源:https://stackoverflow.com/questions/12205769/add-record-on-button-click-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!