Adding record through a text boxes enter event

I am new to Access and was trying to find the code to add to a text box's enter event to add a record after the uses hits enter, using the contents of the all the text boxes on the form for the record.  Can somebody give me some beginner VB code for this?

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

mbizupConnect With a Mentor Commented:

The easiest way to insert a record is of course by using a bound form and letting Access do the work for you.

Via code:

Dim strSQL as string
strSQL = "INSERT INTO YourTable (F1, f2,f3 ...) VALUES (" & txtText1 & "," & cboCombo1 & "," & txtText2 & ")"
currentdb.execute strSQL, dbFailOnError

f1, f2, etc represent all of the fields that your form has controls for.
You would have to expand this for ALL your controls, and use delimiters appropriate for the datatypes of each field. Text needs to be enclosed in quotes; dates need to be enclosed in #'s.

(so it can get a bit complicated).
<enter event>
<add to a text box's enter event to add a record after the uses hits enter,>

The event you are describing would actually be the textbox's after update event.

I'd recommend using a command button's click event to insert a record instead.
Hi sciarref,

If your text box is bound to table or query field, just place Dirty = False into its After Update event.
See snippet below for an example

Private Sub Field1_AfterUpdate()
Dirty = False
End Sub

Open in new window

sciarrefAuthor Commented:
Thanks works great.
All Courses

From novice to tech pro — start learning today.