Add record on button click only

2019-03-01 08:25发布

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条回答
劫难
2楼-- · 2019-03-01 09:03

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.

查看更多
登录 后发表回答