Add record on button click only

孤人 提交于 2019-12-02 04:19:22

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.

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