PDA

View Full Version : Adding Record To Subform From Unbound Textbox



mattster1010
05-06-2010, 02:51 AM
Morning All,

I have a simple form that has a textbox called 'Questxt' and I want to add the contents of the textbox to a subform that is bound to a table called 'Question_Tbl' via a command button.

Can anybody give me some example code of how this may work?

Regards,

Matt

OBP
05-06-2010, 03:15 AM
me.subformname.fieldname = me.Questxt
where subformname is the name of your subform, should be offered by the VBA Editor and fieldname is name of the field on the subform.

mattster1010
05-06-2010, 04:30 AM
Hi OBP,

This works but overwrites the first record in the table. The subform is showing a data sheet view of the table.

There are cuurent 16 records in the table.

Is there anyway to ensure that a new record is created rather than it overwrite the first?

Regards,

Matt

OBP
05-06-2010, 05:13 AM
Matt, unless you are controlling the Mainform as well you will need to use a Recordset to do that, you would write the Record to the table rather than to the form.

mattster1010
05-06-2010, 05:39 AM
Hi OBP,

Do you have any example of how I would write to the table with a recordset?

Regards,

Matt

OBP
05-06-2010, 06:11 AM
This code adds a new record to the table called Documents, but I have changed it to reflect the use of your fieldname.

Dim rs As Object
Set rs = CurrentDb.OpenRecordset("Documents")
With rs
.AddNew
![Document Location] = me.Questxt
.Update
.Bookmark = .LastModified
End With
rs.Close
Set rs = Nothing

mattster1010
05-06-2010, 07:43 AM
Thank you for the code,

Maybe going off the topic slightly, can I do something similar but actually delete the selected record from the subform using a command button?

Regards,

Matt

OBP
05-06-2010, 07:47 AM
You could delete it from the table.